C#根據(jù)http和ftp圖片地址獲取對(duì)應(yīng)圖片
本文實(shí)例為大家分享了C#根據(jù)http和ftp地址獲取對(duì)應(yīng)圖片的具體代碼,供大家參考,具體內(nèi)容如下
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);
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#中HttpWebRequest、WebClient、HttpClient的使用詳解
- C#使用HttpWebRequest與HttpWebResponse模擬用戶登錄
- C# httpwebrequest訪問HTTPS錯(cuò)誤處理方法
- 淺談C#網(wǎng)絡(luò)編程詳解篇
- 詳解C# 網(wǎng)絡(luò)編程系列:實(shí)現(xiàn)類似QQ的即時(shí)通信程序
- 總結(jié)C#網(wǎng)絡(luò)編程中對(duì)于Cookie的設(shè)定要點(diǎn)
- C# Socket網(wǎng)絡(luò)編程實(shí)例
- C#網(wǎng)絡(luò)編程基礎(chǔ)之進(jìn)程和線程詳解
- 深入學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程(上)
相關(guān)文章
C#控制臺(tái)輸出進(jìn)度和百分比的實(shí)例代碼
C#控制臺(tái)輸出進(jìn)度和百分比的實(shí)例代碼,需要的朋友可以參考一下2013-03-03
C#字符串左不足位數(shù)時(shí)補(bǔ)充0的幾種方式
想讓一個(gè)整數(shù)或字符串轉(zhuǎn)換為字符串后,如果其長(zhǎng)度不足5位,則在左邊補(bǔ)充0直到達(dá)到5位,本文給大家介紹了C#字符串左不足位數(shù)時(shí)補(bǔ)充0的幾種方式,感興趣的朋友可以參考下2024-04-04
C#實(shí)現(xiàn)快遞api接口調(diào)用方法
這篇文章主要介紹了C#實(shí)現(xiàn)快遞api接口調(diào)用方法,主要是通過快遞API網(wǎng)接口的服務(wù),使用的時(shí)候直接申請(qǐng)個(gè)接口UID即可,有需要的小伙伴來參考下吧。2015-03-03
C#正則匹配RegexOptions選項(xiàng)的組合使用方法
本文主要簡(jiǎn)單介紹RegexOptions各種選項(xiàng)的作用,并介紹如何組合使用,為初學(xué)者解除一些疑惑。2016-04-04
C#使用Newtonsoft.Json中的JObject對(duì)象
本文詳細(xì)講解了C#使用Newtonsoft.Json中JObject對(duì)象的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
C#使用oledb讀取excel表格內(nèi)容到datatable的方法
這篇文章主要介紹了C#使用oledb讀取excel表格內(nèi)容到datatable的方法,涉及C#操作oledb及datatable的相關(guān)技巧,需要的朋友可以參考下2015-05-05
Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessTexture實(shí)例深入解析
這篇文章主要為大家介紹了Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessTexture實(shí)例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

