在WPF中使用WinForms图形和绘图 [英] Using WinForms graphics and drawing in WPF

查看:60
本文介绍了在WPF中使用WinForms图形和绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我在C#窗体中使用的先前代码从WPF中的URL保存图像但是我发现它不起作用。我试图在互联网上看,但只找到了我需要的部分。有人可以'转换'这个winForms代码,所以我可以在WPF中使用它吗?

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?

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.

推荐答案

您在这部分代码中没有使用 System.Windows.Forms ,仅使用 System.Drawing ,仅适用于位图。您可以将此代码保留在WPF中而不会出现任何问题。不需要转换。



但是,请看我对这个问题的评论。



如果,由于某种原因,你不喜欢它,你可以使用类 System.Windows.Media.Imaging.WriteableBitmap

< a href =http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx> http://msdn.microsoft.com/en-us/library/system .windows.media.imaging.writeablebitmap.aspx [ ^ ]。



-SA
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


这篇关于在WPF中使用WinForms图形和绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆