system.drawing - Drawing a string with full justified in .NET -
i need sraw string bitmap full justified. know stringformat.alignment doesn't support full justified alignment. so, i'm looking solution draw string on bitmap full-justify. ricttextbox has full justify think uses winapi justify text. maybe can draw text richtextbox don't know how controls bitmap(screenshot) without displaying in form. there trick or alternative 3rd party library system.drawing.graphics?
i used method draw richtextbox
on bitmap.
public class extendedrichtextbox : richtextbox { private const double inch = 1440 / 96;//not 14.4!!, believe me can see use 1.44 doesn't work on big bitmaps. round 1440/96 14.4 works on small sized works. use /96 public void drawtobitmap(graphics graphics, rectangle bound) { update(); // ensure rtb painted intptr hdc = graphics.gethdc(); formatrange fmtrange; rect rect; rect.left = (int)math.ceiling(bound.x * inch); rect.top = (int)math.ceiling(bound.y * inch); rect.right = (int)math.ceiling(bound.right * inch); rect.bottom = (int)math.ceiling(bound.bottom * inch); int fromapi; fmtrange.hdc = hdc; fmtrange.hdctarget = hdc; fmtrange.chrg.cpmin = 0; fmtrange.chrg.cpmax = -1; fmtrange.rc = rect; fmtrange.rcpage = rect; intptr lparam = marshal.alloccotaskmem(marshal.sizeof(fmtrange)); marshal.structuretoptr(fmtrange, lparam, false); fromapi = sendmessage(handle, em_formatrange, 0, lparam); fromapi = sendmessage(handle, em_formatrange, 1, lparam); marshal.freecotaskmem(lparam); fromapi = sendmessage(handle, em_formatrange, 0, new intptr(0)); graphics.releasehdc(hdc); } }
you can find winapi implementations on pinvoke website. can take here too:
[dllimport("user32.dll")] private static extern int32 sendmessage(intptr hwnd, int msg, int wparam, intptr lparam); private const int wm_user = 0x400; private const int em_formatrange = wm_user + 57; [structlayout(layoutkind.sequential)] private struct rect { public int left; public int top; public int right; public int bottom; } [structlayout(layoutkind.sequential)] private struct charrange { public int cpmin; public int cpmax; } [structlayout(layoutkind.sequential)] private struct formatrange { public intptr hdc; public intptr hdctarget; public rect rc; public rect rcpage; public charrange chrg; }
and here example of using.
var richtext = new extendedrichtextbox(); /*i've implemented richtextbox isn't realted question. can use richtextbox. extendedrichtextbox has support rtl.*/ richtext.font = font; richtext.forecolor = textcolor; richtext.text = sometext; richtext.selectall(); richtext.righttoleft = rtl; richtext.selectionalignment = align; //fix rtl bug in richtextbox if (rtl == righttoleft.yes) { if (align == textalign.center) richtext.rtf = richtext.rtf.replace(@"\qr", @"\qc"); else if (align == textalign.left) richtext.rtf = richtext.rtf.replace(@"\qr", @"\ql"); else if (align == textalign.justify) richtext.rtf = richtext.rtf.replace(@"\qr", @"\qj"); } //textrect want put text in. var tempbitmap = new bitmap(textrect.width, textrect.height); richtext.drawtobitmap(graphics.fromimage(tempbitmap), temprect); tempbitmap.maketransparent(richtext.backcolor); graph.drawimage(tempbitmap, panelrect.x, panelrect.y);
Comments
Post a Comment