问题
I have a problem with WPF's anti aliasing of text. In my application the user designs text or image fields to be printed using a special printing device. I am using a canvas to host the fields, which are either textblocks or Images
The problem is when i convert this canvas into a RenderTargetBitmap, then into a black and white FormatConvertedBitmap, the text and images end up extremely fuzzy looking.
I have tried using "snaptodevicepixels = true" and "RenderOptions.EdgeMode" = Aliased on everything in the application. I know anti aliasing is great for the screen, but printing is a real disaster.
Here is an example of my code:
private BitmapSource GetCanvasAsBitmap(Canvas cvs)
{
cvs.Background = Brushes.White;
RenderOptions.SetEdgeMode(cvs, EdgeMode.Aliased);
cvs.SnapsToDevicePixels = true;
// render TopCanvas visual tree to the RenderTargetBitmap
Size CanvasSize = new Size(cvs.Width, cvs.Height);
cvs.Measure(CanvasSize);
Rect CanvasRect = new Rect(CanvasSize);
cvs.Arrange(CanvasRect);
RenderTargetBitmap targetBitmap =
new RenderTargetBitmap((int)cvs.ActualWidth,
(int)cvs.ActualHeight,
96, 96,
PixelFormats.Default);
targetBitmap.Render(cvs);
double scale = PIXELSCALE / cvs.Height;
ScaleTransform ST = new ScaleTransform(scale, scale);
TransformedBitmap TB = new TransformedBitmap(targetBitmap, ST);
return TB;
}
private static FormatConvertedBitmap Recolor(BitmapSource b)
{
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(b));
using (FileStream fs = new FileStream("BeforeRecolor.bmp", FileMode.Create))
{
encoder.Save(fs);
fs.Flush();
fs.Close();
}
FormatConvertedBitmap FCB = new FormatConvertedBitmap(b, PixelFormats.Indexed1, new BitmapPalette(new List<Color>() { Colors.Black, Colors.White }), 0);
BmpBitmapEncoder en = new BmpBitmapEncoder();
en.Frames.Add(BitmapFrame.Create(FCB));
using (FileStream fs = new FileStream("AfterRecolor.bmp", FileMode.Create))
{
en.Save(fs);
fs.Flush();
fs.Close();
}
return FCB;
}
how do i turn off the antiailasing before creating the rendertargetbitmap?
回答1:
http://blogs.msdn.com/b/dwayneneed/archive/2008/06/20/implementing-a-custom-bitmapsource.aspx
Apparently dither type is hardcoded
FormatConvertedBitmap
This class wraps the standard WIC pixel format converter (IWICImagingFactory::CreateFormatConverter). This component provides the means of converting from one pixel format to another, handling dithering and halftoning to indexed formats, palette translation and alpha thresholding. The Source, DestinationFormat, DestinationPalette, and AlphaThreshold properties are used to initialize the underlying component via IWICFormatConverter::Initialize. The dither type is hard-coded to be WICBitmapDitherTypeErrorDiffusion. The palette translation type is hard-coded to be WICBitmapPaletteTypeMedianCut. The ISupportInitialize interface is used to snap the values of the properties when initialization is complete. Further changes to the properties are ignored.
来源:https://stackoverflow.com/questions/6550731/turn-off-antialiasing-for-black-and-white-bitmap-rendered-from-canvas