![](/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.