C#打印屏幕活动窗口

C#打印屏幕活动窗口,c#,printing,window,screen,C#,Printing,Window,Screen,我目前正在尝试使用Visual C#打印活动窗口的屏幕。我有以下代码: SaveFileDialog saveImageDialog = new SaveFileDialog(); saveImageDialog.Title = "Select output file:"; saveImageDialog.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; //saveImageDialog.FileName = pri

我目前正在尝试使用Visual C#打印活动窗口的屏幕。我有以下代码:

SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Title = "Select output file:";
saveImageDialog.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
//saveImageDialog.FileName = printFileName;
if (saveImageDialog.ShowDialog() == DialogResult.OK)
{
    // Set the bitmap object to the size of the screen
    bmpScreenshot = new Bitmap(this.Bounds.Width, this.Bounds.Height, PixelFormat.Format32bppArgb);
    // Create a graphics object from the bitmap
    gfxScreenshot = Graphics.FromImage(bmpScreenshot);
    // Take the screenshot from the upper left corner to the right bottom corner
    gfxScreenshot.CopyFromScreen(this.Bounds.X, this.Bounds.Y, 0, 0, this.Bounds.Size, CopyPixelOperation.SourceCopy);
    // Save the screenshot to the specified path that the user has chosen
    bmpScreenshot.Save(saveImageDialog.FileName, ImageFormat.Png);
}

但这段代码也捕获了SaveImageDialog。这个问题有什么解决办法吗?非常感谢。

最简单的方法是切换代码:

// Set the bitmap object to the size of the screen
bmpScreenshot = new Bitmap(this.Bounds.Width, this.Bounds.Height,
                           PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(this.Bounds.X, this.Bounds.Y, 0, 0,
                             this.Bounds.Size, CopyPixelOperation.SourceCopy);

SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Title = "Select output file:";
saveImageDialog.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
//saveImageDialog.FileName = printFileName;
if (saveImageDialog.ShowDialog() == DialogResult.OK)
{
    // Save the screenshot to the specified path that the user has chosen
    bmpScreenshot.Save(saveImageDialog.FileName, ImageFormat.Png);
}
首先创建屏幕截图,然后显示“保存”对话框,如果对话框用“确定”关闭,则将其保存到光盘


问题是,在代码中,程序没有时间重新绘制表单。如果您想保留代码的结构,您需要给它一些时间来处理挂起的事件,可能是通过调用。

啊,为什么我没有想到它?这太容易了。谢谢!如果我可能会问另一个问题,我只想捕获应用程序的预定义区域,例如,我只想打印组框的屏幕。我将此.bounds更改为groupbox.bounds,但它不起作用。它捕获其他区域,但不捕获组框。有什么想法吗?@cnewbie:这真的是另一个问题,请将其作为一个问题发布:)@cnewbie:你没有;-)你通过接受一个答案来证明你的问题得到了回答,而你已经接受了。所以这里一切都很好,只需发布一个新问题