C#根据http和ftp图片地址获取对应图片
来源:本站原创|时间:2020-01-10|栏目:C#教程|点击: 次
本文实例为大家分享了C#根据http和ftp地址获取对应图片的具体代码,供大家参考,具体内容如下
public class GetBitmapImageClass { public BitmapSource GetImageHttp(string url,int width) { var image = new BitmapImage(); int BytesToRead = 100; if (!string.IsNullOrEmpty(url)) { WebRequest request = WebRequest.Create(new Uri(url, UriKind.Absolute)); request.Timeout = -1; WebResponse response = request.GetResponse(); Stream responseStream = response.GetResponseStream(); BinaryReader reader = new BinaryReader(responseStream); MemoryStream memoryStream = new MemoryStream(); byte[] bytebuffer = new byte[BytesToRead]; int bytesRead = reader.Read(bytebuffer, 0, BytesToRead); while (bytesRead > 0) { memoryStream.Write(bytebuffer, 0, bytesRead); bytesRead = reader.Read(bytebuffer, 0, BytesToRead); } image.BeginInit(); image.DecodePixelWidth = width; image.CacheOption = BitmapCacheOption.OnLoad; memoryStream.Seek(0, SeekOrigin.Begin); image.StreamSource = memoryStream; image.EndInit(); image.Freeze(); memoryStream.Close(); reader.Close(); responseStream.Close(); response.Close(); } return image; } public BitmapSource GetImageFtp(string url, int width) { var image = new BitmapImage(); if (!string.IsNullOrEmpty(url)) { FtpWebRequest reqFtp; reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url)); reqFtp.Method = WebRequestMethods.Ftp.DownloadFile; reqFtp.UseBinary = true; FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse(); Stream ftpStream = response.GetResponseStream(); MemoryStream mStream = new MemoryStream(); ftpStream.CopyTo(mStream); mStream.Position = 0; int length = (int)mStream.Length; byte[] returnbyte = new byte[length]; mStream.Read(returnbyte, 0, length); mStream.Close(); ftpStream.Close(); response.Close(); System.IO.MemoryStream stream = new System.IO.MemoryStream(returnbyte); image.BeginInit(); image.DecodePixelWidth = width; image.CacheOption = BitmapCacheOption.OnLoad; stream.Seek(0, SeekOrigin.Begin); image.StreamSource = stream; image.EndInit(); image.Freeze(); stream.Close(); } return image; } [DllImport("gdi32.dll", SetLastError = true)] private static extern bool DeleteObject(IntPtr hObject); public BitmapSource ToBitmapSource(System.Drawing.Bitmap bmp) { try { var ptr = bmp.GetHbitmap(); var source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( ptr, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); DeleteObject(ptr); return source; } catch { return null; } } //获取缩略图 public BitmapSource GetBitImage(string imageLink) { //"http://172.17.1.231:8083/3050273262379466760/2017/05/28/09/340800100999/09163448402.jpg?fid=1267520" if (imageLink.StartsWith("http://")) { return GetImageHttp(imageLink,200); } //ftp格式的 else if (imageLink.StartsWith("ftp://")) { return GetImageFtp(imageLink, 200); } } //获取原图 public BitmapSource GetHightBitImage(string imageLink) { //"http://172.17.1.231:8083/3050273262379466760/2017/05/28/09/340800100999/09163448402.jpg?fid=1267520" if (imageLink.StartsWith("http://")) { return GetImageHttp(imageLink, 0); } //ftp格式的 else if (imageLink.StartsWith("ftp://")) { return GetImageFtp(imageLink, 0); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
上一篇:C#配置文件操作类分享
栏 目:C#教程
下一篇:C#调用微信接口的相关代码
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5613.html
您可能感兴趣的文章
- 01-10Extjs4如何处理后台json数据中日期和时间
- 01-10C#及WPF获取本机所有字体和颜色的方法
- 01-10http图片上传安全性问题 根据ContentType (MIME) 判断其实不准确、不
- 01-10同步调用和异步调用WebService
- 01-10C#编程自学之数据类型和变量二
- 01-10C#编程自学之数据类型和变量三
- 01-10C#编程自学之运算符和表达式
- 01-10C#装箱和拆箱原理详解
- 01-10C#编程自学之类和对象
- 01-10C#编程和Visual Studio使用技巧(下)
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10SublimeText编译C开发环境设置
- 01-11ajax实现页面的局部加载
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法实例总结
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 04-02jquery与jsp,用jquery