简体   繁体   English

C#打印屏幕活动窗口

[英]C# print screen active window

I am currently trying to print screen an active window using Visual C#. 我目前正在尝试使用Visual C#在活动窗口中打印屏幕。 I have this code: 我有以下代码:

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);
}

But this code captures the SaveImageDialog as well. 但是此代码也捕获了SaveImageDialog。 Any cure to this problem? 有什么办法解决这个问题? Thanks a lot. 非常感谢。

The simplest way will be to switch the code: 最简单的方法是切换代码:

// 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);
}

First create the screenshot and than show the save dialog and save it to the disc if the dialog was closed with OK. 首先创建屏幕截图,然后显示“保存”对话框,如果单击“确定”关闭对话框,则将其保存到光盘。

The problem is, that in your code, the program has no time to repaint your form. 问题是,在您的代码中,程序没有时间重新绘制窗体。 If you want to retain the structure of your code, you would need to give it some time to process the pending events, possibly by calling Application.DoEvents . 如果要保留代码的结构,则可能需要给它一些时间来处理挂起的事件,可能需要调用Application.DoEvents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM