C# - Winforms - Visual Studio 2012
The code below works perfect as long as the Windows Display is set to normal or 96 pixels.
But when I select 125% for the Display size or 120 pixels the text in the richtextbox bitmap gets shifted to the right and truncated.
Anyone know how to fix this?
Thank you.
private const double inch = 1440 / 96; // 14.4;
private Bitmap RtbToBitmap(RichTextBox rtb, Rectangle rectangle)
{
rtb.Update();
Bitmap bmp = new Bitmap(rectangle.Width, rectangle.Height);
using (Graphics gr = Graphics.FromImage(bmp))
{
IntPtr hDC = gr.GetHdc();
FORMATRANGE fmtRange;
RECT rect;
int fromAPI;
rect.Top = 0; rect.Left = 0;
rect.Right = (int)Math.Ceiling(rectangle.Right * inch);
rect.Bottom = (int)Math.Ceiling(rectangle.Bottom * inch);
fmtRange.chrg.cpMin = 0;
fmtRange.chrg.cpMax = -1;
fmtRange.hdc = hDC;
fmtRange.hdcTarget = hDC;
fmtRange.rc = rect;
fmtRange.rcPage = rect;
IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
Marshal.StructureToPtr(fmtRange, lParam, false);
fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE, 1, lParam);
Marshal.FreeCoTaskMem(lParam);
fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE, 1, new IntPtr(0));
gr.ReleaseHdc(hDC);
}
return (bmp);
}