zoukankan      html  css  js  c++  java
  • 获取http和ftp地址的图片

    根据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);
                }
            }
    
        }
    View Code
  • 相关阅读:
    原生JavaScript事件详解
    如何真正重写window对象的方法
    JSLint JavaScript代码质量审查工具汉化中文版隆重发布
    {{偷偷告诉你}}本博客已适配移动端浏览
    谷歌(Chrome)浏览器调试JavaScript小技巧
    小米Web前端JavaScript面试题
    根据配置文件加载js依赖模块(JavaScript面试题)
    中移杭州研发中心
    MyBatis与Hibernate区别
    hashmap源码解析,JDK1.8和1.7的区别
  • 原文地址:https://www.cnblogs.com/sxw117886/p/7094105.html
Copyright © 2011-2022 走看看