本文實例為大家分享了C#根據(jù)http和ftp地址獲取對應圖片的具體代碼,供大家參考,具體內(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);
}
}
}另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.chinadenli.net,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)頁標題:C#根據(jù)http和ftp圖片地址獲取對應圖片-創(chuàng)新互聯(lián)
瀏覽路徑:http://www.chinadenli.net/article2/djhpoc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設、網(wǎng)站設計公司、定制開發(fā)、網(wǎng)站制作、關鍵詞優(yōu)化、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容