Disappearing Image

I got the following Code from a book which was devoted to 2005 Express. I put an image in the PictureBox(picPic) and can go on to print it. [In this program I have a PrintPreview so saving the actual print.] However! After printing the image disappears from the Form (And I suspect the actual PictureBox as well!). If I use the event to select a different image, this new one doesn't appear on the Form but it shows on the PrintPreview and prints. Can some kind person please tell me why this is happening. I have obviously tried to work it out, but I am baffled!

Please let me have the solution.

Thanks in advance. AliBong.

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.Drawing.Imaging

Imports System.IO

Imports System.Drawing.Text

Imports System.Drawing.Printing

PublicClass Form1

PrivateSub btnGetPic_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnGetPic.Click

If ofdPic.ShowDialog = Windows.Forms.DialogResult.OKThen

picPic.Image = Image.FromFile(ofdPic.FileName)

txtPic.Text = ofdPic.FileName

EndIf

EndSub

PrivateSub btnPrintPic_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnPrintPic.Click

PrintPreviewDialog1.Document = PrintDocument1

PrintPreviewDialog1.ShowDialog()

'PrintDocument1.Print()

EndSub

PrivateSub PrintDocument1_PrintPage(ByVal senderAsObject,ByVal eAs System.Drawing.Printing.PrintPageEventArgs)Handles PrintDocument1.PrintPage

With PrintDocument1.DefaultPageSettings.PaperSize

If picPic.Width < .WidthThen

picPic.Left = (.Width - picPic.Width) / 2

Else

picPic.Left = 0

EndIf

EndWith

With PrintDocument1.DefaultPageSettings.PaperSize

If picPic.Height < .HeightThen

picPic.Top = (.Height - picPic.Height) / 2

Else

picPic.Top = 0

EndIf

EndWith

Dim rAs Rectangle =New Rectangle(picPic.Left, _

picPic.Top, picPic.Width, picPic.Height)

Console.WriteLine(ofdPic.FileName)

e.Graphics.DrawImage(picPic.Image, r)

EndSub

[4955 byte] By [Alibong] at [2007-12-22]
# 1
Thread moved to VB.NET Express forums
ahmedilyas at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

Try adding the middle line and see if it helps:

Private Sub btnGetPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetPic.Click

Me.picPic.Dock = System.Windows.Forms.DockStyle.Left ' <

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

TallDude at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

Okay, now I see what is happening.

The printpage routine is changing the width, height and top of the picturebox values.

You can, at the beginning of the printpage routine save the original values and

restore them at the end of the routine.

' begining of routine

Dim a as integer = picPic.width

Dim b as ............. do the other values

' end of routine restore values

picPic.width = a

etc. ...........................

These changes were probably moving the picture out of the viewable picturebox area.

TallDude at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

Hello Tall Dude,

Grrrrrrrrreeeeeaaaat. Those answers were great! In what was simple for you, you have taught me a lot. Before I go too far I must thank you very much. Thank you.

I did your first suggestion but the image took a lot of the Form. Never mind!

I did your second suggestion but had to drag the Form's edges to locate the image. Never mind!

I say 'never mind' because you have given me greater insight into the world of 'graphics' and given me some research to do.

I only got your mails a few minutes ago and just had time to quickly work with the solutions and get back here.

And to the Moderators: Sorry I put question in wrong place - finger trouble? Mouse ran away?

Thank you all. AliBong.

Alibong at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...