控件托管WPF控件上的DrawToBitmap问题,DPI大于100% [英] Issue with DrawToBitmap on control hosting WPF control with DPI greater than 100%

查看:231
本文介绍了控件托管WPF控件上的DrawToBitmap问题,DPI大于100%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个托管WPF控件的Windows窗体控件。  WPF控件只显示图像,用户可以右键单击并选择"复制"按钮。将图像复制到剪贴板。 当用户将图像粘贴到绘图程序时,如果DPI设置为100%(96dpi),则所有
都可正常工作,否则(当DPI为120或更高时),它会被截止。

I have a Windows Forms control hosting a WPF control.  The WPF control simply display an image and the user can right click and select "Copy" to copy the image to the clipboard.  When the user paste the image to a paint program everything work fine if the DPI setting is at 100% (96dpi), otherwise (when DPI is at 120 or greater) it gets cutoff.

示例图片

之前:

之后:(dpi @ 120)

After: (dpi @ 120)

这就是我在做什么到目前为止我发现了什么:

Here is what I am doing and what I've found so far:

1。当用户选择"复制"时,这就是代码所做的事情。 (旁注:是的,如果我只想要WPF控件图像,我会使用RenderTargetBitmap方法。我只想在这里保持简单。  在实际应用程序中,
那里是多个  WPF控件,位于Windows窗体控件上。)

1. When the user selects "Copy", this is what the code does. (Side note: Yes, if I only want the WPF control image, I'd use the RenderTargetBitmap method.  I am just trying to keep things simple here.  In the real application, there are multiple WPF controls sitting on the Windows Forms control.)

Bitmap bmp = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));                
Clipboard.SetDataObject(bmp);

2. 看起来DrawToBitmap方法使用本机SendMessage方法将控件打印到位图和BitBlt中。

2. It looks like the DrawToBitmap method uses the native SendMessage method to print the control into the bitmap and BitBlt.

3。在实际的应用程序中,我不能只使用BitBlt方法,因为我需要能够在控件隐藏在其他控件后面时获取图像。是的,我试过了。

3. In the real application, I can't use just BitBlt method because I need to be able to get the image while the control is hidden behind other controls. Yes, I tried it.

4。在Program.cs中,我尝试调用SetProcessDPIAware。没用。

4. In the Program.cs, I tried calling SetProcessDPIAware. Did not work.

5。在Manifest中,我尝试将dpiAware设置为True。没用。

5. In the Manifest, I tried setting the dpiAware to True. Did not Work.

6。调查控件尺寸和图像尺寸。 看起来Windows窗体控件,ElementHost和WPF控件在需要获取正确大小以获取图像时不同步。在示例程序中,Windows窗体 控件
已设置至500x500 @ 96 dpi,在我的显示器上,当程序以120 dpi运行时,控件显示为667x615。 它在屏幕上看起来很好,但是一旦我选择复制然后粘贴到Paint中。 图像为667x615,但有效的渲染区域在
533x492附近,即小于应有的80%。

6. Investigated control size and image size.  It looks like the Windows Forms control, ElementHost and the WPF control are not sync when it needs to get the correct size to grab the image. In a sample program, the Windows Forms  control is set to 500x500 @ 96 dpi and on my monitor, when the program run at 120 dpi the control is shown at 667x615.  It looks fine on the screen, but once I select Copy and then paste into Paint.  The image is 667x615 but the valid render area around 533x492, that is, 80% small than it should be.

问题是,有没有人遇到过这个问题?这是一个已知问题吗?

The question is, has anyone run into this issue? Is this a known issue?

谢谢。

推荐答案

如果您使用Windows窗体控件的DPI Aware设置,那将会很好用,但是因为WPF控件是
与分辨率无关,设置DPI意识 不起作用。 Windows窗体基于GDI +
渲染引擎,WPF基于DirectX渲染引擎。
Microsoft建议不要在Winform
应用程序中使用WPF控件,以避免
发​​生不可预测的错误

It would be work well if you used DPI Aware setting for Windows Form controls, but since WPF control is resolution-independent, set DPI aware will not play a role for it. Windows Forms is based on GDI+ rendering engine, WPF is based on DirectX rendering engine. Microsoft does not recommend using WPF controls in Winform application to avoid the unpredictable errors occur.

最好的问候,

鲍勃


这篇关于控件托管WPF控件上的DrawToBitmap问题,DPI大于100%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆