C#根据http和ftp图片地址获取对应图片
作者:幻影星辰 时间:2023-06-06 02:00:23
本文实例为大家分享了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#,http,ftp,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
2020最新 idea下载、安装与创建项目测试的教程图解
2023-10-22 08:08:14
![](https://img.aspxhome.com/file/2023/2/129952_0s.png)
Java中Date和Calendar常用方法
2023-11-12 07:35:32
android开发之Json文件的读写的示例代码
2021-12-02 11:55:18
![](https://img.aspxhome.com/file/2023/2/84712_0s.png)
Android如何通过组合的方式自定义View
2022-11-01 04:33:46
解决Eclipse的Servers视图中无法添加Tomcat6/Tomcat7的方法
2023-03-30 22:22:27
![](https://img.aspxhome.com/file/2023/6/64806_0s.png)
C#集合之队列的用法
2022-11-24 16:15:38
![](https://img.aspxhome.com/file/2023/4/108794_0s.jpg)
5种Android数据存储方式汇总
2023-08-06 06:49:04
深入讲解java线程与synchronized关键字
2023-08-29 13:56:00
C/C++ Qt StatusBar底部状态栏应用教程
2023-09-29 16:25:45
![](https://img.aspxhome.com/file/2023/0/117790_0s.png)
Spring5中的WebClient使用方法详解
2023-08-05 14:50:24
SpringBoot接口如何对参数进行校验
2021-08-30 17:25:00
![](https://img.aspxhome.com/file/2023/5/74895_0s.png)
java中Locks的使用详解
2023-11-29 00:18:38
springboot接口参数校验JSR303的实现
2023-01-19 03:14:56
![](https://img.aspxhome.com/file/2023/4/72374_0s.png)
SpringbootJPA分页 PageRequest过时的替代方法
2022-03-10 11:53:13
![](https://img.aspxhome.com/file/2023/1/68001_0s.png)
OpenCV实现反阈值二值化
2022-06-29 16:02:33
![](https://img.aspxhome.com/file/2023/6/76316_0s.jpg)
java设计模式之简单工厂模式简述
2021-06-14 17:11:20
![](https://img.aspxhome.com/file/2023/8/79158_0s.jpg)
c# 复写Equals方法的实现
2023-06-09 01:35:35
C语言实现扫雷游戏源代码
2021-10-17 14:20:44
winform实现限制及解除鼠标移动范围的方法
2023-07-17 22:11:51
android实现条目倒计时功能
2023-08-23 08:35:38