C# 系统。具有大偏移矩阵的图纸质量问题
我遇到了一个问题,当图形矩阵具有较大偏移值时,图形文本和基本图形绘制操作没有正确的位置和质量。我尝试了数字平滑模式、插值模式和文本渲染提示选项,但没有成功C# 系统。具有大偏移矩阵的图纸质量问题,c#,.net,system.drawing,drawing2d,C#,.net,System.drawing,Drawing2d,我遇到了一个问题,当图形矩阵具有较大偏移值时,图形文本和基本图形绘制操作没有正确的位置和质量。我尝试了数字平滑模式、插值模式和文本渲染提示选项,但没有成功 using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; public void RenderImageClosePointDrawing() { float x = 68336, y = 99460;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public void RenderImageClosePointDrawing()
{
float x = 68336, y = 99460;
PointF anchorPoint = new PointF(17494176, 25461836);
PointF anchorPoint2= new PointF(17494076, 25461836);
string textLabel = "9318";
float textFontSize = 20;
float symbolsize = 34;
string fontFamly = "Arial";
Bitmap bitmap = new Bitmap(256, 256);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
graphics.Transform = new Matrix(1, 0, 0, 1, -x * 256, -y * 256);
//Draw the circle
Pen polyPen = new Pen(new SolidBrush(Color.Black), 2);
Brush polyBrush = new SolidBrush(Color.Teal);
graphics.DrawEllipse(polyPen, anchorPoint.X, anchorPoint.Y, symbolsize, symbolsize);
graphics.FillEllipse(polyBrush, anchorPoint.X, anchorPoint.Y, symbolsize, symbolsize);
RectangleF drawnArea = new RectangleF(anchorPoint.X, anchorPoint.Y, symbolsize, symbolsize);
Pen polyPen2 = new Pen(new SolidBrush(Color.Black), 1);
Brush polyBrush2 = new SolidBrush(Color.Teal);
graphics.DrawEllipse(polyPen2, anchorPoint2.X, anchorPoint2.Y, symbolsize, symbolsize);
graphics.FillEllipse(polyBrush2, anchorPoint2.X, anchorPoint2.Y, symbolsize, symbolsize);
RectangleF drawnArea2 = new RectangleF(anchorPoint2.X, anchorPoint2.Y, symbolsize, symbolsize);
Pen polyPen3 = new Pen(new SolidBrush(Color.Red), 1);
graphics.DrawRectangle(polyPen3, drawnArea.X, drawnArea.Y, drawnArea.Width, drawnArea.Height);
graphics.DrawRectangle(polyPen3, drawnArea2.X, drawnArea2.Y, drawnArea2.Width, drawnArea2.Height);
//Draw the text
Pen textOutlinePen = new Pen(new SolidBrush(Color.Orange), (float)4);
textOutlinePen.EndCap = LineCap.Round;
textOutlinePen.LineJoin = LineJoin.Round;
textOutlinePen.MiterLimit = 0;
Brush textFillBrush = new SolidBrush(Color.Teal);
FontFamily textFontFamily = new FontFamily(fontFamly);
PointF textAnchor = new PointF(anchorPoint.X, anchorPoint.Y);
ShiftTextAnchor_NW(textLabel, textFontSize, ref drawnArea, textFontFamily, ref textAnchor);
var textPath = new GraphicsPath();
textPath.AddString(textLabel,
textFontFamily,
(int)FontStyle.Bold,
textFontSize,
textAnchor,
new StringFormat()
);
graphics.DrawPath(textOutlinePen, textPath);
graphics.FillPath(textFillBrush, textPath);
//Draw the text2
Pen textOutlinePen2 = new Pen(new SolidBrush(Color.Orange), (float)1);
textOutlinePen.EndCap = LineCap.Round;
textOutlinePen.LineJoin = LineJoin.Round;
textOutlinePen.MiterLimit = 0;
PointF textAnchor2 = new PointF(anchorPoint2.X, anchorPoint2.Y);
ShiftTextAnchor_NW(textLabel, textFontSize, ref drawnArea2, textFontFamily, ref textAnchor2);
var textPath2 = new GraphicsPath();
textPath2.AddString(textLabel,
textFontFamily,
(int)FontStyle.Bold,
textFontSize,
textAnchor2,
new StringFormat()
);
graphics.DrawPath(textOutlinePen2, textPath2);
graphics.FillPath(textFillBrush, textPath2);
}
bitmap.Save(@"C:\ClosePointDrawing.png", ImageFormat.Png);
}
private static void ShiftTextAnchor_NW(string textLabel, float textFontSize, ref RectangleF drawnArea, FontFamily textFontFamily, ref PointF textAnchor)
{
GraphicsPath tempPath = new GraphicsPath();
tempPath.AddString(
textLabel,
textFontFamily,
(int)FontStyle.Bold,
textFontSize,
textAnchor,
new StringFormat()
);
var textBounds = tempPath.GetBounds();
var offsetX = textBounds.X - textAnchor.X;
var offsetY = textBounds.Y - textAnchor.Y;
textAnchor = new PointF(drawnArea.Left - (textBounds.Width + offsetX), drawnArea.Top - (textBounds.Height + offsetY));
}
运行此代码时,您将获得以下输出:
您会注意到文本看起来不好看(有些失真),而且当使用1像素笔划时,圆圈周围的黑色笔划轮廓仅与其青色填充的圆圈正确对齐,而圆圈位于左侧。右边的一个使用2像素的笔划。您还将看到,红色的正方形没有与青色圆圈对齐,它们应该完全封装它
现在,如果更改代码中的前几个值,使其不使用大偏移量,如下所示:
float x=0,y=0;
PointF anchorPoint=新的PointF(150,50);
PointF anchorPoint2=新的PointF(50,50)代码>
您将获得以下输出:
请注意,文本看起来更好,笔划与填充的圆圈以及红色正方形完美对齐
有什么可以做的,以便它能用更大的矩阵正确地渲染它吗?我想知道GDI+是否能处理如此大的值,并且它产生了任何结果。你为什么要做这样的事情,并在256x256位图上进行处理???以便在最近的缩放级别处理谷歌地图平铺坐标。因此,处理这个问题的唯一方法是自己进行偏移调整,而不是使用转换矩阵?GDI+使用32位浮点值进行内部数学运算。与C#程序中的float相同。精度是相当有限的,当你做任何数学运算时,最多6个有效数字。请注意,17494176已经远远超出了这一范围,舍入错误会让事情变得不正常。您必须编写更智能的代码。