【问题标题】:System.Drawing alternative in wpf?wpf 中的 System.Drawing 替代方案?
【发布时间】:2012-04-28 13:53:12
【问题描述】:

您好,我在从 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>

【问题讨论】:

    标签: c# wpf image xaml stream


    【解决方案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。

    【讨论】:

    • 那很好但是...("http://localhost:8000/Service/picture/{0}/{1}", width, height) 我的网络服务在图像高度和宽度的 GET 请求上取一个值。这些需要由我手动配置的图像容器设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多