I need to draw with GDI graphics a circle on my form in WPF. I can't do this with windows forms, so i have added a using. I can not use the Elipse controls from WPF. My teacher told me to do this like this.
This is my code:
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();
}
And this is the error:
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?)
You can't use GDI within WPF directly, to achieve what you need, please use WindowsFormsHost. Add references to System.Windows.Forms and WindowsFormsIntegration, add it to xaml like this (should have something inside, like Panel or whatever):
Then your code-behind will look like this and you'll be ok
UPD: good idea to make use of
using
statement here: