WPF在Windows Server 2003上调整抗锯齿工件的大小

本文关键字:Windows Server 2003 调整 WPF | 更新日期: 2023-09-27 17:51:24

我正在使用。net 4中的以下WPF代码调整控制台应用程序中的PNG的大小:

const int width = 250;
const int height = 179;
DrawingGroup group = new DrawingGroup();
RenderOptions.SetBitmapScalingMode(group, BitmapScalingMode.Fant);
group.Children.Add(new ImageDrawing(source, new Rect(0, 0, width, height)));
DrawingVisual targetVisual = new DrawingVisual();
using (DrawingContext targetContext = targetVisual.RenderOpen())
{
    targetContext.DrawDrawing(group);
    targetContext.Close();
    RenderTargetBitmap target = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
    target.Render(targetVisual);
    BitmapSource resized = BitmapFrame.Create(target);
}

在Windows 7上运行时,这完全符合预期,但在Windows Server 2003上运行时,我得到了我认为是反锯齿的伪影

这张图片显示了,当显示在一个白色背景的网页上时,Server 2003的图片在白色区域有额外的水平和垂直的灰线,这些灰线本不应该在那里。

放大一个小部分,试着弄清楚发生了什么,这张图片显示了原始PNG在透明部分(显示为灰色和白色方格)和白色区域之间有一条1像素的半透明线。两个调整大小的图像都有相同的半透明线,但Server 2003下的图像在透明度级别上有一些奇怪的变化。

有一个很好的MSDN文档,关于在WPF中调整图像大小时抗锯齿的影响(http://msdn.microsoft.com/en-us/library/aa970908.aspx),似乎与我在这里看到的症状相匹配,但为什么在Windows 7和Server 2003之间会有不同的工作方式??

我试图找到方法来切换反混叠测试这个理论使用以下代码,但它没有任何区别:

RenderOptions.SetEdgeMode(group, EdgeMode.Aliased);

WPF在Windows Server 2003上调整抗锯齿工件的大小

由于驱动程序支持问题,AA在XP和Win2003中被关闭。这个论坛的帖子指出了一些变通方法/reg hack。

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c16c611e-f7b6-45c2-9f89-b55514817b58/1773092

经过一周的搜索,我找到了一个解决方案,但我不太确定为什么它解决了这个问题。

我注意到正在处理的图像以Bgr32的PixelFormat开始,但RenderTargetBitmap的输出是Pbra32。阅读这个格式的P部分与渲染图像时如何计算Alpha阈值有关,因此我认为在这个阶段可能会出现问题。

解决方案是首先将PixelFormat转换为Pbra32,使用下面的代码行,然后执行resize

source = new FormatConvertedBitmap(source, PixelFormats.Pbgra32, source.Palette, 0);

据我所知,WPF使用DirectX。这意味着输出可能在很大程度上取决于显卡。试着改变一下宽度和高度,如下所示:

new Rect(0, 0, width - 0.00001d, height - 0.00001d)

如果没有帮助,请尝试在Win Server 2003机器上更新视频驱动程序和DirectX。