我使用以下方法绘制矩形:
<Rectangle Width="300" Height="100" Stroke="Blue" StrokeThickness="6"> </Rectangle>
Run Code Online (Sandbox Code Playgroud)
但是应用了抗锯齿功能.有没有办法把它关掉?我希望它清晰明了.
我正在寻找一种方法来阻止WPF模糊我的图像.我希望我的应用程序在高DPI显示器上看起来很好,所以我创建了96x96px的图标以显示大小为32x32.
google中有很多内容可供查找,甚至在stackoverflow上也有一些关于此的主题.总结他们说:启用属性UseLayoutRounding并设置RenderOptions.BitmapScalingMode为HighQuality.不幸的是,这对我来说并不适用 - 图像看起来仍然非常模糊.然后我在具有更高DPI aaaand-WOW的笔记本电脑上测试了我的应用程序.图像非常清晰.
我唯一的解决方案是创建一个自定义控件,将ImageSource转换为System.Drawing.Bitmap,在那里重新调整它以匹配图像尺寸并将其转换回WPF ImageSource.显然不是完美的解决方案!

所以问题是:为什么在低dpi显示屏上看我的图像模糊?我不知道是什么导致了这一点,希望somone有一个想法来解决这个问题.
这里有一些我发现的最有用的资源:
编辑:
这里要求的是转换ImageSource的代码.有一些方法调用没有包括在内,但你可以通过谷歌快速找到它们.
// Calculate the conversion factor.
float dpi = WpfImageHelper.GetCurrentDPI(this);
float factor = dpi / 96f;
int width = (int)Math.Round(this.image.ActualWidth * factor);
int height = (int)Math.Round(this.image.ActualHeight * factor);
// Create bitmaps.
Bitmap oldBitmap = WpfImageHelper.ToWinFormsBitmap2((BitmapSource)this.Source);
Bitmap newBitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Draw the new bitmap. Use high-quality interpolation mode.
using (Graphics g = Graphics.FromImage(newBitmap))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic; …Run Code Online (Sandbox Code Playgroud) 我正在基于MS RibbonControlsLibrary(3.5.41019.1)以编程方式填充功能区(WPF ).所有图像(最明显的大图像)看起来非常糟糕和"像素":

我试图将BitmapScalingMode设置为" HighQuality "而没有任何影响,源代码:
BitmapImage img = new BitmapImage();
try
{
Uri uri = new Uri("pack://application:,,,/UIMainWindow;component/Resources/" + iPictureName);
img.BeginInit();
img.SetValue(BitmapImage.CacheOptionProperty, BitmapCacheOption.OnLoad);
RenderOptions.SetBitmapScalingMode(img, BitmapScalingMode.HighQuality);
img.UriSource = uri;
img.EndInit();
img.Freeze();
}
catch (Exception ex)
{
throw new Exception("Creation of image failed: " + ex.Message, ex);
}
Run Code Online (Sandbox Code Playgroud)
问题 为什么图像的缩放看起来很糟糕?我该如何解决这个问题?
当我画这样的东西时(这里只是随机图纸):
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DrawingVisual visual = new DrawingVisual();
DrawingContext context = visual.RenderOpen();
Pen pen = new Pen(Brushes.Black, 1);
context.DrawEllipse(Brushes.YellowGreen, pen, new Point(0,0), 40, 40);
for (int i = 0; i <= 100 - 1; i++)
context.DrawLine(new Pen(Brushes.Black, 1), new Point(0, i), new Point(i, i));
context.Close();
RenderTargetBitmap bmp = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32);
bmp.Render(visual);
image1.Source = bmp;
}
}
Run Code Online (Sandbox Code Playgroud)
DrawLine和DrawEllipse混合的颜色.(我发现只有DrawLine才使用笔,而不是像Rectangle和Ellipse这样使用Brush的其他形式).奇怪的是,即使是基础网格背景(argh)的LinearGradientBrush的颜色. 我希望它们是z-Ordered,每个都具有完全不透明度.
这里是XAML代码:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"> …Run Code Online (Sandbox Code Playgroud)