c# - Unable to get correct Size of DrawnText using TextRenderer -


im drawing text using following code onto bitmap

graphicspath pth = new graphicspath(); var style = (int)myfont.style; pth.addstring(tcaption.text, myfont.fontfamily, style, myfont.size, point, stringformat.generictypographic); p = new pen(new solidbrush(bc), 2f); mygraphics.drawpath(p, pth); 

i'm using textrenderer measure size of string..

int  width = textrenderer.measuretext(tcaption.text, myfont).width; 

but not produce correct size of drawn string; there around 20-30% difference actual size of drawn string?

what im doing wrong? please advice.

update:

i want draw text , image onto bitmap,so inorder accommodate both i'm creating bitmap this

intwidth = textrenderer.measuretext(tcaption.text, cfont).width + image.width; intheight = textrenderer.measuretext(tcaption.text, cfont).height +image.height; tempimage= new bitmap(intwidth, intheight); 

then create graphics object bitmap this

 using (graphics newg = graphics.fromimage(tempimage)) 

@hans passant

i have tried graphics.measurestring alternative textrenderer

now set position of text , image-i need draw image @ top left corner ..

                imageposy = 0;                 imageposx = 10;                                 textposy = image.height;                                      textposx = 0; 

then draw text this

   po=new point(textposx, textposy);    newg.smoothingmode = smoothingmode.antialias;                                          graphicspath pth = new graphicspath();    var style = (int)myfont.style;    pth.addstring(tcaption.text, myfont.fontfamily, style, myfont.size, po,     stringformat.generictypographic);    newg.fillpath(new solidbrush(fc), pth); 

now draw image this

 rectangle nrect = new rectangle(imageposx, imageposy, image.width,   image.height);  objgraphics = graphics.fromimage(tempimage);  objgraphics.drawimage(image, nrect); 

as have seen need add offset 10 imageposition x coordinate correct measurement issue.

hope update throws more light question... im doing wrong? please advice..

instead of using textrenderer use graphicspath:

var path = new graphicspath(); path.addstring(text, font.fontfamily, (int)font.style, size, new point(0, 0), stringformat.generictypographic); var area = rectangle.round(path.getbounds()); 

here sample code generates image size of text:

private image drawtext(string text, font font, int size, color textcolor, color backcolor) {     var path = new graphicspath();     path.addstring(text, font.fontfamily, (int)font.style, size, new point(0, 0), stringformat.generictypographic);     var area = rectangle.round(path.getbounds());      rectangle br = rectangle.round(path.getbounds());     var img = new bitmap(br.width, br.height);      var drawing = graphics.fromimage(img);     drawing.textrenderinghint = system.drawing.text.textrenderinghint.antialiasgridfit;     drawing.smoothingmode = smoothingmode.highspeed;     drawing.clear(backcolor);      drawing.translatetransform((img.width - br.width) / 2 - br.x, (img.height - br.height) / 2 - br.y);     drawing.fillpath(brushes.black, path);     brush textbrush = new solidbrush(textcolor);      drawing.save();      textbrush.dispose();     drawing.dispose();      return img; } 

here sample results:

enter image description here enter image description here enter image description here


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -