MrPicing

Hello Every body!

I copied the code from post http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1606666&SiteID=1

to save rtb as bmp. But This code is not being compiled. Message appears that FormateRange is missing reference.

Please tell me what to do for this error.

// Convenient overload

public static Bitmap RtbToBitmap(Control rtb)

{

return RtbToBitmap(rtb,

rtb.ClientRectangle.Width,

rtb.ClientRectangle.Height);

}

// Capture

public Bitmap RtbToBitmap(Control rtb, int width, int height)

{

Bitmap bmp = new Bitmap(width, height);

using (Graphics gr = Graphics.FromImage(bmp))

{

IntPtr hDC = gr.GetHdc();

FORMATRANGE fmtRange;

RECT rect;

int fromAPI;

rect.Top = 0; rect.Left = 0;

rect.Bottom = (int)(bmp.Height

+ (bmp.Height * (bmp.HorizontalResolution / 100))

* inch);

rect.Right = (int)(bmp.Width

+ (bmp.Width * (bmp.VerticalResolution / 100))

* inch);

fmtRange.chrg.cpMin = 0;

fmtRange.chrg.cpMax = -1;

fmtRange.hdc = hDC;

fmtRange.hdcTarget = hDC;

fmtRange.rc = rect;

fmtRange.rcPage = rect;

int wParam = 1;

IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));

Marshal.StructureToPtr(fmtRange, lParam, false);

fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE, wParam, lParam);

Marshal.FreeCoTaskMem(lParam);

fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE,

wParam, new IntPtr(0));

gr.ReleaseHdc(hDC);

}

return bmp;

}




Re: Windows Forms General Error saving RTB as bmp.

Karthikeya Pavan Kumar .B

Have you added the Import User32.dll code and checked for appropriate references.






Re: Windows Forms General Error saving RTB as bmp.

MrPicing

Thanks 4 ur reply.

I tried to import but following errors appeard.

Error 1 The DllImport attribute must be specified on a method marked 'static' and 'extern'
F:\CSharpWork\MyApp3\frmMai
Error 2 An object reference is required for the nonstatic field, method, or property
'MyApp.frmMain.RtbToBitmap(System.Windows.Forms.Control, int, int)'
F:\CSharpWork\MyApp3\frmMain.cs 358 22 MyApp
Error 3 The type or namespace name 'FORMATRANGE' could not be found
(are you missing a using directive or an assembly reference )
F:\CSharpWork\MyApp3\frmMain.cs 369 17 MyApp
Error 4 The type or namespace name 'RECT' could not be found (are you missing a using
directive or an assembly reference )
F:\CSharpWork\MyApp3\frmMain.cs 370 17 MyApp

and many errors about the object used in orignal code.

Thanks once again.




Re: Windows Forms General Error saving RTB as bmp.

Karthikeya Pavan Kumar .B

I hope the some part of the code you are writing and stated in the link is missing try writing this code first

Code Snippet

using System;

using System.ComponentModel;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace WindowsApplication1 {

public partial class Form1 : Form {

[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;

}

private const double inch = 14.4;

private Rectangle contentRectangle;

public Form1() {

InitializeComponent();

}

private void SaveAsJpegButton_Click(object sender, EventArgs e) {

RtbToBitmap(richTextBox1, contentRectangle, @"c:\rtb.jpg");

}

private void RtbToBitmap(RichTextBox rtb, Rectangle rectangle, string fileName) {

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.Bottom = (int)(bmp.Height + (bmp.Height * (bmp.HorizontalResolution/100)) * inch);

rect.Right = (int)(bmp.Width + (bmp.Width * (bmp.VerticalResolution / 100)) * inch);

fmtRange.chrg.cpMin = 0;

fmtRange.chrg.cpMax = -1;

fmtRange.hdc = hDC;

fmtRange.hdcTarget = hDC;

fmtRange.rc = rect;

fmtRange.rcPage = rect;

int wParam = 1;

IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));

Marshal.StructureToPtr(fmtRange, lParam, false);

fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE, wParam, lParam);

Marshal.FreeCoTaskMem(lParam);

fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE, wParam, new IntPtr(0));

gr.ReleaseHdc(hDC);

}

bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);

bmp.Dispose();

}

private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e) {

contentRectangle = e.NewRectangle;

}

}

}