於是就有了第三種解決方案(暫時還沒有加恢復剪切板數據的代碼):
const uint KEYEVENTF_EXTENDEDKEY = 0x1;
const uint KEYEVENTF_KEYUP = 0x2;
const byte VK_SNAPSHOT = 0x2C;
Native.keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero);
Native.keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero);
IDataObject iObj = Clipboard.GetDataObject();
if (iObj.GetDataPresent(DataFormats.Bitmap, true))
{
Bitmap bmpScreen = iObj.GetData(DataFormats.Bitmap, true) as Bitmap;
Bitmap bmpOutput = new Bitmap((int)this.Rect.Width, (int)this.Rect.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOutput);
Rectangle destRectangle = new Rectangle(0, 0, (int)this.Rect.Width, (int)this.Rect.Height);
g.DrawImage(bmpScreen,destRectangle, (int)this.Rect.X, (int)this.Rect.Y, (int)this.Rect.Width, (int)this.Rect.Height, GraphicsUnit.Pixel);
bmpOutput.Save(sPath, System.Drawing.Imaging.ImageFormat.Bmp);
}
測試可用,只好先用著了
不過還有幾個問題,先寫下來,留待以後解決:
1. 針對第三種方案,既然可以按PrintScreen鍵截圖,那對應的API是什麼,總覺得發鍵盤消息沒有直接調API穩定
2. 針對WPF截圖有沒有更好的解決方案