SoFunction
Updated on 2025-03-06

C# obtains corresponding pictures based on http and ftp image addresses

This article shares the specific code of C# to obtain corresponding pictures based on http and ftp addresses for your reference. The specific content is as follows

public class GetBitmapImageClass
 {
  public BitmapSource GetImageHttp(string url,int width)
  {
   var image = new BitmapImage();
   int BytesToRead = 100;
   if (!(url))
   {
    WebRequest request = (new Uri(url, ));
     = -1;
    WebResponse response = ();
    Stream responseStream = ();
    BinaryReader reader = new BinaryReader(responseStream);
    MemoryStream memoryStream = new MemoryStream();

    byte[] bytebuffer = new byte[BytesToRead];
    int bytesRead = (bytebuffer, 0, BytesToRead);

    while (bytesRead > 0)
    {
     (bytebuffer, 0, bytesRead);
     bytesRead = (bytebuffer, 0, BytesToRead);
    }

    ();
     = width;
     = ;
    (0, );

     = memoryStream;
    ();
    ();
    ();
    ();
    ();
    ();
   }
   return image;
  }

  public BitmapSource GetImageFtp(string url, int width)
  {
   var image = new BitmapImage();
   if (!(url))
   {
    FtpWebRequest reqFtp;
    reqFtp = (FtpWebRequest)(new Uri(url));

     = ;
     = true;
    FtpWebResponse response = (FtpWebResponse)();
    Stream ftpStream = ();
    MemoryStream mStream = new MemoryStream();
    (mStream);
     = 0;
    int length = (int);
    byte[] returnbyte = new byte[length];
    (returnbyte, 0, length);

    ();
    ();
    ();

     stream = new (returnbyte);
    ();
     = width;
     = ;
    (0, );

     = stream;
    ();
    ();
    ();
   }
   return image;

  }


  [DllImport("", SetLastError = true)]
  private static extern bool DeleteObject(IntPtr hObject);

  public BitmapSource ToBitmapSource( bmp)
  {
   try
   {
    var ptr = ();
    var source = (
     ptr, , , ());
    DeleteObject(ptr);
    return source;
   }
   catch
   {
    return null;
   }
  }

  //Get thumbnail  public BitmapSource GetBitImage(string imageLink)
  {
   //"http://172.17.1.231:8083/3050273262379466760/2017/05/28/09/340800100999/?fid=1267520"
   if (("http://"))
   {
    return GetImageHttp(imageLink,200);
   }
   // ftp format   else if (("ftp://"))
   {
    return GetImageFtp(imageLink, 200);
   }
  }

  //Get the original image  public BitmapSource GetHightBitImage(string imageLink)
  {
   //"http://172.17.1.231:8083/3050273262379466760/2017/05/28/09/340800100999/?fid=1267520"
   if (("http://"))
   {
    return GetImageHttp(imageLink, 0);
   }
   // ftp format   else if (("ftp://"))
   {
    return GetImageFtp(imageLink, 0);
   }
  }

 }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.