我在从我的 winform 应用程序中创建位图图像时遇到问题.
I have a problem on creating bitmap image out of my winform application.
情况:
我有一个名为CanvasControl
"的 UserControl
,它接受 OnPaint
方法作为我的 Draw Pad 应用程序的画布.在此用户控件中,我有一个函数PrintCanvas()
",它将创建 UserControl
的屏幕截图图像到 PNG 文件中.下面是 PrintCanvas()
函数:
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 +
@"ExperimentFilesExperiment1" + filename, ImageFormat.Png);
bmp.Dispose();
}
这个用户控件(CanvasControl
)在我的主窗体中被调用,用户将在其中绘制一些东西,然后可以选择使用保存按钮进行保存.保存按钮会调出UserControl
的PrintCanvas()
"函数.
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:
为了测试这不是语法问题,我尝试将 PrintCanvas()
函数转移到我的主窗体中,令人惊讶的是,我在文件中获得了整个主窗体的图像,但 UserControl
在那里不可见.
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.
我是否错过了任何其他设置以使 winform UserControl
可打印?
Is there any other setup i missed out to make a winform UserControl
printable?
更新:(绘图程序)
充当画布的用户控件 - 此处代码问题中的代码给出了第一个提示,但链接中的代码显示了问题的根源:您使用了