k@l-3l


i have recently tried to develop a printing code in VB and whenever i click the button it throws an exception stating that the Value name can not be null .

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

e.Graphics.DrawImage(formImage, 200, 300)

End Sub

{"Value cannot be null. Parameter name: image"}

PLS HELP !!!!



Re: Print Preview Error

Rea Software Engineering


Hi,

The error is stating that the object you are trying to use does not have a name. In you instance the object is "formImage". I am guessing you are programmatically creating the "formImage" object and whatever it contains.

Since you didn't provide any of the coding on how the "formImage" object is instantiated I can't provide a direct coding resolution.

At this moment I would suggest adding a line of code that names your "formImage" object. An example would be something like: formImage.Name = "myImage". Your complete coding may look something like this:

Code Snippet

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

formImage.Name = "Test"

e.Graphics.DrawImage(formImage, 200, 300)

End Sub

This would work if you are declaring the "formImage" as an object that supports the "name" property. If you want a more clear response, please post the complete coding you are using to create the "formImage" and any other coding you are using to initialize the "myDocument.print" method.

I hope the above example helps...if not then I am sure we can get it solved if further coding is posted.

Thanks,

James






Re: Print Preview Error

k@l-3l

thank you for your help, james. I have found a print component which more or less does the job (here is the URL, http://msdn2.microsoft.com/en-us/vbasic/aa701261.aspx), but, i have discovered another problem. lets say that i want to print an A4 sized document using forms, using a print component. Everything works so far up to even having a print preview, unfortunately i am unable to set it to A4 size. Some help would be greatly appreciated. Thanks




Re: Print Preview Error

JohnWein

You should be able to set the paper size manually using the PrintDialog control or programmatically using the PaperSize class.



Re: Print Preview Error

k@l-3l

Hi James,

i still encounter same error. i have pasted the coding below. Hope you can give help me. Thank You

Imports System.Drawing

Public Class Form1

Private Const SRCCOPY As Integer = &HCC0020

Private Declare Function BitBlt _

Lib "gdi32.dll" ( _

ByVal hdcDest As IntPtr, _

ByVal x As Int32, _

ByVal y As Int32, _

ByVal Width As Int32, _

ByVal Height As Int32, _

ByVal hdcSrc As IntPtr, _

ByVal xSrc As Int32, _

ByVal ySrc As Int32, _

ByVal dwRop As Int32 _

) As Boolean

Public formImage As Bitmap

Public Sub PrintForm(Optional ByVal fullWindow As Boolean = False)

' Copyright c 2004 by Mathias Schiffer. Leave this notice in place.

' Copies a screenshot of the form this code is being run in

' to the active printer. Place this code in a form module.

' Set fullWindow to True for including borders and titlebar.

' Remember to use an Imports reference to System.Drawing.

' Also, add a PrintDocument1 control to the form for printing.

' The disadvantage of this code is that it does only capture

' those parts of the form that are actually visible (screenshot),

' i.e. it must not be hidden and may not even be obscured.

' VB's PrintForm method does not require that, so this is not a

' perfect replacement. Keep that in mind.

With Me ' This can easily be replaced with a Form parameter

' Create a Graphics object for the form

Dim formGraphics As Graphics = .CreateGraphics

' Create a compatible bitmap and get its Graphics object

'If fullWindow Then

' formImage = New Bitmap(.Width, .Height, formGraphics)

'Else

formImage = New Bitmap(.ClientRectangle.Width, _

.ClientRectangle.Height, _

formGraphics)

'End If

Dim memGraphics As Graphics = Graphics.FromImage(formImage)

' Get the target and source device context handles (hDC)

Dim sourceDC As IntPtr = formGraphics.GetHdc

Dim targetDC As IntPtr = memGraphics.GetHdc

' Do the screenshot part of the job

'If fullWindow Then

' ' Consider the border width and the titlebar height

' Dim widthDelta As Integer = (.Width - _

' .ClientRectangle.Width)

' Dim heightDelta As Integer = (.Height - _

' .ClientRectangle.Height)

' ' Copy the form including its titlebar and borders

' BitBlt(targetDC, _

' 0, 0, _

' .ClientRectangle.Width + widthDelta, _

' .ClientRectangle.Height + heightDelta, _

' sourceDC, _

' 0 - widthDelta \ 2, 0 - (heightDelta - widthDelta \ 2), _

' SRCCOPY)

'Else

' Copy the form's client area

BitBlt(targetDC, _

94, 112, .ClientRectangle.Width, .ClientRectangle.Height, _

sourceDC, _

.ClientRectangle.X, .ClientRectangle.Y, _

SRCCOPY)

'End If

' Release DCs and dispose objects

formGraphics.ReleaseHdc(sourceDC)

formGraphics.Dispose()

memGraphics.ReleaseHdc(targetDC)

memGraphics.Dispose()

' formImage now has the form's image. Print it before disposing it.

PrintDocument1.Print() ' Invokes PrintDocument1_PrintPage (below)

' Finally dispose the image

formGraphics.Dispose()

End With

End Sub

Public Sub PrintDocument1_PrintPage( _

ByVal sender As System.Object, _

ByVal e As System.Drawing.Printing.PrintPageEventArgs _

) Handles PrintDocument1.PrintPage

' Print the screen shot in formImage. Use DrawImage's

' parameters to control the position of the printer output.

e.Graphics.DrawImage(formImage, 100, 200) ' printing position x=100, y=200

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

PrintPreviewDialog1.Document = PrintDocument1

' Set the zoom factor of the dialog box to 100 percent.

PrintPreviewDialog1.PrintPreviewControl.Zoom = 1.0

PrintPreviewDialog1.ShowDialog()

'PrintForm()

End Sub

End Class