牛骨文教育服务平台(让学习变的简单)
博文笔记

C# 通过URL得到图片的问题

创建时间:2016-12-06 投稿人: 浏览次数:5849
第一个方法在读取某些图片会报错
public static Image get_Fill_image(string url)
        {
            var image = new Image();
            image.Source = new BitmapImage(new Uri(url, UriKind.Absolute));
            image.Stretch = Stretch.Fill;
            return image;
        }

换成第二个方法不会报错
 public static Image get_image(string url)
        {
            var image = new Image();
            try
            {
                System.Net.WebRequest webreq = System.Net.WebRequest.Create(url);
                System.Net.WebResponse webres = webreq.GetResponse();
                System.IO.Stream stream = webres.GetResponseStream();
                System.Drawing.Image img1 = System.Drawing.Image.FromStream(stream);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(img1);
                IntPtr hBitmap = bmp.GetHbitmap();
                System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                image.Source = WpfBitmap;
                image.Stretch = Stretch.Uniform;
                stream.Dispose();
            }
            catch (Exception e)
            {
                return null;
            }
            return image;
        }

阅读更多
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。