Placing an image on a Panel

J

Jerry West

I'm a newbie to .NET from VB6.

I have a Form with a Panel control on it. When the Form loads it maximizes
and so does the Panel control. I then fill an array with the path to images
at a specific folder. At that point a timer fires every x seconds which
calls a routine that draws an image onto the Panel. This works great the
first time the code runs. But each subsquent timer event fails to load a new
image --it is always the same image (which is the first one it loaded). Of
course, I stepped through the code and can clearly see a new image is being
drawn each time after first clearing the old one. Yet, it is always the same
image. Is there something about a Panel that will not load a new image? I
set a watch on the array which references the image to load. When I was
stepping through my code I noticed in the Watch window that on the first run
through the Watch window showed the array value in black text. On the next
run through --and all subsquent ones-- it was in red text. No warnings or
anything it was just in red text. Is that significant? I could find no docs
on what the meaning of this in the Watch window could be. My code is this:

Private Sub DrawTheImage()

Dim Photo As Short

Dim PanelGraphic As Graphics = pnlImage.CreateGraphics

PanelGraphic.Clear(Color.Black)

Try

Photo = RandomNumber(ss.Pics.Count)

Dim newImage As Bitmap =
Bitmap.FromFile(ss.Pics(Photo).FullName)

Dim x As Integer
Dim y As Integer
Dim wid As Integer
Dim hgt As Integer

If (newImage.Height / newImage.Width) > (pnlImage.Height /
pnlImage.Width) Then

' The new image is relatively tall and thin.
' Make the image as tall as possible.
hgt = pnlImage.ClientSize.Height
wid = CInt(hgt * newImage.Width / newImage.Height)

Else

' The new image is relatively short and wide.
' Make the image as wide as possible.
wid = pnlImage.ClientSize.Width
hgt = CInt(wid * newImage.Height / newImage.Width)

End If

x = (pnlImage.Width - wid) \ 2
y = (pnlImage.Height - hgt) \ 2

PanelGraphic.DrawImage(newImage, x, y, wid, hgt)

newImage.Dispose()
PanelGraphic.Dispose()

Catch ex As Exception

'DrawErrorImage()

End Try

End Sub
 
R

rowe_newsgroups

I'm a newbie to .NET from VB6.

I have a Form with a Panel control on it. When the Form loads it maximizes
and so does the Panel control. I then fill an array with the path to images
at a specific folder. At that point a timer fires every x seconds which
calls a routine that draws an image onto the Panel. This works great the
first time the code runs. But each subsquent timer event fails to load a new
image --it is always the same image (which is the first one it loaded). Of
course, I stepped through the code and can clearly see a new image is being
drawn each time after first clearing the old one. Yet, it is always the same
image. Is there something about a Panel that will not load a new image? I
set a watch on the array which references the image to load. When I was
stepping through my code I noticed in the Watch window that on the first run
through the Watch window showed the array value in black text. On the next
run through --and all subsquent ones-- it was in red text. No warnings or
anything it was just in red text. Is that significant? I could find no docs
on what the meaning of this in the Watch window could be. My code is this:

Private Sub DrawTheImage()

Dim Photo As Short

Dim PanelGraphic As Graphics = pnlImage.CreateGraphics

PanelGraphic.Clear(Color.Black)

Try

Photo = RandomNumber(ss.Pics.Count)

Dim newImage As Bitmap =
Bitmap.FromFile(ss.Pics(Photo).FullName)

Dim x As Integer
Dim y As Integer
Dim wid As Integer
Dim hgt As Integer

If (newImage.Height / newImage.Width) > (pnlImage.Height /
pnlImage.Width) Then

' The new image is relatively tall and thin.
' Make the image as tall as possible.
hgt = pnlImage.ClientSize.Height
wid = CInt(hgt * newImage.Width / newImage.Height)

Else

' The new image is relatively short and wide.
' Make the image as wide as possible.
wid = pnlImage.ClientSize.Width
hgt = CInt(wid * newImage.Height / newImage.Width)

End If

x = (pnlImage.Width - wid) \ 2
y = (pnlImage.Height - hgt) \ 2

PanelGraphic.DrawImage(newImage, x, y, wid, hgt)

newImage.Dispose()
PanelGraphic.Dispose()

Catch ex As Exception

'DrawErrorImage()

End Try

End Sub

What happens if you remove the "PanelGraphic.Dispose()" call?

Thanks,

Seth Rowe
 
J

Jerry West

Tried removing it but same results. I'm at a loss to know why? It seems the
red text of the watch item which provides the path to the next image to load
is significant. I have verified the path is correct and valid.

If anyone has any suggestions I'd love to hear them!

JW
 
J

Jerry West

I changed my code to simplify it and used a PictureBox control instead. Same
results:

Dim Photo As Integer

Photo = RandomNumber(ss.Pics.Count)

Dim myImage As Bitmap = New Bitmap(ss.Pics(Photo).FullName, True)

Me.pic1.Image = myImage

Again, I set a watch on ss.Pics(Photo).FullName. The first run through the
value for the watch item is in black text. The next run through the value
column shows the text value for that item in red text. This is in spite of
the fact that the path it indicates is valid. I'm guessing this fails
because of the reason why the debugger is flagging that watch item in red
text. It doesn't indicate an error --just places it in red text. I've looked
and looked for the significance of that in the help to no avail.

Anyone have an idea what could be going on here?

JW
 
J

Jerry West

Even stranger...if I step through the code it works fine. Why in the heck
would that be?!!

JW
 
L

Lloyd Sheen

Jerry West said:
Even stranger...if I step through the code it works fine. Why in the heck
would that be?!!

JW

Do you set the timer.enabled = false at the beginning of the timer event
handler. I have had problems when the timer event processing takes longer
than the timer interval.

Set the timer back to enabled=true as last thing you do in timer event.

Hope this helps
Lloyd Sheen
 
J

Jerry West

Yep, that was the trick. Thanks!

JW

Lloyd Sheen said:
Do you set the timer.enabled = false at the beginning of the timer event
handler. I have had problems when the timer event processing takes longer
than the timer interval.

Set the timer back to enabled=true as last thing you do in timer event.

Hope this helps
Lloyd Sheen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top