.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

C# .net

Posted By: mithra     Posted Date: September 16, 2008    Points:2   Category :C#
how to print the result of a perticular page in c# .net window application....
Thanks.... G.mithra.

Author: Amit Mehra             
Posted Date: September 17, 2008     Points: 5   

Use this code to print in windows application

Include the following namespaces

using System.Drawing.Imaging;
using System.Drawing.Printing;

and import the following .dll for the necessary GDI functions


The following code is placed in the declaration section of the form. The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context.

private System.IO.Stream streamToPrint;

string streamType;

private static extern bool BitBlt


IntPtr hdcDest, // handle to destination DC

int nXDest, // x-coord of destination upper-left corner

int nYDest, // y-coord of destination upper-left corner

int nWidth, // width of destination rectangle

int nHeight, // height of destination rectangle

IntPtr hdcSrc, // handle to source DC

int nXSrc, // x-coordinate of source upper-left corner

int nYSrc, // y-coordinate of source upper-left corner

System.Int32 dwRop // raster operation code


Select the PrintPage event of the PrintDocument component and include the following code in the event

private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)


System.Drawing.Image image = System.Drawing.Image.FromStream

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;




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

Include the following code in the Print Click event handler

private void btnPrint_Click(object sender, EventArgs e)


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



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"))




And the StatrtPrint method to customize the PrintDialog and print the stored image

public void StartPrint(Stream streamToPrint, string streamType)


this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

this.streamToPrint = streamToPrint;

this.streamType = streamType;

System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();

PrintDialog1.AllowSomePages = true;

PrintDialog1.ShowHelp = true;

PrintDialog1.Document = printDoc;

DialogResult result = PrintDialog1.ShowDialog();

if (result == DialogResult.OK)





Please "Mark As Answer " if this post help you.

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend