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

第一个方法在读取某些图片会报错
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;
        }

posted @ 2016-12-06 20:06  birdhumen鸟人  阅读(1346)  评论(0编辑  收藏  举报