91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#根據http和ftp圖片地址獲取對應圖片

發布時間:2020-08-20 02:03:03 來源:腳本之家 閱讀:378 作者:幻影星辰 欄目:編程語言

本文實例為大家分享了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);
   }
  }

 }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

青浦区| 永平县| 陈巴尔虎旗| 洛隆县| 辽中县| 平度市| 台南市| 巴林右旗| 二手房| 贵州省| 昌邑市| 兴化市| 闻喜县| 炎陵县| 莲花县| 江口县| 庆阳市| 宁陕县| 桓仁| 西城区| 松原市| 仙游县| 漳州市| 甘南县| 康保县| 英超| 金堂县| 沅江市| 三明市| 视频| 佛坪县| 琼海市| 阳城县| 新津县| 和林格尔县| 伊吾县| 江门市| 海晏县| 嵊州市| 蒲江县| 和硕县|