我是靠谱客的博主 欢喜蜜蜂,这篇文章主要介绍C# 返回图片的字节流byte[],现在分享给大家,希望可以做个参考。

#region 返回图片的字节流byte[]
        /// <summary>
        /// 返回图片的字节流byte[]
        /// </summary>
        /// <param name="imagePath"></param>
        /// <param name="webClient"></param>
        /// <returns></returns>
        public static byte[] getImageByte(string imagePath, WebClient webClient)
        {
            byte[] imgByte = null;
            try
            {
                //MessageBox.Show("getImageByte");
                //Stopwatch stopwatch = new Stopwatch();
                //stopwatch.Start();
                //DateTime dateStart = DateTime.Now;

                if (PubFunc.UrlDiscern(imagePath))
                {
                    Bitmap bt = new Bitmap(webClient.OpenRead(imagePath));
                    imgByte = PubFunc.ImgToByte(bt);
                }
                else
                {
                    using (FileStream files = new FileStream(imagePath, FileMode.Open))
                    {
                        imgByte = new byte[files.Length];
                        files.Read(imgByte, 0, imgByte.Length);
                        files.Close();
                    }
                }
                //stopwatch.Stop();
                //MessageBox.Show((DateTime.Now - dateStart).TotalMilliseconds.ToString());

            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
            return imgByte;
        }
        #endregion
  #region 图片转换成字节流
        /// <summary>
        /// 图片转换成字节流
        /// </summary>
        /// <param name="img">要转换的Image对象</param>
        /// <returns>转换后返回的字节流</returns>
        public static byte[] ImgToByte(Image img)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] imagedata = null;
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    imagedata = ms.GetBuffer();
                    return imagedata;
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
                return null;
            }
        }
        #endregion


最后

以上就是欢喜蜜蜂最近收集整理的关于C# 返回图片的字节流byte[]的全部内容,更多相关C#内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部