打印屏幕活动窗口

打印屏幕活动窗口

本文介绍了C#打印屏幕活动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently trying to print screen an active window using 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);
}

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

But this code captures the SaveImageDialog as well. 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.

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

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.

这篇关于C#打印屏幕活动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:24