0

我在ImageBrushStream. 以下代码用于使用 填充RectangleWPF ImageBrush

        ImageBrush imgBrush = new ImageBrush();
        imgBrush.ImageSource = new BitmapImage(new Uri("\\image.png", UriKind.Relative));
        Rectangle1.Fill = imgBrush;

我想要做的是调用 aWebRequest并获取 a 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;

问题是我不知道如何设置我的imgBrush.ImageSourceusing response.GetResponseStream(). 我怎样才能Stream在我的ImageBrush?

4

1 回答 1

0

构造BitmapImage函数没有以 aStream作为参数的重载。
要使用响应流,您应该使用无参数构造函数并设置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;
于 2015-05-09T11:06:34.850 回答