关键词搜索

源码搜索 ×
×

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

发布2015-06-17浏览5235次

详情内容

  1. #region 返回图片的字节流byte[]
  2. /// <summary>
  3. /// 返回图片的字节流byte[]
  4. /// </summary>
  5. /// <param name="imagePath"></param>
  6. /// <param name="webClient"></param>
  7. /// <returns></returns>
  8. public static byte[] getImageByte(string imagePath, WebClient webClient)
  9. {
  10. byte[] imgByte = null;
  11. try
  12. {
  13. //MessageBox.Show("getImageByte");
  14. //Stopwatch stopwatch = new Stopwatch();
  15. //stopwatch.Start();
  16. //DateTime dateStart = DateTime.Now;
  17. if (PubFunc.UrlDiscern(imagePath))
  18. {
  19. Bitmap bt = new Bitmap(webClient.OpenRead(imagePath));
  20. imgByte = PubFunc.ImgToByte(bt);
  21. }
  22. else
  23. {
  24. using (FileStream files = new FileStream(imagePath, FileMode.Open))
  25. {
  26. imgByte = new byte[files.Length];
  27. files.Read(imgByte, 0, imgByte.Length);
  28. files.Close();
  29. }
  30. }
  31. //stopwatch.Stop();
  32. //MessageBox.Show((DateTime.Now - dateStart).TotalMilliseconds.ToString());
  33. }
  34. catch (Exception ee)
  35. {
  36. MessageBox.Show(ee.ToString());
  37. }
  38. return imgByte;
  39. }
  40. #endregion
  1. #region 图片转换成字节流
  2. /// <summary>
  3. /// 图片转换成字节流
  4. /// </summary>
  5. /// <param name="img">要转换的Image对象</param>
  6. /// <returns>转换后返回的字节流</returns>
  7. public static byte[] ImgToByte(Image img)
  8. {
  9. try
  10. {
  11. using (MemoryStream ms = new MemoryStream())
  12. {
  13. byte[] imagedata = null;
  14. img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  15. imagedata = ms.GetBuffer();
  16. return imagedata;
  17. }
  18. }
  19. catch (Exception ee)
  20. {
  21. MessageBox.Show(ee.ToString());
  22. return null;
  23. }
  24. }
  25. #endregion


相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载