Silverlight打印抗锯齿

5
我正在尝试从Silverlight 4应用程序中打印一张图片(QR码),但是当打印时,图片会出现抗锯齿效果(我已经尝试过XPS文件打印机和硬件打印机),导致图像模糊,并且无法被条形码读取器读取。
以下是打印的XPS文档中的图像:http://img805.imageshack.us/img805/7677/qraliasing.png 我使用以下简单代码进行打印:
WriteableBitmap bitmap = new WriteableBitmap(width, height);
//write bitmap pixels
Image image = new Image(){Stretch = Stretch.None};
image.Source = bitmap;
image.Width = bitmap.PixelWidth;
image.Height = bitmap.PixelHeight;
//Print
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += (sender, args) =>
{
    args.PageVisual = image;
};
printDocument.Print("QrCode");
3个回答

2
我找到了一个解决方案。
在 Silverlight 4 中打印图像控件时,它会发送其源属性中设置的图像而不是像在 UserControl 中看起来那样的图像控件的“打印屏幕”。如果您生成两个位图分别为 100x100 像素和 1000x1000 像素,并将它们放入大小为 100x100 像素的 Image 控件中,则打印结果将不同于您所期望的。
因此,解决方案是生成高分辨率的图像(或放大图像)并将其放入所需尺寸的 Image 控件中。

0

看起来你在我输入的时候已经找到了解决方案,但我还是要提交一下...

这种情况发生的原因是PrintDocument本质上会获取UIElement(即你的图像),然后将其放大到适合打印的600 DPI,而通常它只能在96 DPI的屏幕上显示。由于无法告诉这个放大操作如何处理平滑度,所以你得到的结果就是那种丑陋的模糊效果。

然而,如果你自己进行放大操作,然后对图像应用相反的RenderTransform,当PrintDocument进行放大操作时,你的高分辨率放大图像就是结果。

一旦你获得了QR码的高分辨率放大图像(实际上是正常大小的6.25倍),你可以应用一个比例变换,将其缩小相同的比例:

image.RenderTransform = new ScaleTransform { 
    ScaleX = 96.0 / 600.0, 
    ScaleY = 96.0 / 600.0
};

当你打印这个时,应该能看到锐利的边缘。


-1
你尝试过在图形对象上更改平滑模式吗?
WriteableBitmap bitmap = new WriteableBitmap(width, height);
//write bitmap pixels
Image image = new Image(){Stretch = Stretch.None};
image.Source = bitmap;
image.Width = bitmap.PixelWidth;
image.Height = bitmap.PixelHeight;
//Print
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += (sender, args) =>
{
    //**Add this**
    args.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;

    args.PageVisual = image;
};
printDocument.Print("QrCode");

没有 args.Graphics 属性。System.Drawing.Drawing2D.SmoothingMode 在 Silverlight 中不可用。 - Alex Burtsev
也许在这里尝试使用“None”属性会更好?http://10rem.net/blog/2010/05/01/crappy-image-resizing-in-wpf-try-renderoptionsbitmapscalingmode 我不知道这是否相关,只是想提供帮助。 - Josh Maag
System.Drawing.Drawing2D.SmoothingMode在Silverlight中不可用。您提供的链接与WPF相关。 - Alex Burtsev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接