欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#根据http和ftp图片地址获取对应图片

程序员文章站 2023-11-29 12:08:46
本文实例为大家分享了c#根据http和ftp地址获取对应图片的具体代码,供大家参考,具体内容如下 public class getbitmapimageclass...

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

 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。