2

我想在我的 Mainform 上制作一个面板的屏幕截图。此屏幕截图应在用户选择子表单上的某些选项后制作。一开始一切都很好,但现在屏幕截图包含子表单的一部分。

子表单如下打开:

private void Bexport_Click(object sender, EventArgs e) //button
{
    ex = new Export();
    initexForm();
    ex.FormClosed += this.exFormClosed;
    ex.TXTfilename.Focus();
    ex.ShowDialog(this);
}

制作截图的函数:

void exFormClosed(object sender, EventArgs e)
{
    try
    {
        System.Drawing.Rectangle bounds = Mainpanel.Bounds;
        bounds.Width = bounds.Width - 6;
        bounds.Height = bounds.Height - 4;
        using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(
                         Mainpanel.PointToScreen(new Point()).X + 3,
                         Mainpanel.PointToScreen(new Point()).Y + 2, 0,
                         0, bounds.Size);
            }

            bitmap.Save(Application.StartupPath + temppic.bmp);
            Document doc = new Document();
            ...

我使用了事件FormClosedFormClosing,结果相似。然后我试图隐藏子窗体,ex.Hide()但它隐藏了整个程序,这意味着屏幕截图从程序后面显示了桌面。

有人知道如何确保在制作屏幕截图之前关闭子表单吗?

乔纳森

4

3 回答 3

4

问题可能是子窗体关闭后主窗体没有时间重新绘制。

this.Update();

将强制表单重新绘制(http://msdn.microsoft.com/en-us/library/system.windows.forms.control.update.aspx

于 2013-09-03T12:36:09.330 回答
0

您要做的是创建一个虚拟表单,该表单与您要绘制的控件的大小相同,然后将控件添加到虚拟表单并显示表单并从虚拟表单中绘制控件。

public Bitmap ControlToBitmap(Control ctrl)
{
    Bitmap image = new Bitmap(ctrl.Width, ctrl.Height);
    //Create form
    Form f = new Form();
    //add control to the form
    f.Controls.Add(ctrl);
    //set the size of the form to the size of the control
    f.Size = ctrl.Size;
    //draw the control to the bitmap
    ctrl.DrawToBitmap(image, new Rectangle(0, 0, ctrl.Width, ctrl.Height));
    //dispose the form
    f.Dispose();
    return image;
}

所以如果你这样称呼它:

void exFormClosed(object sender, EventArgs e)
{
    Bitmap bitmap ControlToBitmap(Mainpanel);
    bitmap.Save(Application.StartupPath + temppic.bmp);
    Document doc = new Document();
    ...

即使表单已经关闭,这也将起作用。

于 2013-09-03T12:38:02.307 回答
0
void exFormClosed(object sender, EventArgs e)
{
    try
    {
        Application.DoEvents();
        System.Drawing.Rectangle bounds = Mainpanel.Bounds;
        bounds.Width = bounds.Width - 6;
        bounds.Height = bounds.Height - 4;
        using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                    g.CopyFromScreen(
                            Mainpanel.PointToScreen(new Point()).X + 3,
                            Mainpanel.PointToScreen(new Point()).Y + 2,
                            0, 0, bounds.Size);
            }

...

于 2013-09-03T12:39:38.093 回答