none
Problem with symbols when drawing Hebrew text by Graphics.DrawString function from the RichTextBox control

    Question

  • Hi all,

    I have a question:
    I'm trying to draw RightToLeft (Hebrew) text on the Image object using "Graphics.DrawString" function.
    Text is previously loaded from the RTF file into RichTextBox control, and in this control it seems correct.
    But after drawing on the image symbols like "(" , ":"  and "." displayed on the incorrect positions when they are last or first in the text line.
    In other words mixing Hebrew with Simbols (English) does not work correct.

    Do you have any idea?

    Thanks a lot!
    Timur.
    • Changed type Jing0Moderator Friday, September 04, 2009 3:08 AM
    • Changed type winrazor Monday, September 14, 2009 5:21 PM See my answer
    Tuesday, August 25, 2009 7:35 AM

All replies

  • "Incorrect positions" is too fuzzy.  I'd recommend you post a screenshot to an image sharing service as well as a code snippet that shows how you used the text in the RTB in your DrawString() call.

    Hans Passant.
    Tuesday, August 25, 2009 6:50 PM
    Moderator

  • Simplest example:

    Input text:
    המון זמן.

    Output text:
    .המון זמן

    Dot was rotated.

    Using DrawString:

    newStringFormat.Trimming = StringTrimming.Word;
    newStringFormat.Alignment = StringAlignment.Far; //R2L
    layoutSize = new SizeF(pictureBox1.Width - (leftMargin + rightMargin), pictureBox1.Height - (topMargin + bottomMargin));
    newStringFormat.FormatFlags = StringFormatFlags.LineLimit;

    stringSize = objGraphics.MeasureString(tmpStr, objFont, layoutSize, newStringFormat, out charactersFitted, out linesFilled);


    objGraphics.DrawString(tmpStr.Substring(0, charactersFitted), objFont, new SolidBrush(fgColor),
     new RectangleF(new PointF(leftMargin, topMargin),
     new SizeF(pictureBox1.Width - (leftMargin + rightMargin), pictureBox1.Height - (topMargin + bottomMargin))), newStringFormat);

    Thank you,
    Timur.

    Wednesday, August 26, 2009 5:35 AM
  • Hi,

     

    I test your code on my side. It works fine.

    string tmpStr = "המון זמן.";

     

            private void button1_Click(object sender, EventArgs e)

            {

                StringFormat newStringFormat = new StringFormat();

                SizeF layoutSize;

     

                Graphics objGraphics = pictureBox1.CreateGraphics();

                SizeF stringSize;

     

                Font objFont = textBox2.Font;

     

                int leftMargin = 5;

                int rightMargin = 5;

                int topMargin = 5;

                int bottomMargin = 5;

     

                int charactersFitted = 5;

                int linesFilled = 5;

                Color fgColor = Color.Red;

     

                newStringFormat.Trimming = StringTrimming.Word;

                newStringFormat.Alignment = StringAlignment.Far; //R2L

     

                layoutSize = new SizeF(pictureBox1.Width - (leftMargin + rightMargin), pictureBox1.Height - (topMargin + bottomMargin));

     

                newStringFormat.FormatFlags = StringFormatFlags.LineLimit;

     

                stringSize = objGraphics.MeasureString(tmpStr, objFont, layoutSize, newStringFormat, out charactersFitted, out linesFilled);

     

                objGraphics.DrawString(tmpStr.Substring(0, charactersFitted), objFont, new SolidBrush(fgColor),

                 new RectangleF(new PointF(leftMargin, topMargin),

                 new SizeF(pictureBox1.Width - (leftMargin + rightMargin), pictureBox1.Height - (topMargin + bottomMargin))), newStringFormat);

            }

     

    And the result:

    I think I may miss something. Could you please provide a demo for us to test? Upload your demo project to some server and offer us the URL. Skydrive (http://skydrive.live.com/ ) may be a good option. You can sign up 25GB free space only with your Windows Live Passport.

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, August 31, 2009 10:45 AM
    Moderator
  • We are changing the issue type to “General Discussion” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Friday, September 04, 2009 3:07 AM
    Moderator
  • Dear Friend,

    Hebrew is Right to Left language, it means that dot should be from the left side. (in the end of the frase)

    Initial string also has dot from the right - it's another bug of this online tool.

    Dots braces and other symbols are placed incorrectly by MS new functions.
    Finally I've fixed the problem by using old functions, which have awfull preformance with long texts, this problem is also fixed by manual separating pages with additional optimization.

    In other words:  MS should fix 2 bugs:
    1) In the new function "MeasureString" - RTL is not supported as needed!!! (symbols mixed with Hebrew text have incorrect positions)
    2) Bug in this online tool - typing and copy\paste have the same problem with symbols as 1) .


    I did serious investigation in the net, and a lot of people have the same problem and suggest to use old interface,
    it also suggested in the MS example. I think that a nice way or fix this bug or notice about this problem in the help of the new functions.

    And please don't say that you don't have enough info - you can see the problem in your answer.

    Anyway, thank you for your answer! And please let me know when 1) is fixed. (if it's possible)
    Timur.
    Monday, September 14, 2009 5:19 PM