您当前的位置: 首页 >  c#

衣舞晨风

暂无认证

  • 0浏览

    0关注

    1156博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

衣舞晨风 发布时间:2015-06-17 08:09:33 ,浏览量:0

#region 返回图片的字节流byte[]
        /// 
        /// 返回图片的字节流byte[]
        /// 
        /// 
        /// 
        /// 
        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 图片转换成字节流
        /// 
        /// 图片转换成字节流
        /// 
        /// 要转换的Image对象
        /// 转换后返回的字节流
        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
关注
打赏
1647422595
查看更多评论
立即登录/注册

微信扫码登录

0.0400s