locked
Capturing Graphics From RichTextBox RRS feed

  • Question

  •  

    Ok. I have posted a simliar post about this topic, and I'm not trying to violate any rules of this forum, or post a duplicate thread. But what I would like to know is how to capture the text from a richtextbox, and write it to a Bitmap, colors and all, without using the Graphics.CopyFromScreen. Is this possible.
    Saturday, August 25, 2007 9:23 PM

Answers

  • Hi Demitrius,

     

    As I understand from your description, you would like to create a Bitmap from the text in the RichTextBox. If I have misunderstood you, please correct me. You can try to refer to the following sample codes:

    1.Get the Text from the RichTextBox:

     string rtbTxt = rtb.Text;

     

    2.Draw a string bitmap(some sample codes as follows):

            private void DrawImage(string filePath, string content,float xPosition, float yPosition)
            {
                System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(filePath);
                sourceImage.RotateFlip(RotateFlipType.Rotate90FlipXY);
                Bitmap newImage = new Bitmap(sourceImage, sourceImage.Width, sourceImage.Height);
                Graphics gh = Graphics.FromImage(newImage);
                gh.SmoothingMode = SmoothingMode.HighQuality;
                gh.DrawImage(newImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel);
                Font newFont = null;
                SizeF fontSize = new SizeF();
                newFont = new Font(FontFamily.GenericSerif, 22, FontStyle.Bold);
                fontSize = gh.MeasureString(content, newFont);
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                SolidBrush brush = new SolidBrush(Color.Red);
                gh.DrawString(content, newFont, brush, new PointF(xPosition, yPosition), format);
                newImage.Save(@"C:\1.bmp", ImageFormat.Jpeg);
                gh.Dispose();
                sourceImage.Dispose();
            }

     

    You can call this method just as DrawImage(@"d:\1.bmp", rtbTxt, 15.0, 20.0).

     

    Hope this helps,

     

    Citizens on the earth

     

    Tuesday, August 28, 2007 8:41 AM
  • Check out the OnPaint() code in this thread.  Instead of using "e.Graphics", use a Graphics instance that you created from a Bitmap with Graphics.FromImage().  Adjust the fmtRange values so it fits the bitmap.
    Tuesday, August 28, 2007 2:06 PM

All replies

  • Hi Demitrius,

     

    As I understand from your description, you would like to create a Bitmap from the text in the RichTextBox. If I have misunderstood you, please correct me. You can try to refer to the following sample codes:

    1.Get the Text from the RichTextBox:

     string rtbTxt = rtb.Text;

     

    2.Draw a string bitmap(some sample codes as follows):

            private void DrawImage(string filePath, string content,float xPosition, float yPosition)
            {
                System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(filePath);
                sourceImage.RotateFlip(RotateFlipType.Rotate90FlipXY);
                Bitmap newImage = new Bitmap(sourceImage, sourceImage.Width, sourceImage.Height);
                Graphics gh = Graphics.FromImage(newImage);
                gh.SmoothingMode = SmoothingMode.HighQuality;
                gh.DrawImage(newImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel);
                Font newFont = null;
                SizeF fontSize = new SizeF();
                newFont = new Font(FontFamily.GenericSerif, 22, FontStyle.Bold);
                fontSize = gh.MeasureString(content, newFont);
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                SolidBrush brush = new SolidBrush(Color.Red);
                gh.DrawString(content, newFont, brush, new PointF(xPosition, yPosition), format);
                newImage.Save(@"C:\1.bmp", ImageFormat.Jpeg);
                gh.Dispose();
                sourceImage.Dispose();
            }

     

    You can call this method just as DrawImage(@"d:\1.bmp", rtbTxt, 15.0, 20.0).

     

    Hope this helps,

     

    Citizens on the earth

     

    Tuesday, August 28, 2007 8:41 AM
  • Check out the OnPaint() code in this thread.  Instead of using "e.Graphics", use a Graphics instance that you created from a Bitmap with Graphics.FromImage().  Adjust the fmtRange values so it fits the bitmap.
    Tuesday, August 28, 2007 2:06 PM
  • Ok. Something like that I already know how to do. What you left how is the part on how to get the color as well. I'm talking about if a richTextBox says "Hello World" and each letter being different colors. How could I accomplish this in a simple piece of code rather than a looping process.

    Thursday, August 30, 2007 10:45 PM
  • I'm pretty sure EM_FORMATRANGE renders colors properly too.  No need for a loop either.
    Thursday, August 30, 2007 10:50 PM
  • A code sample would be a great. I'm not asking you to send me a whole program, just and idea please.

    Sunday, September 2, 2007 10:08 PM