Obtain images of http and ftp addresses,

Source: Internet
Author: User

Obtain images of http and ftp addresses,

Obtain the thumbnail and original image of the corresponding image based on the http and ftp image addresses.

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 (byt Ebuffer, 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 (); respon SeStream. 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 ;}// obtain the thumbnail 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);} // else if (imageLink. startsWith ("ftp: //") {return GetImageFtp (imageLink, 200) ;}// obtain the original 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);} // else if (imageLink. startsWith ("ftp: //") {return GetImageFtp (imageLink, 0 );}}}
View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.