c# - Write text on an image -
i developing image editor in have implemented rectangle, lines , ellipse drawing functionality using mouse event , using graphics.drawline()
, graphics.drawrectangle()
, graphics.drawellipse()
.
i searching writing text on image not find solution, mean whenever click on image @ location cursor change (like writing text in textbox) , can start typing on location.
the graphics.drawstring method similar looking not support dynamic typing
alex fr provided excellent set of drawing tools in drawtools article , these tools serve basis draw tool redux.
i use transparent textbox from: http://www.codeproject.com/articles/4390/alphablendtextbox-a-transparent-translucent-textbo
to add textbox control drawing tools need make class tooltext , drawtext.
in tooltext class, show form "textdialog" without border has textbox:
internal class tooltext : toolobject { public tooltext() { cursor = new cursor(gettype(), "rectangle.cur"); } public override void onmousedown(drawarea drawarea, mouseeventargs e) { point p = drawarea.backtrackmouse(new point(e.x, e.y)); textdialog td = new textdialog(); td.location = new point(e.x, e.y + drawarea.top + td.height); if (td.showdialog() == dialogresult.ok) { string t = td.thetext; color c = td.thecolor; font f = td.thefont; addnewobject(drawarea, new drawtext(p.x, p.y, t, f, c)); } }
base drawtext class off drawrectangle couple of properties, text, font, etc , drawing implementation:
public override void draw(graphics g) { pen pen = new pen(color); graphicspath gp = new graphicspath(); stringformat format = stringformat.genericdefault; gp.addstring(_thetext, _font.fontfamily, (int)_font.style, _font.sizeinpoints, new pointf(rectangle.x, rectangle.y), format); // rotate path it's center if necessary if (rotation != 0) { rectanglef pathbounds = gp.getbounds(); matrix m = new matrix(); m.rotateat(rotation, new pointf(pathbounds.left + (pathbounds.width / 2), pathbounds.top + (pathbounds.height / 2)), matrixorder.append); gp.transform(m); } g.drawpath(pen, gp); rectangle.size = g.measurestring(_thetext, _font).tosize(); pen.dispose(); }
Comments
Post a Comment