简体   繁体   English

当 Windows 缩放比例设置为大于 100%,例如 125% 时,绘图图像变得模糊并且图像变大并被截断

[英]DrawingImage is getting fuzzy and image gets bigger and cut-off when Windows scaling is set to more than 100%, for example 125%

I have an WPF usercontrol which is used in a winforms applications.我有一个用于 winforms 应用程序的 WPF 用户控件。 WPF usercontrol is contained within an ElementHost container. WPF 用户控件包含在 ElementHost 容器中。

This WPF usercontrol has some images, button with images, labels, etc.这个 WPF 用户控件有一些图像、带图像的按钮、标签等。

I have a dictionary with some geometries, the following is one of them.我有一本包含一些几何图形的字典,以下是其中之一。

  <Geometry x:Key="hlpGeometry">F0 M22,22z M0,0z M11,22C17.0751,22 22,17.0751 22,11 22,4.92487 17.0751,0 11,0 4.92487,0 0,4.92487 0,11 0,17.0751 4.92487,22 11,22z M12.1901,16.6889L10.2662,16.6889 10.2662,18.6998 12.1901,18.6998 12.1901,16.6889z M8.18758,5.59005C7.40125,6.43439,7.00808,7.55265,7.00808,8.94484L8.72898,8.94484C8.76121,8.10695 8.89334,7.46564 9.12537,7.02091 9.53787,6.2217 10.2823,5.82209 11.3587,5.82209 12.2288,5.82209 12.8508,6.05412 13.2246,6.51818 13.6049,6.98224 13.795,7.53009 13.795,8.16173 13.795,8.61291 13.6661,9.04797 13.4083,9.46691 13.2665,9.70539 13.0796,9.9342 12.8475,10.1533L12.0741,10.9171C11.3329,11.6454 10.8527,12.2932 10.6336,12.8604 10.4144,13.4211 10.3049,14.1623 10.3049,15.084L12.0258,15.084C12.0258,14.2719 12.116,13.6596 12.2965,13.2471 12.4834,12.8281 12.8862,12.319 13.505,11.7195 14.3557,10.8945 14.9197,10.2694 15.1969,9.84396 15.4804,9.41857 15.6222,8.86427 15.6222,8.18107 15.6222,7.05314 15.2387,6.12824 14.4718,5.40636 13.7112,4.67804 12.6961,4.31388 11.4263,4.31388 10.0535,4.31388 8.9739,4.73927 8.18758,5.59005z</Geometry>
  <DrawingGroup x:Key="hlpDrawingGroup" ClipGeometry="M0,0 V22 H22 V0 H0 Z">
    <GeometryDrawing Brush="#FF00AA2B" Geometry="{StaticResource hlpGeometry}" />
  </DrawingGroup>
  <DrawingImage x:Key="ico_helpDrawingImage" Drawing="{StaticResource hlpDrawingGroup}" />

I have an WPF Image and I bound above DrawingImage to it using the Source attribute.我有一个 WPF 图像,我使用 Source 属性将 DrawingImage 绑定到它上面。 I bind the source attribute to a property in the view model.我将源属性绑定到视图 model 中的一个属性。

Something like below:像下面这样的东西:

<Image x:Name="MyImage"
       Height="24"
       Width="24"
       VerticalAlignment="Center"
       Source="{Binding Path=MyIcon}"/>

It is working fine when windows is scaled to 100% but when scaled to a higher one, let's say, 125%, then the image gets fuzzy.当 windows 缩放到 100% 时,它工作正常,但当缩放到更高的缩放比例时,比如 125%,图像就会变得模糊。

Also the image look like gets bigger than 24x24 and it is being cut-off when I set a scale greater than 100% (125%).此外,图像看起来比 24x24 大,当我将比例设置为大于 100% (125%) 时,它被截断了。

How can I make image to not get fuzzy and set image to always be the same size 24x24?如何使图像不模糊并将图像设置为始终为 24x24 的相同大小?

You can use the UseLayoutRounding and SnapsToDevicePixels properties.您可以使用 UseLayoutRounding 和 SnapsToDevicePixels 属性。

Set the UseLayoutRounding property to true to prevent blurring caused by anti-aliasing and to tell the layout system to align elements with pixel boundaries.将 UseLayoutRounding 属性设置为 true 以防止由抗锯齿引起的模糊并告诉布局系统将元素与像素边界对齐。

RenderOptions.BitmapScalingMode is mainly for larger images to be displayed more smoothly, you can choose HighQuality. RenderOptions.BitmapScalingMode主要是为了让大图显示更流畅,可以选择HighQuality。

Use high quality bitmap scaling, which is slower than LowQuality mode, but produces higher quality output.使用高质量 bitmap 缩放,比 LowQuality 模式慢,但产生更高质量的 output。

You can check this link:您可以查看此链接:

https://learn.microsoft.com/en-us/do.net/api/system.windows.media.bitmapscalingmode?redirectedfrom=MSDN&view=windowsdesktop-7.0 https://learn.microsoft.com/en-us/do.net/api/system.windows.media.bitmapscalingmode?redirectedfrom=MSDN&view=windowsdesktop-7.0

Code:代码:

<Image x:Name="img"  MouseWheel="img_MouseWheel"
   Height="24"
   Width="24"
   UseLayoutRounding="True" 
   SnapsToDevicePixels="True" 
   VerticalAlignment="Center"
   RenderOptions.BitmapScalingMode="HighQuality"
   Source="2.jpg"/>

With images I have always found the below to always clear up the fuzzy stuff.对于图像,我总是发现下面的图像总是可以清除模糊的东西。

<Image x:Name="MyImage"
    Height="24"
    Width="24"
    RenderOptions.BitmapScalingMode="HighQuality"
    VerticalAlignment="Center"
    Source="{Binding Path=MyIcon}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM