![](/img/trans.png)
[英]how to save pdf file in folder without save dialog c# windows application
[英]How to grab an image and save it in a folder [c# windows application]
我正在使用 c# 创建一个 windows 应用程序。我有一个按钮应该捕获图像(整个桌面屏幕)并将其保存在文件夹中。我还需要显示图像的预览。
示例代码:
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Size.Width, Screen.PrimaryScreen.Bounds.Size.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
g.Save();
bmp.Save("D:\\file.jpg", ImageFormat.Bmp);
至于显示预览。 IMO 自己编写它并不难。
有不同的方式来执行你带来的东西。 使用Screen
class,我在网上找到了几个简单的示例。 其他人正在使用 Direct3D。
In short, the idea consists of getting the image of the desktop using the Screen
class or your favorite way, store it into a Bitmap
object and save this bitmap into a file.
至于显示预览,一旦创建了Bitmap
实例,您只需要一个PictureBox
并设置其Image
属性并向用户显示您的表单,以便他可以看到图像。
希望这可以帮助! =)
您将需要导入一些 Interop dll。
看看下面的例子很好地展示了如何捕获屏幕截图并保存到磁盘。
public void CaptureScreen(string fileName,ImageFormat imageFormat)
{
int hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()),
hdcDest = GDI32.CreateCompatibleDC(hdcSrc),
hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,
GDI32.GetDeviceCaps(hdcSrc,8),GDI32.GetDeviceCaps(hdcSrc,10)); GDI32.SelectObject(hdcDest,hBitmap);
GDI32.BitBlt(hdcDest,0,0,GDI32.GetDeviceCaps(hdcSrc,8),
GDI32.GetDeviceCaps(hdcSrc,10),hdcSrc,0,0,0x00CC0020);
SaveImageAs(hBitmap,fileName,imageFormat);
Cleanup(hBitmap,hdcSrc,hdcDest);
}
以上示例取自网站。 Perry Lee 的所有代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.