根据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, );
}
} }

获取http和ftp地址的图片的更多相关文章

  1. 页面中直接显示FTP中的图片

    页面中直接显示FTP中的图片 FTP根目录下有一张图片,如下 第一步: 通过如下格式,在浏览器上输入路径,确定可看到图片 ftp://root:root@127.0.0.1/111.png ftp:/ ...

  2. Java学习笔记——IO操作之以图片地址下载图片

    以图片地址下载图片 读取给定图片文件的内容,用FileInputStream public static byte[] mReaderPicture(String filePath) { byte[] ...

  3. [Swift通天遁地]五、高级扩展-(5)获取互补色、渐变色、以及图片主题颜色

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. 获取本地的IP地址(内网)

    方法一 public static String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = N ...

  5. url地址的图片路径

    url地址的图片路径: (./images/1.jpg) 中的./指根路径,有或没有都可以: (../images/1.jpg) 中的../指相对路径: (../../images/1.jpg) 中的 ...

  6. 获取本机IP地址

    这里有两种方法: //获取本机IP - (NSString *)localIPAddress { NSString *localIP = nil; struct ifaddrs *addrs; ) { ...

  7. 关于是用dotnet获取本机IP地址+计算机名的方法

    印象中在maxscript帮助文档里找到过方法,但是当时没记下来.只能通过dotnet实现了. 如果电脑有无线网卡和本地连接,可能会出现乱码,也问了写dotnet的朋友,提供了一些思路,不过最终还是使 ...

  8. 【知识积累】服务器端获取客户端的IP地址(当客户端调用由Axis开发的WebService)

    一.前言 由于项目中一个小的模块需要获取客户端的IP地址以保证安全调用webservice接口,项目中客户端使用C#编写,服务器端使用Java编写,服务器端与客户端采用Axis开发的WebServic ...

  9. 获取设备的mac地址可靠的方法

    参考自:http://www.open-open.com/lib/view/open1433406847322.html /** * 获取设备的mac地址 * * @param ac * @param ...

随机推荐

  1. tkinter界面卡死的解决办法

    0.如果点击按钮,运行了一个比较耗时的操作,那么界面会卡死 import tkinter as tk import time def onclick(text, i): time.sleep(3) t ...

  2. ES6 localStorage 类库

    无意中看到的,记录下. 用到了es6语法.支持在js中写构造函数 class CovLocalDB { constructor (name) { this.LS = null this.name = ...

  3. arcpy示范教学(一):基本操作

    arcpy基本操作 打开目录,遍历目录,打开要素类,遍历要素,打开文件,写入属性值 import arcpy import codecs # 设置工作目录 arcpy.env.workspace = ...

  4. SQL基本数据类型等

    bit   类似C#中的bool类型   true/false int   整型 nvarchar  字符串类型 float   小数型 decimal(,) 小数型  (限制小数位数) dateti ...

  5. 车牌,车架号,VIN码毫秒识别技术,汽车后市场的春天到来了

    vin码(车架号)识别运用 不仅在制造.销售.保养.保险.车辆评估.交易环节会需要录入汽车的VIN码,在交通事故处理中,作为汽车身份唯一识别码,VIN码是处理事故的执法人员必须要记录的信息之一.随着汽 ...

  6. Jmeter使用HTTP代理服务器录制脚本

    使用Jmeter录制脚本通常使用Badboy工具录制或者Jmeter自带的HTTP代理服务器录制脚本,这里说一下使用HTTP代理服务器录制时遇到的问题. 1.  Jmeter安装 下载得到Jmeter ...

  7. TPO-17 C2 Reschedule part-time job in campus dining hall

    TPO-17 C2 Reschedule part-time job in campus dining hall 第 1 段 1.Listen to a conversation between a ...

  8. 合并SQL 调优

    SELECT le.equipcode,sum(case when wo.ordertype=0 then 1 else 0 END) as wxcount,sum(case when wo.orde ...

  9. openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 四

    openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 一 openstack-r版(rocky)搭建基于centos7.4 的openstac ...

  10. 在Emacs 23里字体的调整(转自ChinaUnix.net)

    首先,在Emacs中,通过菜单Options --> Set Default Font,设置好你喜欢的字体. 然后,把光标放到你所在的字体上,用命令M-x describe-font来查看你当前 ...