簡體   English   中英

WPF中的System.Drawing替代品?

[英]System.Drawing alternative in wpf?

嗨,我正在從wcf rest服務加載位圖圖像時遇到一個小問題:

    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>

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM