积极答复者
在C#的工程里面如何截取应用程序自己的界面尼?

问题
答案
-
- 已建议为答案 john_shen 2012年8月31日 4:23
- 已标记为答案 Jie BaoModerator 2012年9月5日 6:37
全部回复
-
- 已建议为答案 john_shen 2012年8月31日 4:23
- 已标记为答案 Jie BaoModerator 2012年9月5日 6:37
-
class CaptureScreen { public static void startCS() { DoWorkAsync(); } private async static void DoWorkAsync() { var response = await Task<string>.Factory.StartNew(() => { // Time consuming task (data is still accessible here) PrintScreen(); return "ok"; }); // Completion task (data is still accessible here) if (response == "ok") { SaveCapturePIC(); } } public static void PrintScreen() { CaptureApi.keybd_event((byte)0x2c, 0, 0, 0);//keydown CaptureApi.keybd_event((byte)0x2c, 0, 2, 0);//keyup } public class CaptureApi { [DllImport("user32.dll")] public static extern void keybd_event( byte bVk,// 虛擬鍵值 byte bScan,// 硬體掃描碼 int dwFlags,// 動作標識 int dwExtraInfo// 與鍵盤動作關聯的輔助訊息 ); } public async static void SaveCapturePIC() { // Get the bitmap and display it. var dataPackageView = Clipboard.GetContent(); if (dataPackageView.Contains(StandardDataFormats.Bitmap)) { IRandomAccessStreamReference imageReceived = null; try { //獲取DataPackageView imageReceived = await dataPackageView.GetBitmapAsync(); } catch (Exception ex) { Debug.WriteLine("從剪貼板中檢索圖像錯誤: " + ex.Message); } using (var sourceStream = await imageReceived.OpenReadAsync()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream); BitmapTransform transform = new BitmapTransform() { ScaledHeight = decoder.PixelHeight, ScaledWidth = decoder.PixelWidth }; PixelDataProvider pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); StorageFolder pictureFolder = KnownFolders.PicturesLibrary; StorageFile destinationFile = await pictureFolder.CreateFileAsync("test.png", CreationCollisionOption.ReplaceExisting); using (var destinationStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, destinationStream); encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, decoder.PixelWidth, decoder.PixelHeight, 96, 96, pixelData.DetachPixelData()); await encoder.FlushAsync(); } } } else { Debug.WriteLine("剪貼板中的圖片格式不可用"); } } }
截屏用此段程序,已实际试过是可用的。
- 已编辑 andylin5 2012年9月4日 8:08
-
此代码中使用了 keybd_event http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx 为 desktop apps only
虽然你可以在本地实现其功能,但是无法通过WACK的Testing, 不能被应用商店接受,使用了非支持的API是不被接受的。这里列出了支持的 Win32 and COM API http://msdn.microsoft.com/en-us/library/windows/apps/br205757.aspx 出这些外,使用其他API会造成通不过WACK测试。
Bob Bao [MSFT]
MSDN Community Support | Feedback to us