Share via


How to print windows form in C# application

Question

Monday, July 13, 2009 10:05 AM | 1 vote

Dear All,
 I have application,that having one windwos form.on form some images are drawn, I want to print that form on one button click event.
Please help me that code writing.without crystal report and print preview option.

I want to without PrintScreen, windows form printing...

Thanks,

Rajesh.

All replies (13)

Wednesday, July 15, 2009 10:41 AM ✅Answered | 4 votes

You are a little lazy, man :)

You just have to print the resulting image.

Like this:

protected void btnPrint_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(PrintImage);
    pd.Print();      
}

void PrintImage(object o, PrintPageEventArgs e)
{
    int x = SystemInformation.WorkingArea.X;
    int y = SystemInformation.WorkingArea.Y;
    int width = this.Width;
    int height = this.Height; 

    Rectangle bounds = new Rectangle(x, y, width, height); 

    Bitmap img = new Bitmap(width, height); 

    this.DrawToBitmap(img, bounds);
    Point p = new Point(100, 100);
    e.Graphics.DrawImage(img, p);    
 }

Best regards, Sergiu


Monday, July 13, 2009 10:36 AM | 1 vote

            Bitmap bitmap = new Bitmap(this.Width, this.Height);
            this.DrawToBitmap(bitmap, this.ClientRectangle);
            bitmap.Save("myPrintScreen.bmp");

this code works on a Windows.Forms-Best regards, Sergiu


Monday, July 13, 2009 1:33 PM | 2 votes

Hi - I think my information can help.

Well, Sergiu is almost correct, his code will capture the Windows Form application UI.
It will also save it.


Figure A: Using the Sergiu's code example.

==========================================================
try
{
      int x = SystemInformation.WorkingArea.X;
      int y = SystemInformation.WorkingArea.Y;

      int width = this.Width;
      int height = this.Height;

 

      Rectangle bounds = new Rectangle(x, y, width, height);

 

      Bitmap img = new Bitmap(width, height);

 

      this.DrawToBitmap(img, bounds);       string date = DateTime.Now.ToString("Mdyyyy");       img.Save(date + ".bmp"); } catch (Exception ex) {     MessageBox.Show(ex.Message.ToString()); }

Here are the results using my above code example:


Figure B: It will capture the entire form, all form bounds.

I hope this information was helpful....

Have a nice day...

Best regards,
Fisnik


Coder24.com


Tuesday, July 14, 2009 6:09 AM

But how can i print that img, on button click event without showing user


Tuesday, July 14, 2009 7:13 AM

Hi again:

It will never be shown to the user.

What do you mean by that?

I mean the file is getting saved in a directory.

Have a nice day...

Best regards,
FisnikCoder24.com


Tuesday, July 14, 2009 7:18 AM

Hi
  I have form that having print button, if user click button that time print command will fire.
so please give me that code.

thanks
Rajesh


Tuesday, July 14, 2009 7:20 AM

Check out my replay, the earlier one.

That one with screenshots.

Have a nice day...

Best regards,
FisnikCoder24.com


Tuesday, July 14, 2009 7:40 AM | 1 vote

Hi,

this is the code-snippet:

try
{
      int x = SystemInformation.WorkingArea.X;
      int y = SystemInformation.WorkingArea.Y;
       int width = this.Width;
      int height = this.Height; 

     Rectangle bounds = new Rectangle(x, y, width, height); 

     Bitmap img = new Bitmap(width, height); 

       this.DrawToBitmap(img, bounds);       string date = DateTime.Now.ToString("Mdyyyy");       img.Save(date + ".bmp"); } catch (Exception ex) {     MessageBox.Show(ex.Message.ToString()); }

Have a nice day...

Best regards,
Fisnik

Coder24.com


Wednesday, September 1, 2010 12:42 PM

Hello Sir,

I try this, showing me this error, it might be some another problem but if you can help....

Error 1 Unable to copy file "obj\Debug\AdityaConsultants.exe" to "bin\Debug\AdityaConsultants.exe". The process cannot access the file 'bin\Debug\AdityaConsultants.exe' because it is being used by another process. WindowsApplication1

Thanks

Anas


Thursday, September 9, 2010 10:08 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Printing;
using System.Runtime.InteropServices;

namespace fro
{
public partial class print : Form
{
private System.IO.Stream streamToPrint;
string streamType;
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt
(
IntPtr hdcDest,
int nXDest,
int nYDest,
int nWidth,
int nHeights,
IntPtr hdcSrc,
int nXSrc,
int nYSrc,
System.Int32 dwRop
);

public print()
{
InitializeComponent();
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}

private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = image.Width;
int height = image.Height;
if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
{
width = e.MarginBounds.Width;
height = image.Height * e.MarginBounds.Width / image.Width;
}
else
{
height = e.MarginBounds.Height;
width = image.Width * e.MarginBounds.Height / image.Height;
}
System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
}

public void StartPrint(Stream streamToPrint, string streamType)
{
this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
this.streamToPrint = streamToPrint;
this.streamType = streamType;
System.Windows.Forms.PrintDialog printDialog1 = new PrintDialog();
printDialog1.AllowSomePages = true;
printDialog1.ShowHelp = true;
printDialog1.Document = printDocument1;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();

}

}

private void print_Load(object sender, EventArgs e)
{
this.Load += new System.EventHandler(this.print_Load);
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("errr");
Graphics g1 = this.CreateGraphics();
Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
MyImage.Save(@"C:\PrintPage.jpg", ImageFormat.Jpeg);
FileStream filestream = new FileStream(@"C:\PrintPage.jpg", FileMode.Open, FileAccess.Read);
StartPrint(filestream, "Image");
if (System.IO.File.Exists(@"c:\PrintPage.jpg"))
{
System.IO.File.Delete(@"C:\PrintPage.jpg");
}
else
MessageBox.Show("errr");
}

private void InitializeComponent()
{
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.SuspendLayout();
// //
// // Form1
// //
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "print";
this.Load += new System.EventHandler(this.print_Load);
this.ResumeLayout(false);

}

}

}


Monday, September 26, 2011 5:43 PM

Your code function, but you have open this file with an other application. Close this application and try again. That will be good

Clémox


Wednesday, January 4, 2012 10:50 AM

Hi Fisnik,

Just wondering where it is the image saves itself?

 

Kind Regards

Sam Mines

Sam Mines


Saturday, October 13, 2012 12:09 PM

can u rotate the image in landscape mode while printing

because my image size is 950*665