-
I'm trying to render some text as completely black and white to png in low resolution (~200 dpi, actually pointing to 8 px per mm) Rendering the svg gives text with grayscale bordering: But I'm looking looking for the following result: I have tried several combiantions of graphics options (including set none) but I have not been able to achieve the desired result. Simplified sample code: public Bitmap Draw()
{
byte[] byteArray = Encoding.UTF8.GetBytes(SVG_CODE);
using MemoryStream stream = new MemoryStream(byteArray);
SvgDocument Doc = SvgDocument.Open<SvgDocument>(stream);
int w = 320;
int h = 1280;
Bitmap bmp = new Bitmap(w, h, PixelFormat.Format32bppArgb);
float density = (float)203.2;
bmp.SetResolution(density, density);
using Graphics g = Graphics.FromImage(bmp);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//Set some of the graphics properties so that the text renders nicely
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//Compositing Mode can't be set since string needs source over to be valid
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
//And an additional step to make sure text is proper anti-aliased and takes advantage
//of clear type as necessary
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
Doc.Draw(g);
return bmp;
}
const string SVG_CODE = @"<svg width=""320"" height=""1280"" position=""relative"" xml=""http://www.w3.org/XML/1998/namespace""
xmlns:xlink=""http://www.w3.org/1999/xlink""
xmlns:xml=""http://www.w3.org/XML/1998/namespace"" version=""1.1""
xmlns=""http://www.w3.org/2000/svg"">
<defs />
<g class=""svg-in-label-background"">
<rect x=""0"" y=""0"" width=""100%"" height=""100%"" fill-opacity=""0"" />
</g>
<g class=""svg-in-label-elements-container"">
<g transform=""matrix(1, 0, 0, 1, 0, 0)"" text-anchor=""start"" font-family=""'Arial Narrow'"" font-size=""16px"" font-style=""normal"" text-decoration=""none"" font-weight=""700"" data-id=""-317631"" wrap-mode=""WRAP_INCREASE_HORIZONTAL_SIZE"" gl22-rotation-degrees=""0"" class=""svg-model-element svg-model-element-text"" style=""fill:black;"">
<text x=""16"" y=""88"">
<tspan x=""16"" dy=""1.2em"">Produkt CLEMENTINEN</tspan>
<tspan x=""16"" dy=""1.2em"" visibility=""hidden"">HIDDEN</tspan>
<tspan x=""16"" dy=""1.2em"">Sorte:</tspan>
</text>
</g>
<g transform=""matrix(1, 0, 0, 1, 0, 0)"" text-anchor=""start"" font-family=""'Arial Narrow'"" font-size=""42px"" font-style=""normal"" text-decoration=""none"" font-weight=""700"" data-id=""-851205"" wrap-mode=""WRAP_INCREASE_HORIZONTAL_SIZE"" gl22-rotation-degrees=""0"" class=""svg-model-element svg-model-element-text"" style=""fill:black;"">
<text x=""64"" y=""104"">
<tspan x=""64"" dy=""1.2em"">CLEMENRUBI</tspan>
</text>
</g>
</g>
</svg>"; I'm trying to replicate some old Java code but instead of rendering each piece as that old code used to do, I'm using an Svg: //Java code
for (Part part : figures){
IFigure figure = part.getFigure();
GC gc = new GC(image);
gc.setAntialias(SWT.OFF);
gc.setTextAntialias(SWT.OFF);
// create the graphics to draw the figure.
final Graphics graphics = new SWTGraphics(gc);
graphics.translate(bounds.getLocation().negate());
// Paint the figure.
figure.paint(graphics);
graphics.dispose();
gc.dispose();
part.dispose();
} There, setting the antialias is trivial and works like a charm as the Desired image shows. Is there any way to achieve the desired result? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Why not try disabling anti-aging with SVG |
Beta Was this translation helpful? Give feedback.
-
I'm not sure what effect you expect, but I paste the code I tested. var d = new Bitmap(1446, 532);
{
using (var s = new Bitmap(320, 1280))
{
using (var g = SvgRenderer.FromImage(s))
{
((IGraphicsProvider)g).GetGraphics().TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
((IGraphicsProvider)g).GetGraphics().CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
svgDoc.Draw(g);
}
using (var g = Graphics.FromImage(d))
{
g.DrawImage(s, -20f, -700f, s.Width * 8f, s.Height * 8f);
}
}
} |
Beta Was this translation helpful? Give feedback.
I'm not sure what effect you expect, but I paste the code I tested.