Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6747999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:30:47+00:00 2026-05-26T12:30:47+00:00

I need to sraw a string to a bitmap with full justified. I know

  • 0

I need to sraw a string to a bitmap with full justified. I know that StringFormat.Alignment doesn’t support full justified alignment. So, I’m looking for a solution to draw a string on a bitmap with full-justify. RictTextBox has full justify but I think it uses WinAPI to justify the text. Maybe I can draw the text with RichTextBox but I don’t know how to get the controls bitmap(screenshot) without displaying in the form.
Is there any trick or an alternative 3rd party library to System.Drawing.Graphics?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:30:47+00:00Added an answer on May 26, 2026 at 12:30 pm

    I used a method to draw RichTextBox on a bitmap.

    public class ExtendedRichTextBox : RichTextBox
    {
        private const double inch = 1440 / 96;//Not 14.4!!, believe me you can see someone use 1.44 but it doesn't work on big bitmaps. They round the 1440/96 as 14.4 but it works on only small sized works. use /96
    
        public void DrawToBitmap(Graphics graphics, Rectangle bound)
        {
            Update();  // Ensure RTB fully 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. But you 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 is the example of using.

    var richtext = new ExtendedRichTextBox(); 
    /*I've implemented a RichTextBox but it isn't realted with this question. 
    You can use simply RichTextBox. ExtendedRichTextBox has support rtl.*/
    
    richtext.Font = font;
    richtext.ForeColor = textColor;
    richtext.Text = sometext;
    richtext.SelectAll();
    richtext.RightToLeft = rtl;
    richtext.SelectionAlignment = align;
    
    //Fix the 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 is where we want to 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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to know how to get YQL to return only data that's newer than
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
need a little help with this one. I have a form that I am
Need your help with my PHP/MYSQL array. I have a php script that selects
Need a little help with string formatting... I have a string like this: Bmw
Need to be able to set server(s) that replicate all information, as a master
Need to develop a .NET solution to graphically represent seats in sections, plotted in
Need to have a type-safe bag of items that all implement a generic interface.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.