简体   繁体   中英

Silverlight printing anti-aliasing

I'm trying to print an image (QR code) from Silverlight 4 app, however the image is antialised when printed (I have tried both XPS file printer and hardware printer)image is blury, and is not readable by barcode reader.

Image from printed XPS document http://img805.imageshack.us/img805/7677/qraliasing.png

I'm using this simple code to print it:

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");

I have found a solution.

When printing Image control in Silverlight 4, it sends to printer not a "print screen" of an image control like it looks in your UserControl but an image set in it's source property. If you generate two bitmaps of 100x100 px and 1000x1000px resolutions and put them in 100x100px size Image controls the print result will not be the same as you may expect.

So the solution is to generate high resolution image (or upscale image) and put it in Image controls of desired size.

It seems you've come across a solution as I was typing mine, but I'll submit anyway...

The reason this happens is that the PrintDocument will essentially take the UIElement (your image), which it normally blits to a 96 DPI screen, and upscale it to 600 DPI suitable for printing. Since there's no way to tell this upscale operation how to handle smoothing, what you get is that ugly blurriness.

However, if you do the upscale blit yourself, then apply an opposite RenderTransform to the image, when the PrintDocument goes to scale up the image, your high-res blit will be the result.

Once you have your high-res blit of the QR code (essentially 600 / 96 = 6.25 times as large as normal), you apply a scale transform that sizes it back down by the exact same amount:

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

When you print this, you should see sharp edges.

Did you try changing the smoothing mode on the graphics object?

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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