rtf to image

Hi, i need to transform the contents of a richtextbox (rtf) to an image.

Any suggestions will be highly appreciated

Thank you




Answer this question

rtf to image

  • Darren Woodford

    Hello ogg,

    Thank you very much,

    i am developing powerpoint like application , as each slide has to be preview as a thumbnalil in a listview

    i tried with DrawBitmap but it is not working for richtextbox and any com components

    as i have to place media clips and rchtext on a slide i am unable to work with this method

    your code helped me a lot ,

    again thank you very much


  • roger bruhin

    Yes, a more elegant solution like just calling the base OnPaint or OnPrint (like most other controls) would be a better solution. But, RichTextBox.OnPaint and RichTextBox.OnPrint don't work like other controls... making you resort to PInvoke.

    Thanks for posting your code.



  • MuthuAnnamalai

    Hi, thank you for answering!

    DrowToBitmap works fine but it gets only the visible part of the rtf text, i managed to get there wIth

    Size s1 = richTextBox.Size;

    memoryGraphics.CopyFromScreen(richTextBox.Location.X, richTextBox.Location.Y, 0, 0, s);

    but i need all the text, including the scrolled text



  • ASKavya

    Peter Ritchie wrote:
    You could try raising the OnPrint event, passing a Graphics object that is associated with an image. Something like (aircode, not compiled or tested)

    Bitmap bitmap = new Bitmap(100, 100); // change size to suite needs
    using(Graphics graphics = Graphics.FromImage(bitmap))
    {
    PaintEventArgs e = new (graphics, new Rectangle(0,0, bitmap.Width, bitmap.Height));
    OnPaint(e);
    // Do something with bitmap here...
    }

    Let us know if that works for you.
    Sorry, the call to OnPaint should be associated with your RichTextBox object. Plus, you'll have to create a new class deriving from RichTextBox control to get access to RichTextBox.OnPaint.

  • lpd0084

    hi,

    if you just need to make an image from text, maybe this works for you....

    http://www.thecodebehind.com/code/dotnet/aspnet/creating-an-image-from-text-with-c-and.aspx

    cheers,
    b.

  • Ian.H

    How about RichTextBox.DrawToBitmap()

  • Raymond R

    Hi,

    precisely, i need an image of a rtf text - wiht colors, different fonts, sizes, alignement ...

    it would be great if someone already did something like that because there's many issues to consider on this problem and my time is limited



  • Rajeshpta

    Hi

    i found another solution (there are some bugs to fix ...)

    as u said i create a class deriving from RichTextBox control and then i use the Print method ...

    There's the code :

    public class RichTextBoxPrintCtrl : RichTextBox

    {

    //Convert the unit used by the .NET framework (1/100 inch)

    //and the unit used by Win32 API calls (twips 1/1440 inch)

    private const double anInch = 14.4;

    [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; //First character of range (0 for start of doc)

    public int cpMax; //Last character of range (-1 for end of doc)

    }

    [StructLayout(LayoutKind.Sequential)]

    private struct FORMATRANGE

    {

    public IntPtr hdc; //Actual DC to draw on

    public IntPtr hdcTarget; //Target DC for determining text formatting

    public RECT rc; //Region of the DC to draw to (in twips)

    public RECT rcPage; //Region of the whole DC (page size) (in twips)

    public CHARRANGE chrg; //Range of text to draw (see earlier declaration)

    }

    private const int WM_USER = 0x0400;

    private const int EM_FORMATRANGE = WM_USER + 57;

    [DllImport("USER32.dll")]

    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

    // Render the contents of the RichTextBox for printing

    // Return the last character printed + 1 (printing start from this point for next page)

    public int Print(int charFrom, int charTo, Graphics gr, Rectangle bounds)

    {

    //Calculate the area to render and print

    RECT rectToPrint;

    rectToPrint.Top = 0;// (int)(bounds.Top * anInch);

    rectToPrint.Bottom = (int)(bounds.Height * anInch);// (int)(bounds.Bottom * anInch);

    rectToPrint.Left = 0;// (int)(bounds.Left * anInch);

    rectToPrint.Right = (int)(bounds.Width * anInch);// (int)(bounds.Right * anInch);

    //Calculate the size of the page

    RECT rectPage;

    rectPage.Top = 0;//(int)(bounds.Top * anInch);

    rectPage.Bottom = (int)(gr.ClipBounds.Height* anInch);//(int)(bounds.Bottom * anInch);

    rectPage.Left = 0;//(int)(bounds.Left * anInch);

    rectPage.Right = (int)(gr.ClipBounds.Right * anInch);//(int)(bounds.Right * anInch);

    IntPtr hdc = gr.GetHdc();

    FORMATRANGE fmtRange;

    fmtRange.chrg.cpMax = charTo; //Indicate character from to character to

    fmtRange.chrg.cpMin = charFrom;

    fmtRange.hdc = hdc; //Use the same DC for measuring and rendering

    fmtRange.hdcTarget = hdc; //Point at printer hDC

    fmtRange.rc = rectToPrint; //Indicate the area on page to print

    fmtRange.rcPage = rectPage; //Indicate size of page

    IntPtr res = IntPtr.Zero;

    IntPtr wparam = IntPtr.Zero;

    wparam = new IntPtr(1);

    //Get the pointer to the FORMATRANGE structure in memory

    IntPtr lparam = IntPtr.Zero;

    lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));

    Marshal.StructureToPtr(fmtRange, lparam, false);

    //Send the rendered data for printing

    res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);

    //Free the block of memory allocated

    Marshal.FreeCoTaskMem(lparam);

    //Release the device context handle obtained by a previous call

    gr.ReleaseHdc(hdc);

    //Return last + 1 character printer

    return res.ToInt32();

    }

    }

    Then i'm using the Print method like this :

    Image image = new Bitmap(richTextBoxPrint.ClientSize.Width, richTextBoxPrint.PreferredSize.Height);

    Graphics g = Graphics.FromImage(image);

    richTextBoxPrint.Print(0,richTextBoxPrintObsPreli.Text.Length,g,new Rectangle(richTextBoxPrintObsPreli.Location,new Size( richTextBoxPrint.ClientSize.Width+30,richTextBoxPrint.PreferredSize.Height+10)));

    image.Save("anImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

    There should be a better and more "elegant" solution but this worked for me:)



  • Mike McGavin

    DrawToBitmap actually makes a copy of the screen image; much like a screen capture. So, yes, it will only get what's visible.

    You could try raising the OnPrint event, passing a Graphics object that is associated with an image. Something like (aircode, not compiled or tested)


    Bitmap bitmap = new Bitmap(100, 100); // change size to suite needs
    using(Graphics graphics = Graphics.FromImage(bitmap))
    {
    PaintEventArgs e = new (graphics, new Rectangle(0,0, bitmap.Width, bitmap.Height));
    OnPaint(e);
    // Do something with bitmap here...
    }

     
    Let us know if that works for you.



  • medicineworker

    Would it be possible to do this in VB.NET. I know VB.net is very similar to C#.net, but I'm having trouble with some of the data definition syntax. A version of this code in VB.NET would be very helpful to me.

    Thanks

    Marshall



  • rtf to image