我在从Stream创建ImageBrush时遇到了困难。下面的代码用于使用ImageBrush填充WPF Rectangle
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri("\\image.png", UriKind.Relative));
Rectangle1.Fill = imgBrush;我想要做的是调用一个WebRequest并获取一个Stream。然后我想用Stream图像填充我的矩形。代码如下:
ImageBrush imgBrush = new ImageBrush();
WebRequest request = WebRequest.Create(iconurl);
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
imgBrush.ImageSource = new BitmapImage(s); // Here is the problem
Rectangle1.Fill = imgBrush;问题是我不知道如何使用response.GetResponseStream()设置我的imgBrush.ImageSource。如何在我的ImageBrush中使用Stream
发布于 2015-05-09 19:06:35
BitmapImage constructors没有以Stream作为参数重载。
若要使用响应流,应使用无参数构造函数并设置StreamSource属性。
它看起来像这样:
// Get the stream for the image
WebRequest request = WebRequest.Create(iconurl);
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
// Load the stream into the image
BitmapImage image = new BitmapImage();
image.StreamSource = s;
// Apply image as source
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = image;
// Fill the rectangle
Rectangle1.Fill = imgBrush;https://stackoverflow.com/questions/30133061
复制相似问题