2017-07-22 8 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 Regionを指しています。 –

+0

その地域へのハンドルを取得できれば、これを使うことができます。 – Janos

+0

成功しましたか? – Janos

関連する問題