我在从我的 winform 应用程序中创建位图图像时遇到问题。
情况:
我有一个UserControl
名为 " CanvasControl
" 的方法,它接受OnPaint
充当我的 Draw Pad 应用程序的画布的方法。在此用户控件中,我有一个函数“ PrintCanvas()
”,它将创建UserControl
PNG 文件的屏幕截图图像。下面是PrintCanvas()
函数:
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();
}
CanvasControl
这个用户控件(保存按钮会调出“ PrintCanvas()
”的功能UserControl
。
我得到了预期的输出图像文件,但问题是它是一个空白图像。
到目前为止我已经尝试过:
为了测试这不是语法问题,我尝试将PrintCanvas()
函数转移到我的主窗体中,令人惊讶的是,我在文件中获得了整个主窗体的图像,但在UserControl
那里不可见。
是否有任何其他设置我错过了使 winformUserControl
可打印?
更新:(绘图程序)
- 充当画布的用户控件 -此处的代码