码迷,mamicode.com
首页 > Windows程序 > 详细

C# 返回图片的字节流byte[]

时间:2015-06-17 09:35:52      阅读:235      评论:0      收藏:0      [点我收藏+]

标签: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#   返回   图片   字节流   byte   

原文地址:http://blog.csdn.net/jiankunking/article/details/46529473

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!