Search from Cycling Blog
Showing posts with label Printing. Show all posts
Showing posts with label Printing. Show all posts

Printing the Picture in C#.NET

Printing the Picture in C#.NET

Language: C#.NET

Author: Aamir Mustafa

E-Mail: aamir_mustafa2007@msn.com

aamirmustafa134@gmail.com

Introduction:

I was looking different methods how to print a picture, so I have one way. This time I m going to tell you in C#.NET

This article will show simple method to print the picture

How to Print:

Create an empty project

On the form, add a menu bar and change its name to File

Below File menu, add these menus

mnuOpen

mnuPageSetup

mnuPrint

So we have following shape

Fileà Open, Page Setup, Print

After adding the menus

Go to Toolbox and go to Printing section and add following controls

PageSetupDialog1

We are using for setting page set up

PrintDocument1

We are using for printing document

PrintPreviewDialog1

We are using for take preview of our print

After adding all these controls, add a picture box to the form

Now we are going to add code

First of all add following code to Open menu

We will use it for opening a picture from disk

Code:

private void mnuOpen_Click(object sender, EventArgs e)

{

Bitmap m_bitmap;

OpenFileDialog openFileDlg=new OpenFileDialog();

openFileDlg.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";

if (openFileDlg.ShowDialog() == DialogResult.OK)

{

string strFileName = openFileDlg.FileName;

m_bitmap = new Bitmap(strFileName);

this.pictureBox1.Image = m_bitmap;

}

}

After adding above code , add following code in Page setup menu

We are using it for setting page setup

Code:

private void mnupageSetup_Click(object sender, EventArgs e)

{

pageSetupDialog1.Document = printDocument1;

pageSetupDialog1.ShowDialog();

}

Now we will add code in Print event click

Code:

private void mnuprint_Click(object sender, EventArgs e)

{

printPreviewDialog1.Document=printDocument1;

printPreviewDialog1.ShowDialog();

}

At the end we will add code behind PrintPage event of PrintDocument1

So double click on it on the form or access its PageEvent in code editor and add following code

Code:

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

{

e.Graphics.DrawImage(this.pictureBox1.Image, 0, 0, this.pictureBox1.Width, this.pictureBox1.Height);

}

In the above code we are using DrawImage method of Graphics

DrawImage has five arguments

First argument which we pass is image, so here we are passing here our picture

Next argument is the x location of image and next location is y

Third is width of image, we are setting our picture width and so next is height of our image.

0 comments  

Printing the Picture in VB.NET

TitlePrint a form's image in VB.NET
DescriptionThis example shows how to print a form's image in VB.NET. It makes a Bitmap, uses BitBlt to copy the form's image into it, and prints the result.
Keywordsprint screen, screen capture, form image, print form image
CategoriesGraphics, VB.NET

When the user clicks the Print button, the program calls the GetFormImage subroutine to make a Bitmap holding an image of the form's contents. It saves the result in a global variable. It then creates a PrintDocument object and calls its Print method to print the result.

Subroutine GetFormImage gets a Graphics object for the form. It makes a Bitmap big enough to hold the image and gets a Graphics object for it. It then gets the device context handles (hDC) for the two Graphics objects and uses BitBlt to copy the form's image into the Bitmap.

The PrintDocument's Print method uses the DrawImage method to draw the form's picture centered on the printout.


Private Declare Auto Function BitBlt Lib "gdi32.dll" (ByVal _
hdcDest As IntPtr, ByVal nXDest As Integer, ByVal _
nYDest As Integer, ByVal nWidth As Integer, ByVal _
nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc _
As Integer, ByVal nYSrc As Integer, ByVal dwRop As _
System.Int32) As Boolean
Private Const SRCCOPY As Integer = &HCC0020

' Variables used to print.
Private m_PrintBitmap As Bitmap
Private WithEvents m_PrintDocument As PrintDocument

' Print the picture.
Private Sub btnPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnPrint.Click
' Copy the form's image into a bitmap.
m_PrintBitmap = GetFormImage()

' Make a PrintDocument and print.
m_PrintDocument = New PrintDocument
m_PrintDocument.Print()
End Sub

Private Function GetFormImage() As Bitmap
' Get this form's Graphics object.
Dim me_gr As Graphics = Me.CreateGraphics

' Make a Bitmap to hold the image.
Dim bm As New Bitmap(Me.ClientSize.Width, _
Me.ClientSize.Height, me_gr)
Dim bm_gr As Graphics = me_gr.FromImage(bm)
Dim bm_hdc As IntPtr = bm_gr.GetHdc

' Get the form's hDC. We must do this after
' creating the new Bitmap, which uses me_gr.
Dim me_hdc As IntPtr = me_gr.GetHdc

' BitBlt the form's image onto the Bitmap.
BitBlt(bm_hdc, 0, 0, Me.ClientSize.Width, _
Me.ClientSize.Height, _
me_hdc, 0, 0, SRCCOPY)
me_gr.ReleaseHdc(me_hdc)
bm_gr.ReleaseHdc(bm_hdc)

' Return the result.
Return bm
End Function

' Print the form image.
Private Sub m_PrintDocument_PrintPage(ByVal sender As _
Object, ByVal e As _
System.Drawing.Printing.PrintPageEventArgs) Handles _
m_PrintDocument.PrintPage
' Draw the image centered.
Dim x As Integer = e.MarginBounds.X + _
(e.MarginBounds.Width - m_PrintBitmap.Width) \ 2
Dim y As Integer = e.MarginBounds.Y + _
(e.MarginBounds.Height - m_PrintBitmap.Height) \ 2
e.Graphics.DrawImage(m_PrintBitmap, x, y)

' There's only one page.
e.HasMorePages = False
End Sub
download source code here

0 comments  

Printing the Picture

Printing the Picture

Language: VB.NET

Author: Aamir Mustafa

E-Mail: aamir_mustafa2007@msn.com

aamirmustafa134@gmail.com

Introduction:

I was looking different methods how to print a picture, so I have one way.

This article will show simple method to print the picture

How to Print:

Create an empty project

On the form, add a menu bar and change its name to File

Below File menu, add these menus

mnuOpen

mnuPageSetup

mnuPrint

So we have following shape

Fileà Open, Page Setup, Print

After adding the menus

Go to Toolbox and go to Printing section and add following controls

PageSetupDialog1

We are using for setting page set up

PrintDocument1

We are using for printing document

PrintPreviewDialog1

We are using for take preview of our print

After adding all these controls, add a picture box to the form

Now we are going to add code

First of all add following code to Open menu

We will use it for opening a picture from disk

Code:

Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click

Try

Dim openFileDlg As New OpenFileDialog

openFileDlg.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"

With openFileDlg

.CheckFileExists = True

.ShowReadOnly = False

.Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"

.FilterIndex = 2

If .ShowDialog = Windows.Forms.DialogResult.OK Then

PictureBox1.Image = Image.FromFile(.FileName)

End If

End With

Catch ex As Exception

MessageBox.Show("Can not open file")

End Try

End Sub

After adding above code , add following code in Page setup menu

We are using it for setting page setup

Code:

Private Sub mnuPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PageSetupToolStripMenuItem.Click

PageSetupDialog1.Document = PrintDocument1

PageSetupDialog1.ShowDialog()

End Sub

Now we will add code in Print event click

Code:

Private Sub mnuPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click

PrintPreviewDialog1.Document = PrintDocument1

PrintPreviewDialog1.ShowDialog()

End Sub

At the end we will add code behind PrintPage event of PrintDocument1

So double click on it on the form or access its PageEvent in code editor and add following code

Code:

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

e.Graphics.DrawImage(Me.PictureBox1.Image, 0, 0, Me.PictureBox1.Width, Me.PictureBox1.Height)

End Sub

In the above code we are using DrawImage method of Graphics

DrawImage has five arguments

First argument which we pass is image, so here we are passing here our picture

Next argument is the x location of image and next location is y

Third is width of image, we are setting our picture width and so next is height of our image.

0 comments