MeasureString和DrawString的区别为什么我要将MeasureString()结果宽度增加21%size.Width=size.Width*1.21f;转义DrawString()自动换行?我需要一个解决方案来获得准确的结果。两个函数中使用相同的字体、相同的字符串格式、相同的文本。OP的回答:SizeFsize=graphics.MeasureString(element.Currency,Currencyfont,newPointF(0,0),strFormatLeft);尺寸.宽度=尺寸.宽度*1.21f;intfreespace=rect.Width-(int)size.Width;如果(自由空间0)ImageSize=自由空间;否则ImageSize=0;}intFlagY=y+(CurrencySize-ImageSize)/2;intFlagX=(freespace-ImageSize)/2;graphics.DrawImage(GetResourseImage(@"Flags."+element.Flag.ToUpper()+".png"),newRectangle(FlagX,FlagY,ImageSize,ImageSize));graphics.DrawString(element.Currency,Currencyfont,Brushes.Black,newRectangle(FlagX+ImageSize,rect.Y,(int)(size.Width),CurrencySize),strFormatLeft);我的代码。MeasureString()方法存在一些问题,尤其是在绘制非ASCII字符时。请尝试TextRenderer.MeasureText()。Graphics.MeasureString、TextRenderer.MeasureText和Graphics.MeasureCharacterRanges都返回一个大小,其中包括字形周围的空白像素以容纳上升和下降。换句话说,它们为“a”返回与“d”(升序)或“y”(降序)相同的高度。如果您需要字形的真实大小,唯一的方法是绘制字串并计算图像:PublicSharedFunctionMeasureStringSize(ByValgraphicsAsGraphics,ByValtextAsString,ByValfontAsFont)AsSizeF'GetinitialestimatewithMeasureTextDimflagsAsTextFormatFlags=TextFormatFlags.Left+TextFormatFlags.NoClippingDimproposedSizeAsSize=NewSize(Integer.MaxValue,Integer.MaxValue)DimsizeAsSize=TextRenderer.MeasureText(graphics,text,font,proposedSize,flags)'创建位图暗淡图像AsNewBitmap(size.Width,size.Height)image.SetResolution(graphics.DpiX,graphics.DpiY)DimstrFormatAsNewStringFormatstrFormat.Alignment=StringAlignment.NearstrFormat.LineAlignment=StringAlignment.Near'绘制实际文本Dimg作为Graphics=graphics.FromImage(image)g.SmoothingMode=SmoothingMode.HighQualityg.TextRenderingHint=Drawing.Text.TextRenderingHint.AntiAliasGridFitg.Clear(Color.White)g.DrawString(text,font,Brushes.Black,NewPointF(0,0),strFormat)'找到true字形的边界DimxsAsInteger=0DimxfAsInteger=size.Width-1DimysAsInteger=0DimyfAsInteger=size.Height-1'FindleftmarginDoWhilexsColor.White.ToArgbThenExitDoEndIfNextxs+=1Loop'FindrightmarginDoWhilexf>xsForyAsInteger=ysToyfIfimage.GetPixel(xf,y).ToArgbColor.White.ToArgb然后退出DoEndIfNextxf-=1Loop'找到上边距DoWhileysColor.White.ToArgbThenExitDoEndIfNextys+=1Loop'找到下边距DoWhileyf>ysForxAsInteger=xsToxfIfimage.GetPixel(x,yf).ToArgbColor.White.ToArgbThenExitDoEndIfNextyf-=1LoopReturnNewSizeF(xf-xs+1,yf-ys+1)EndFunction如果对任何人有帮助,我会回答从smirkingman转换而来到C#,固定内存错误(使用-Dispose)和外循环中断(无TODO)我还使用缩放图形(和字体)所以我也添加了它(否则它不起作用)。它返回RectangleF,因为我想精确定位文本(使用Graphics.DrawText)。不完美但足够我的目标代码:staticclassStringMeasurer{privatestaticSizeFGetScaleTransform(Matrixm){/*3x3矩阵,仿射变换(倾斜-旋转使用)[Xscale,Yskew,0][Xskew,Yscale,0][Xtranslate,Ytranslate,1]indices(0,...):Xscale,Yskew,Yskew,Xscale,Xtranslate,Ytranslate*/returnnewSizeF(m.Elements[}0],m.元素[3]);}publicstaticRectangleFMeasureString(Graphicsgraphics,Fontf,strings){//只复制比例,不旋转或变换varscale=GetScaleTransform(graphics.Transform);//使用MeasureText获取初始估计值//TextFormatFlagsflags=TextFormatFlags.Left|TextFormatFlags.NoClipping;//大小proposedSize=newSize(int.MaxValue,int.MaxValue);//大小size=TextRenderer.MeasureText(graphics,s,f,proposedSize,flags);SizeFsizef=graphics.MeasureString(s,f);sizef.Width*=scale.Width;sizef.Height*=scale.Height;大小size=sizef.ToSize();intx左=0;intxRight=大小.宽度-1;intyTop=0;intyBottom=size.Height-1;//使用(Bitmapimage=newBitmap(size.Width,size.Height))创建位图{image.SetResolution(graphics.DpiX,graphics.DpiY);StringFormatstrFormat=newStringFormat();strFormat.Alignment=StringAlignment.Near;strFormat.LineAlignment=StringAlignment.Near;//使用(Graphicsg=Graphics.FromImage(image)){g.SmoothingMode=graphics.SmoothingMode;g.TextRenderingHint=graphics.TextRenderingHint;g.Clear(Color.White);g.ScaleTransform(scale.Width,scale.Height);g.DrawString(s,f,Brushes.Black,newPointF(0,0),strFormat);}//Findthetrueboundariesoftheglyph//Findleftmarginfor(;xLeftxLeft;xRight--)for(inty=yTop;yyTop;yBottom--)for(intx=xLeft;x这篇关于codeproject提供了两种方式来获取DrawString渲染的字符的准确大小您可能需要添加以下StringFormat标志:StringFormatFlags.FitBlackBox试试这个解决方案:http://www.codeproject.com/Articles/2118/Bypass-Graphics-MeasureString-limitation(位于https://stackoverflow.com/a/11708952/908936)代码:以上就是C#学习教程的全部内容:MeasureString和DrawString的区别,如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注---staticpublicintMeasureDisplayStringWidth(Graphicsgraphics,stringtext,Fontfont){System.Drawing.StringFormatformat=newSystem.Drawing.StringFormat();System.Drawing.RectangleFrect=newSystem.Drawing.RectangleF(0,0,1000,1000);varranges=newSystem.Drawing.CharacterRange(0,text.Length);System.Drawing.Region[]区域=newSystem.Drawing.Region[1];format.SetMeasurableCharacterRanges(new[]{范围});regions=graphics.MeasureCharacterRanges(text,font,rect,format);rect=regions[0].GetBounds(graphics);返回(int)(rect.Right+1.0f);}本文收集自网络,不代表立场。侵权请点击右侧联系管理员删除。如需转载请注明出处:
