根据http和ftp图片地址获取对应图片的缩略图和原图

public class GetBitmapImageClass
{
public BitmapSource GetImageHttp(string url,int width)
{
var image = new BitmapImage();
int BytesToRead = ;
if (!string.IsNullOrEmpty(url))
{
WebRequest request = WebRequest.Create(new Uri(url, UriKind.Absolute));
request.Timeout = -;
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, , BytesToRead); while (bytesRead > )
{
memoryStream.Write(bytebuffer, , bytesRead);
bytesRead = reader.Read(bytebuffer, , BytesToRead);
} image.BeginInit();
image.DecodePixelWidth = width;
image.CacheOption = BitmapCacheOption.OnLoad;
memoryStream.Seek(, 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 = ;
int length = (int)mStream.Length;
byte[] returnbyte = new byte[length];
mStream.Read(returnbyte, , 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(, 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,);
}
//ftp格式的
else if (imageLink.StartsWith("ftp://"))
{
return GetImageFtp(imageLink, );
}
} //获取原图
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, );
}
//ftp格式的
else if (imageLink.StartsWith("ftp://"))
{
return GetImageFtp(imageLink, );
}
} }

最新文章

  1. jQuery实现鼠标经过图片变亮效果
  2. 在yii框架中如何连接数据库mongodb
  3. C++的深拷贝与浅拷贝
  4. Linux gnome
  5. mysql卸载
  6. C# SerialPort的简单使用
  7. web.xml 的加载顺序
  8. 二叉搜索树(Binary Search Tree)--C语言描述(转)
  9. Python 经典面试题汇总之框架篇
  10. hbase-default.xml(Hbase 默认参数翻译)
  11. CodeForces - 1013B And 与运算暴力
  12. Python 语法提示vim配置
  13. 在web.xml中配置监听器来控制ioc容器生命周期
  14. GPT(保护分区)解决办法
  15. android studio 自动导入包
  16. 如何在Macbook苹果笔记本上按右键点击(适用小米黑苹果)
  17. [转]Winform开发框架的重要特性总结
  18. VMware Tools安装和卸载
  19. JavaScript——引用类型之数组
  20. 别忽视UIImage中的方向属性, imageOrientation-转

热门文章

  1. 1. oracle12C的安装
  2. 【BZOJ2754】[SCOI2012]喵星球上的点名
  3. C++ OI图论 学习笔记(初步完结)
  4. Java 中的正则(Pattern)
  5. 二分查找的C#实现
  6. Maven学习(十一)-----使用Maven创建Web应用程序项目
  7. Node开发项目管理工具 Grunt 对比 Gulp
  8. cs231n学习笔记(一)计算机视觉及其发展史
  9. PowerShell自定义修改远程桌面RDP端口
  10. JS加密库