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