c# - How to apply Graphics scale and translate to the TextRenderer -



c# - How to apply Graphics scale and translate to the TextRenderer -

i'm using scaling , transforming graphics object when painting custom control, in order apply zooming , scrolling. utilize following:

matrix mx = new matrix(); mx.scale(mzoomfactor, mzoomfactor); mx.translate(-clip.x + mgraphicsoffsetx, -clip.y + mgraphicsoffsety); e.graphics.clip = new region(this.bounds); e.graphics.transform = mx;

then when paint strings using:

graphics g = ... g.drawstring(...)

the scalling , transforming correctly applied strings, zoomed out , in , on.

however if utilize next paint strings:

textrenderer.drawtext(...)

the text not correctly scaled , transformed.

do know how apply concepts textrenderer?

the comments above accurate--textrenderer.drawtext, beingness gdi, has limited back upwards coordinate transformation given resolution dependence. you've noticed, coordinate translation supported scaling not (and neither coordinate rotation).

the solution we've used (and resource i've been able find on internet) manually scale font , rectangle objects reflect scaling applied matrix.scale(float, float) in conjunction graphics.transform:

private font getscaledfont(graphics g, font f, float scale) { homecoming new font(f.fontfamily, f.sizeinpoints * scale, f.style, graphicsunit.point, f.gdicharset, f.gdiverticalfont); } private rectangle getscaledrect(graphics g, rectangle r, float scale) { homecoming new rectangle((int)math.ceiling(r.x * scale), (int)math.ceiling(r.y * scale), (int)math.ceiling(r.width * scale), (int)math.ceiling(r.height * scale)); }

here entire test form:

public partial class form1 : form { private picturebox box = new picturebox(); public form1() { initializecomponent(); this.load += new eventhandler(form1_load); } public void form1_load(object sender, eventargs e) { box.dock = dockstyle.fill; box.backcolor = color.white; box.paint += new painteventhandler(drawtest); this.controls.add(box); } public void drawtest(object sender, painteventargs e) { graphics g = e.graphics; string text = "test text"; float scale = 1.5f; float translate = 200f; var flags = textformatflags.preservegraphicsclipping | textformatflags.preservegraphicstranslatetransform; var mx = new matrix(); mx.scale(scale, scale); mx.translate(translate, translate); g.clip = new region(bounds); g.transform = mx; size rendererpsize = bounds.size; font f = getscaledfont(g, new font("arial", 12), scale); size rendererrsize = textrenderer.measuretext(g, text, f, rendererpsize, flags); rectangle rendererrect = new rectangle(0, 0, rendererrsize.width, rendererrsize.height); rectangle r = getscaledrect(g, rendererrect, scale); textrenderer.drawtext(g, text, f, realrect, color.black, flags); } private font getscaledfont(graphics g, font f, float scale) { homecoming new font(f.fontfamily, f.sizeinpoints * scale, f.style, graphicsunit.point, f.gdicharset, f.gdiverticalfont); } private rectangle getscaledrect(graphics g, rectangle r, float scale) { homecoming new rectangle((int)math.ceiling(r.x * scale), (int)math.ceiling(r.y * scale), (int)math.ceiling(r.width * scale), (int)math.ceiling(r.height * scale)); } }

c# .net winforms gdi+ paint

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -