Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Render a Visual aliased to a bitmap

$
0
0

I need to render some Grid Visual to a bitmap to use it in documents like MS Word. The bitmap should be copied via the clipboard. I've found the RenderTargetBitmap class to do that and the rendering and copying works basically.

To improve the bitmap quality, I'd like to use a higher resolution. 4 times the 96 dpi seems to be good. But then, to decrease the file size, I'd like to disable anti-aliasing. It just should not be necessary at high resolution and it really harms the quality on printing, blurring everything with grey-scale rasterisation in the printer.

But that seems impossible. Whatever I try, the text and border lines are always anti-aliased in the bitmap. Some code:

// Initialisation
Size size = ReportGrid.RenderSize;
TextElement.SetFontFamily(ReportGrid, new FontFamily(Reporting.PrintFontFamily));
int scale = 1;
var rtb = new RenderTargetBitmap(
	(int) (ReportGrid.ActualWidth * scale),
	(int) (ReportGrid.ActualHeight * scale),
	96 * scale,
	96 * scale,
	PixelFormats.Pbgra32);

// Trying to disable anti-aliasing but does not work
TextOptions.SetTextRenderingMode(rtb, TextRenderingMode.Aliased);
RenderOptions.SetEdgeMode(rtb, EdgeMode.Aliased);

// First add a white background
Rectangle back = new Rectangle();
back.Fill = Brushes.White;
back.Measure(size);
back.Arrange(new Rect(size));
rtb.Render(back);

// Add the Grid
ReportGrid.Measure(size);
ReportGrid.Arrange(new Rect(size));
rtb.Render(ReportGrid);

// Copy to clipboard
Clipboard.SetImage(rtb);

Any idea how the aggressive anti-aliasing of text (from TextBlock) and lines (from Border) can be turned off so that I have a chance to get really sharp images printed?

Using vector graphics doesn't seem to be an option because MS Word only understands WMF/EMF but WPF chose not to support it for its security issues, with no replacement. So exporting to a bitmap seems the only option.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>