WPF RenderTargetBitmap 将 TextRenderMode 缩小为 GreyScale

发布于 2024-10-21 14:27:30 字数 245 浏览 1 评论 0原文

RenderTargetBitmap 删除将 RichtextBox 的 TextRenderingMode 降级为 GreyScale。因此,捕获的 PNG 看起来质量很差,并且与 WPF 控件不匹配。

如果我使用 WINDOWS ALT+PRINT SCREEN,则可以完美捕获文本。

那么如何才能将文本控件呈现为与 ALT+PRINT SCREEN 相同的质量。

如有任何建议,我们将不胜感激 祝

一切顺利

RenderTargetBitmap removes downgrades a RichtextBox's TextRenderingMode to GreyScale. So a captured PNG looks poor quality and doesnt match the WPF control

If I use WINDOWS ALT+PRINT SCREEN, the text is captured perfectly.

So how can I render the text control to the same quality as ALT+PRINT SCREEN.

Any advice would seriously be appreciated

All the best

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-10-28 14:27:30

您可以使用与使用 Alt + Print Screen 截屏时使用的窗口相同的技术将窗口渲染到位图中。这个想法是获取一个窗口句柄,然后使用 BitBlt 系统调用。

下面是一个示例(您需要在项目中引用 System.Drawing.dll 程序集才能使其工作):

public static void SaveToBitmapNative(Window window, FrameworkElement element, string fileName)
{
    WindowInteropHelper helper = new WindowInteropHelper(window);

    // detect the window client area position if rendering a child element
    double incX = 0, incY = 0;
    if (window != element)
    {
        System.Drawing.Point pos = new System.Drawing.Point(0, 0);
        ClientToScreen(helper.Handle, ref pos);
        incX = pos.X - (int)window.Left;
        incY = pos.Y - (int)window.Top;
    }

    // transform child position to window coordinates
    GeneralTransform transform = element.TransformToVisual(window);
    Point point = transform.Transform(new Point(0, 0));
    Rect rect = new Rect(point.X + incX, point.Y + incY, element.ActualWidth, element.ActualHeight);

    // render window into bitmap
    using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(
        (int)rect.Width, (int)rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
    {
        using (System.Drawing.Graphics memoryGraphics = System.Drawing.Graphics.FromImage(bitmap))
        {
            IntPtr dc = memoryGraphics.GetHdc();
            IntPtr windowDC = GetWindowDC(helper.Handle);

            BitBlt(dc, 0, 0, (int)rect.Width, (int)rect.Height,
                    windowDC, (int)rect.Left, (int)rect.Top, TernaryRasterOperations.SRCCOPY);

            memoryGraphics.ReleaseHdc(dc);
            ReleaseDC(helper.Handle, windowDC);
        }
        // save bitmap to file
        bitmap.Save(fileName);
    }
}

[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, TernaryRasterOperations rop);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr ptr);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("user32.dll")]
static extern bool ClientToScreen(IntPtr hWnd, ref System.Drawing.Point lpPoint);

public enum TernaryRasterOperations : uint
{
    SRCCOPY = 0x00CC0020,
    SRCPAINT = 0x00EE0086,
    SRCAND = 0x008800C6,
    SRCINVERT = 0x00660046,
    SRCERASE = 0x00440328,
    NOTSRCCOPY = 0x00330008,
    NOTSRCERASE = 0x001100A6,
    MERGECOPY = 0x00C000CA,
    MERGEPAINT = 0x00BB0226,
    PATCOPY = 0x00F00021,
    PATPAINT = 0x00FB0A09,
    PATINVERT = 0x005A0049,
    DSTINVERT = 0x00550009,
    BLACKNESS = 0x00000042,
    WHITENESS = 0x00FF0062
}

以下是调用此方法的方法

private void saveButton_Click(object sender, RoutedEventArgs e)
{
    // saves the entire window into the bitmap
    SaveToBitmapNative(this, this, "c:\\test0.png");
    // saves a child control (RichTextBox) into the bitmap
    SaveToBitmapNative(this, richTextBox, "c:\\test1.png"); 
}

注意:这对于通过 UpdateLayeredWindow 函数。

希望这有帮助,问候

You can use the same technique to render your window into the bitmap as windows using when taking a screen shot with Alt + Print Screen. The idea is to get a window handle and then render it to a bitmap using BitBlt system call.

Below is an example (you would need to reference the System.Drawing.dll assembly in your project to make it work):

public static void SaveToBitmapNative(Window window, FrameworkElement element, string fileName)
{
    WindowInteropHelper helper = new WindowInteropHelper(window);

    // detect the window client area position if rendering a child element
    double incX = 0, incY = 0;
    if (window != element)
    {
        System.Drawing.Point pos = new System.Drawing.Point(0, 0);
        ClientToScreen(helper.Handle, ref pos);
        incX = pos.X - (int)window.Left;
        incY = pos.Y - (int)window.Top;
    }

    // transform child position to window coordinates
    GeneralTransform transform = element.TransformToVisual(window);
    Point point = transform.Transform(new Point(0, 0));
    Rect rect = new Rect(point.X + incX, point.Y + incY, element.ActualWidth, element.ActualHeight);

    // render window into bitmap
    using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(
        (int)rect.Width, (int)rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
    {
        using (System.Drawing.Graphics memoryGraphics = System.Drawing.Graphics.FromImage(bitmap))
        {
            IntPtr dc = memoryGraphics.GetHdc();
            IntPtr windowDC = GetWindowDC(helper.Handle);

            BitBlt(dc, 0, 0, (int)rect.Width, (int)rect.Height,
                    windowDC, (int)rect.Left, (int)rect.Top, TernaryRasterOperations.SRCCOPY);

            memoryGraphics.ReleaseHdc(dc);
            ReleaseDC(helper.Handle, windowDC);
        }
        // save bitmap to file
        bitmap.Save(fileName);
    }
}

[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, TernaryRasterOperations rop);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr ptr);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("user32.dll")]
static extern bool ClientToScreen(IntPtr hWnd, ref System.Drawing.Point lpPoint);

public enum TernaryRasterOperations : uint
{
    SRCCOPY = 0x00CC0020,
    SRCPAINT = 0x00EE0086,
    SRCAND = 0x008800C6,
    SRCINVERT = 0x00660046,
    SRCERASE = 0x00440328,
    NOTSRCCOPY = 0x00330008,
    NOTSRCERASE = 0x001100A6,
    MERGECOPY = 0x00C000CA,
    MERGEPAINT = 0x00BB0226,
    PATCOPY = 0x00F00021,
    PATPAINT = 0x00FB0A09,
    PATINVERT = 0x005A0049,
    DSTINVERT = 0x00550009,
    BLACKNESS = 0x00000042,
    WHITENESS = 0x00FF0062
}

here's how you can call this

private void saveButton_Click(object sender, RoutedEventArgs e)
{
    // saves the entire window into the bitmap
    SaveToBitmapNative(this, this, "c:\\test0.png");
    // saves a child control (RichTextBox) into the bitmap
    SaveToBitmapNative(this, richTextBox, "c:\\test1.png"); 
}

Note: this would not work for layered windows updated via UpdateLayeredWindow function.

hope this helps, regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文