I have a problem on creating bitmap image out of my winform application.
Situation:
I have a UserControl
named as "CanvasControl
" that accepts OnPaint
method acting as canvas for my Draw Pad application. Inside this user control I have a function "PrintCanvas()
" that will create a screenshot image of the UserControl
into PNG file. Below is the PrintCanvas()
function:
public void PrintCanvas(string filename = "sample.png")
{
Graphics g = this.CreateGraphics();
//new bitmap object to save the image
Bitmap bmp = new Bitmap(this.Width, this.Height);
//Drawing control to the bitmap
this.DrawToBitmap(bmp, new Rectangle(0, 0, this.Width, this.Height));
bmp.Save(Application.StartupPath +
@"\ExperimentFiles\Experiment1" + filename, ImageFormat.Png);
bmp.Dispose();
}
This user control (CanvasControl
) is called out inside my main form where user will draw something and have an option to save afterwards using a save button. The save button will call out the "PrintCanvas()
" function of the UserControl
.
I get the output image file as expected, but the problem is it was a blank image.
What I have tried so far:
To test that it is not a syntax issue, I tried to transfer the PrintCanvas()
function into my main form and surprisingly I get an image of the whole main form on file but the UserControl
is not visible there.
Is there any other setup i missed out to make a winform UserControl
printable?
UPDATE: (DRAWING ROUTINES)
- User control acting as canvas - code here