WPF上用GDI图形画圆
Drawing a circle with GDI graphics on WPF
我需要在 WPF 窗体上用 GDI 图形绘制一个圆圈。
我不能用 windows 形式做到这一点,所以我添加了一个 using.
我无法使用 WPF 中的 Elipse 控件。我的老师让我这样做的。
这是我的代码:
public void MakeLogo()
{
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new System.Drawing.Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
}
这是错误:
MainWindow' does not contain a definition for 'CreateGraphics' and no extension method 'CreateGraphics' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?)
您不能直接在 WPF 中使用 GDI,要实现您的需要,请使用 WindowsFormsHost。添加对 System.Windows.Forms 和 WindowsFormsIntegration 的引用,像这样将其添加到 xaml(里面应该有一些东西,比如 Panel 或其他):
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="350" Width="525">
<!--whatever goes here-->
<WindowsFormsHost x:Name="someWindowsForm">
<wf:Panel></wf:Panel>
</WindowsFormsHost>
<!--whatever goes here-->
</Window>
然后你的 code-behind 看起来像这样,你会没事的
SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
Graphics formGraphics = this.someWindowsForm.Child.CreateGraphics();
formGraphics.FillEllipse(myBrush, new System.Drawing.Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
UPD:在这里使用 using
语句是个好主意:
using (var myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green))
{
using (var formGraphics = this.someForm.Child.CreateGraphics())
{
formGraphics.FillEllipse(myBrush, new System.Drawing.Rectangle(0, 0, 200, 300));
}
}
我需要在 WPF 窗体上用 GDI 图形绘制一个圆圈。 我不能用 windows 形式做到这一点,所以我添加了一个 using. 我无法使用 WPF 中的 Elipse 控件。我的老师让我这样做的。
这是我的代码:
public void MakeLogo()
{
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new System.Drawing.Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
}
这是错误:
MainWindow' does not contain a definition for 'CreateGraphics' and no extension method 'CreateGraphics' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?)
您不能直接在 WPF 中使用 GDI,要实现您的需要,请使用 WindowsFormsHost。添加对 System.Windows.Forms 和 WindowsFormsIntegration 的引用,像这样将其添加到 xaml(里面应该有一些东西,比如 Panel 或其他):
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="350" Width="525">
<!--whatever goes here-->
<WindowsFormsHost x:Name="someWindowsForm">
<wf:Panel></wf:Panel>
</WindowsFormsHost>
<!--whatever goes here-->
</Window>
然后你的 code-behind 看起来像这样,你会没事的
SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
Graphics formGraphics = this.someWindowsForm.Child.CreateGraphics();
formGraphics.FillEllipse(myBrush, new System.Drawing.Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
UPD:在这里使用 using
语句是个好主意:
using (var myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green))
{
using (var formGraphics = this.someForm.Child.CreateGraphics())
{
formGraphics.FillEllipse(myBrush, new System.Drawing.Rectangle(0, 0, 200, 300));
}
}