查看: 2174|回复: 0

[.NET开发] C#根据http和ftp图片地址获取对应图片

发表于 2018-2-9 08:00:00

本文实例为大家分享了C#根据http和ftp地址获取对应图片的具体代码,供大家参考,具体内容如下

  1. public class GetBitmapImageClass
  2. {
  3. public BitmapSource GetImageHttp(string url,int width)
  4. {
  5. var image = new BitmapImage();
  6. int BytesToRead = 100;
  7. if (!string.IsNullOrEmpty(url))
  8. {
  9. WebRequest request = WebRequest.Create(new Uri(url, UriKind.Absolute));
  10. request.Timeout = -1;
  11. WebResponse response = request.GetResponse();
  12. Stream responseStream = response.GetResponseStream();
  13. BinaryReader reader = new BinaryReader(responseStream);
  14. MemoryStream memoryStream = new MemoryStream();
  15. byte[] bytebuffer = new byte[BytesToRead];
  16. int bytesRead = reader.Read(bytebuffer, 0, BytesToRead);
  17. while (bytesRead > 0)
  18. {
  19. memoryStream.Write(bytebuffer, 0, bytesRead);
  20. bytesRead = reader.Read(bytebuffer, 0, BytesToRead);
  21. }
  22. image.BeginInit();
  23. image.DecodePixelWidth = width;
  24. image.CacheOption = BitmapCacheOption.OnLoad;
  25. memoryStream.Seek(0, SeekOrigin.Begin);
  26. image.StreamSource = memoryStream;
  27. image.EndInit();
  28. image.Freeze();
  29. memoryStream.Close();
  30. reader.Close();
  31. responseStream.Close();
  32. response.Close();
  33. }
  34. return image;
  35. }
  36. public BitmapSource GetImageFtp(string url, int width)
  37. {
  38. var image = new BitmapImage();
  39. if (!string.IsNullOrEmpty(url))
  40. {
  41. FtpWebRequest reqFtp;
  42. reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
  43. reqFtp.Method = WebRequestMethods.Ftp.DownloadFile;
  44. reqFtp.UseBinary = true;
  45. FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
  46. Stream ftpStream = response.GetResponseStream();
  47. MemoryStream mStream = new MemoryStream();
  48. ftpStream.CopyTo(mStream);
  49. mStream.Position = 0;
  50. int length = (int)mStream.Length;
  51. byte[] returnbyte = new byte[length];
  52. mStream.Read(returnbyte, 0, length);
  53. mStream.Close();
  54. ftpStream.Close();
  55. response.Close();
  56. System.IO.MemoryStream stream = new System.IO.MemoryStream(returnbyte);
  57. image.BeginInit();
  58. image.DecodePixelWidth = width;
  59. image.CacheOption = BitmapCacheOption.OnLoad;
  60. stream.Seek(0, SeekOrigin.Begin);
  61. image.StreamSource = stream;
  62. image.EndInit();
  63. image.Freeze();
  64. stream.Close();
  65. }
  66. return image;
  67. }
  68. [DllImport("gdi32.dll", SetLastError = true)]
  69. private static extern bool DeleteObject(IntPtr hObject);
  70. public BitmapSource ToBitmapSource(System.Drawing.Bitmap bmp)
  71. {
  72. try
  73. {
  74. var ptr = bmp.GetHbitmap();
  75. var source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
  76. ptr, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
  77. DeleteObject(ptr);
  78. return source;
  79. }
  80. catch
  81. {
  82. return null;
  83. }
  84. }
  85. //获取缩略图
  86. public BitmapSource GetBitImage(string imageLink)
  87. {
  88. //"http://172.17.1.231:8083/3050273262379466760/2017/05/28/09/340800100999/09163448402.jpg?fid=1267520"
  89. if (imageLink.StartsWith("http://"))
  90. {
  91. return GetImageHttp(imageLink,200);
  92. }
  93. //ftp格式的
  94. else if (imageLink.StartsWith("ftp://"))
  95. {
  96. return GetImageFtp(imageLink, 200);
  97. }
  98. }
  99. //获取原图
  100. public BitmapSource GetHightBitImage(string imageLink)
  101. {
  102. //"http://172.17.1.231:8083/3050273262379466760/2017/05/28/09/340800100999/09163448402.jpg?fid=1267520"
  103. if (imageLink.StartsWith("http://"))
  104. {
  105. return GetImageHttp(imageLink, 0);
  106. }
  107. //ftp格式的
  108. else if (imageLink.StartsWith("ftp://"))
  109. {
  110. return GetImageFtp(imageLink, 0);
  111. }
  112. }
  113. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

您可能感兴趣的文章:

  • asp.net(c#)获取内容第一张图片地址的函数
  • 使用C#获取远程图片 Form用户名与密码Authorization认证的实现
  • C#获取图片的后缀名解析
  • C#获取图片文件扩展名的方法
  • C#编程获取资源文件中图片的方法
  • C#远程获取图片文件流的方法
  • C#获取HTML文本的第一张图片与截取内容摘要示例代码
  • c# 应用NPOI获取Excel中的图片,保存至本地的算法
  • 使用C#正则表达式获取必应每日图片地址
  • 详解C#编程获取资源文件中图片的方法


回复

使用道具 举报