Link to home
Start Free TrialLog in
Avatar of MyersA
MyersA

asked on

Printing a paper form (in BMP) from windows application. The Windows application plugs in values...

I have a BMP image (the form is also in PDF) that contains a scanned copy of a paper form that we need to fill out. Is it possible to use this image in my application so that the application can print out the form and the data that the user entered? For example, the user loads the application and a normal Windows Form asks for the user's name. When the user enters the name
and clicks on the "Print" button, I'd like to print the form in the jpg
image and include the person's name in that form (in the appropriate
position).
I wanted to use a Windows Form (whose background would be the image), add a
textbox, and then print the Windows Form (with the txtbox that includes the
user's name), but the Windows Form's size is limited to the screen
resolution.
Someone suggested using PrintDocument but from what I understand, that'll only print the document. I still need to insert some values at run-time before printing it.
Any suggestions are appreciated.
Thanks.
Avatar of GrumbleBot
GrumbleBot

Here is an example from MSDN.

Hope it Helps.

Good Luck

Example
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
   Graphics mygraphics = this.CreateGraphics();
   Size s = this.Size;
   memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
   Graphics memoryGraphics = Graphics.FromImage(memoryImage);
   IntPtr dc1 = mygraphics.GetHdc();
   IntPtr dc2 = memoryGraphics.GetHdc();
   BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
   mygraphics.ReleaseHdc(dc1);
   memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
   CaptureScreen();
   printDocument1.Print();
}
Compiling the Code
This example requires:

A PrintDocument component named printDocument1 with a PrintPage event handler.
A Button named printButton with a Click event handler.
The example code replaces the existing event handlers. The form is printed when printButton is clicked.

Robust Programming
The following conditions may cause an exception:

You do not have permission to access the printer.
You do not have permission to use unmanaged code.
There is no printer installed.
Security
In order to run this example, you must have permission to execute unmanaged code and to access the printer.

Avatar of MyersA

ASKER

Thanks for the info.
It didn't work quite as expected. First, it prints out the Windows taskbar. Also, Form class limits the size of the form so it's not larger than the screen size, so I won't be ablt to create a form that's as big as the bmp I'll be importing. And I don't think I can plug in the values during runtime.

ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial