Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to save an image from a url in WPF with previous code I have used in C# windows forms but as I have found out, it does not work the same. I have tried to look on the internet but only found parts I need. Could someone kindly 'convert' this winForms code so I can use it in WPF please?
C#
string ImageSourcePath = "http://datapoint.metoffice.gov.uk/public/data/layer/wxobs/RADAR_UK_Composite_Highres/png?TIME=" + reformedStep + "Z&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                Point[] PointSetImage = { new Point(0, 0), new Point(640, 0), new Point(0, 852) };
                Bitmap CanvasImage = new Bitmap(640, 852);
                using (Graphics GraphicImage = Graphics.FromImage(CanvasImage))
                {
                    GraphicImage.DrawImage(Image.FromStream(System.Net.HttpWebRequest.Create(ImageSourcePath).GetResponse().GetResponseStream()), PointSetImage);
                    CanvasImage.Save("C:/Users/Henry/AppData/Local/WeatherLink/" + reformedStep.Replace(":", "-") + ".png");
                }
                HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create("http://datapoint.metoffice.gov.uk/public/data/layer/wxobs/RADAR_UK_Composite_Highres/png?TIME=" + reformedStep + "Z&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                WebResponse imageResponse = imageRequest.GetResponse();

Some of the variables are missing but it is really the graphics and drawing part I need the most. Thank you.
Posted
Comments
Kenneth Haugland 23-Oct-13 14:59pm    
All you are trying to do is to create a Bitmap image in WPF, right?
Henry Hunt 23-Oct-13 15:05pm    
Yes, from a url and save it to the disk
Kenneth Haugland 23-Oct-13 15:10pm    
http://stackoverflow.com/questions/4161359/save-bitmapimage-to-file
Sergey Alexandrovich Kryukov 23-Oct-13 15:27pm    
First of all, there are no situation when using hard-coded path names can be useful.
—SA

1 solution

You are not using System.Windows.Forms in this part of code, using only System.Drawing and only for a bitmap. You can keep this code in WPF without any problems. No "conversion" will be needed.

However, please see my comment to the question.

If, by some reason, you don't like it, you can use the class System.Windows.Media.Imaging.WriteableBitmap:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Kenneth Haugland 23-Oct-13 15:32pm    
Yupp, that is also true :-) 5'ed.
Sergey Alexandrovich Kryukov 23-Oct-13 15:40pm    
Thank you, Kenneth.
—SA
Henry Hunt 25-Oct-13 14:25pm    
As you said this code does work but it limits me to what I can do as I cannot use this code with the 'System.Windows' and 'System.Drawing' namespaces both referenced as I get the ambiguity error between the point definition, that was why I needed a 'conversion'
Sergey Alexandrovich Kryukov 25-Oct-13 14:29pm    
No, there is no ambiguity, ever. Just use fully-qualified type names, directly or with the help of "using" alias.
And, as I said, you can alternatively do it in WPF. I really gave you a solution, are you going to accept it formally?
In all cases your follow-up questions are welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900