【问题标题】:How to capture Outlook Form Region as a image? VSTO Outlook Addin如何将 Outlook 窗体区域捕获为图像? VSTO Outlook 插件
【发布时间】:2017-12-28 13:57:22
【问题描述】:

我想在 Outlook 中保存已完成表单的图像。我正在使用 VSTO Outlook 插件。我能够捕获全屏图像,但我对表单区域本身没有运气。有人有什么想法吗?

       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);
            }
        }

【问题讨论】:

    标签: c# .net screen-capture


    【解决方案1】:

    这就是我捕获任何窗口内容的方式。希望对您有所帮助,但不清楚您要捕获的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;
        }
    

    【讨论】:

    • 谢谢。我试图单独捕获前景表格。我的代码中的“this”指的是我当前所在的 Outlook 表单区域。
    • 如果你能得到那个区域的句柄,你可以用这个。
    • 你成功了吗?
    • 抱歉,Janos 回复晚了。我没有。但是我找到了另一种处理它的方法。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    相关资源
    最近更新 更多