0

嗨,我在从 wcf 休息服务加载位图图像时遇到了一个小问题:

    public Image GetImage(int width, int height)
    {
        string uri = string.Format("http://localhost:8000/Service/picture/{0}/{1}", width, height);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                return new Bitmap(stream); //no System.Drawing.Bitmap class in wpf?
            }
        }
    }

似乎没有用于 wpf 的 System.Drawing 类,那么我该如何解决这个问题?与此相关的另一个问题是如何设置源:

image1.Source = GetImage(image1.Height, image1.Width); //best overload for this line
// also not sure if source would be correct?

在 Windows 窗体中,您可以这样做:

pictureBox1.Image = GetImage(pictureBox1.Height, pictureBox1.Width); 

哪个工作正常,但 wpf 显然必须让我无休止!

我真的希望在这里可以做一些简单的事情?

        <GroupBox Height="141" HorizontalAlignment="Left" Name="groupBox1" VerticalAlignment="Top" Width="141" BorderBrush="#FFA3A3A3" Background="#37000000" Margin="1,21,0,0">
            <Image Name="image1" Stretch="Fill"/>
        </GroupBox>
4

1 回答 1

1

WPF 不应该惹恼你。它甚至更容易。

    <GroupBox Height={Binding Height}" Width="{Binding Width"}>
        <Image Source="{Binding MyImageUrl}" />
    </GroupBox>

您的视图模型可能类似于

public class ImageViewModel : INotifyPropertyChanged 
{
    public string ImageUrl
    {
        get
        {
            return "your url here";
        }
    }

    public double Width
    {
        get { return "required width"; }
    }

    public double Height
    {
        get { return "required height"; }
    }
}

当然你需要实现 INotifyPropertyChanged。

于 2012-04-28T14:11:17.993 回答