2017-07-22 101 views
0

我想在Outlook中保存完整表格的圖像。我正在使用VSTO Outlook Addin。我能夠捕捉到全屏圖像,但是我自己並沒有運用表單區域。有沒有人有任何想法?如何捕獲Outlook窗體區域作爲圖像? VSTO Outlook Addin

 var bmpScreenshot = new Bitmap(this.Width, 
         this.Height, 
         System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

     var grxScreenshot = Graphics.FromImage(bmpScreenshot); 

     grxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, 
        Screen.PrimaryScreen.Bounds.Y, 
        0, 
        0, 
        Screen.PrimaryScreen.Bounds.Size, 
        CopyPixelOperation.SourceCopy); 


     string outputFileName = @"C:\Users\63530\Desktop\image.png"; 
     using (MemoryStream memory = new MemoryStream()) 
     { 
      using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite)) 
      { 
       bmpScreenshot.Save(memory, ImageFormat.Png); 
       byte[] bytes = memory.ToArray(); 
       fs.Write(bytes, 0, bytes.Length); 
      } 
     } 

回答

0

這是我如何捕獲任何窗口內容。我希望它能幫助你,但不清楚你想要捕獲什麼this。即使窗口不在前臺,此代碼也能正常工作。

[DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect); 

    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags); 

    public static Bitmap GetSnapshot(IntPtr hWnd) // capture a window by its handle 
    { 
     Int32 windowLeft; 
     Int32 windowTop; 
     Int32 windowWidth; 
     Int32 windowHeight; 
     if (hWnd == IntPtr.Zero) return null; 
     if (!GetWindowRect(hWnd, out windowLeft, out windowTop, out windowWidth, out windowHeight)) return null; 

     Bitmap bmp = new Bitmap(windowWidth, windowHeight, PixelFormat.Format32bppArgb); 
     Graphics gfxBmp = Graphics.FromImage(bmp); 
     IntPtr hdcBitmap = gfxBmp.GetHdc(); 

     PrintWindow(hWnd, hdcBitmap, 0); // from user32.dll 

     gfxBmp.ReleaseHdc(hdcBitmap); 
     gfxBmp.Dispose(); 

     return bmp; 
    } 

    private static bool GetWindowRect(IntPtr hWnd, out Int32 left, out Int32 top, out Int32 width, out Int32 height) 
    { 
     left = 0; 
     top = 0; 
     width = 0; 
     height = 0; 

     RECT rct = new RECT(); 
     if (!GetWindowRect(hWnd, ref rct)) return false; // from user32.dll 

     left = rct.Left; 
     top = rct.Top; 
     width = rct.Right - rct.Left + 1; 
     height = rct.Bottom - rct.Top + 1; 
     return true; 
    } 

    public struct RECT 
    { 
     public Int32 Left; 
     public Int32 Top; 
     public Int32 Right; 
     public Int32 Bottom; 
    } 
+0

謝謝。我試圖單獨捕捉前景表格。我的代碼中的「this」引用了我當前所在的Outlook Form區域。 –

+0

如果您可以獲得該區域的句柄,則可以使用該句柄。 – Janos

+0

你成功了嗎? – Janos

相關問題