简体   繁体   中英

System.Drawing alternative in wpf?

Hi I am running into a small problem with loading a Bitmap image from a wcf rest service:

    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?
            }
        }
    }

Seems there is no System.Drawing class for wpf so how can I fix this? Another problem related to this is how do I set the source:

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

In windows forms you can do this:

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

Which works fine but wpf obviously has to annoy me to no end!

Im really hoping there is something simple that can be done here?

        <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 shouldn't annoy you. It's even easier.

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

Your view model could be something like

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"; }
    }
}

and of course you would need to implement INotifyPropertyChanged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM