欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#根據(jù)http和ftp圖片地址獲取對(duì)應(yīng)圖片

 更新時(shí)間:2017年06月30日 09:38:34   作者:幻影星辰  
這篇文章主要為大家詳細(xì)介紹了C#根據(jù)http和ftp圖片地址獲取對(duì)應(yīng)圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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);
   }
  }

 }

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#生成XML的三種途徑小結(jié)

    C#生成XML的三種途徑小結(jié)

    為了全面,這里都將XML保存到文件中,有三種生成XML的方式,需要的朋友可以參考下
    2013-05-05
  • C# WPF利用Clip屬性實(shí)現(xiàn)截屏框功能

    C# WPF利用Clip屬性實(shí)現(xiàn)截屏框功能

    這篇文章主要為大家詳細(xì)介紹了C# WPF如何利用Clip屬性實(shí)現(xiàn)截屏框功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • 解析如何正確使用SqlConnection的實(shí)現(xiàn)方法

    解析如何正確使用SqlConnection的實(shí)現(xiàn)方法

    本篇文章對(duì)如何正確使用SqlConnection的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C#使用dynamic一行代碼實(shí)現(xiàn)反射操作

    C#使用dynamic一行代碼實(shí)現(xiàn)反射操作

    dynamic的出現(xiàn)讓C#具有了弱語言類型的特性。編譯器在編譯的時(shí)候不再對(duì)類型進(jìn)行檢查,編譯時(shí)默認(rèn)dynamic對(duì)象支持你想要的任何特性,這篇文章主要介紹了C#用dynamic一行代碼實(shí)現(xiàn)反射操作,需要的朋友可以參考下
    2023-04-04
  • C#使用游標(biāo)實(shí)現(xiàn)補(bǔ)間函數(shù)

    C#使用游標(biāo)實(shí)現(xiàn)補(bǔ)間函數(shù)

    這篇文章主要為大家詳細(xì)介紹了C#使用游標(biāo)實(shí)現(xiàn)補(bǔ)間函數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#從文件或標(biāo)準(zhǔn)輸入設(shè)備讀取指定行的方法

    C#從文件或標(biāo)準(zhǔn)輸入設(shè)備讀取指定行的方法

    這篇文章主要介紹了C#從文件或標(biāo)準(zhǔn)輸入設(shè)備讀取指定行的方法,涉及C#文件及IO操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C# 注冊(cè)表 操作實(shí)現(xiàn)代碼

    C# 注冊(cè)表 操作實(shí)現(xiàn)代碼

    Windows 操作系統(tǒng)的注冊(cè)表包含了很多有關(guān)計(jì)算機(jī)運(yùn)行的配置方式,打開注冊(cè)表我們可以看到注冊(cè)表是按類似于目錄的樹結(jié)構(gòu)組織的
    2009-07-07
  • Unity游戲開發(fā)中的橋接模式

    Unity游戲開發(fā)中的橋接模式

    橋接模式是Unity游戲開發(fā)中常用的設(shè)計(jì)模式之一,用于將抽象部分與實(shí)現(xiàn)部分分離,從而使它們可以獨(dú)立地變化。通過橋接模式,不同的抽象類可以與不同的實(shí)現(xiàn)類組合使用,從而實(shí)現(xiàn)更加靈活和可擴(kuò)展的系統(tǒng)設(shè)計(jì)。常見的應(yīng)用包括游戲中的場(chǎng)景渲染、UI界面設(shè)計(jì)、音效播放等
    2023-05-05
  • C#通過XML節(jié)點(diǎn)屬性/屬性值讀取寫入XML操作代碼實(shí)例

    C#通過XML節(jié)點(diǎn)屬性/屬性值讀取寫入XML操作代碼實(shí)例

    本文主要介紹C#通過XML節(jié)點(diǎn)屬性、屬性值對(duì)XML的讀取,寫入操作,大家參考使用吧
    2013-11-11
  • winform實(shí)現(xiàn)限制及解除鼠標(biāo)移動(dòng)范圍的方法

    winform實(shí)現(xiàn)限制及解除鼠標(biāo)移動(dòng)范圍的方法

    這篇文章主要介紹了winform實(shí)現(xiàn)限制及解除鼠標(biāo)移動(dòng)范圍的方法,涉及C#控制WinForm鼠標(biāo)事件屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09

最新評(píng)論