Image error "Invalid Paramter used"

P

Patrick Dugan

Hello,
I'm trying to load different images (icons) into a PictureBox1.Image.

The first image loads just fine, but the second image always returns the
error "Invalid property used."
It doesn't matter what icons are loaded. The first always shows up and any
icons after that give me the error.

Here is the offending code:
The "DestinationPath" variable is the full path and filename of the icon
Private Sub LoadIcon()
Dim FStream As New FileStream(DestinationPath, FileMode.Open)
Try
PictureBox1.Image = System.Drawing.Image.FromStream(FStream)
Catch Err As Exception
MsgBox(Err.Message)
End Try
FStream.Close()
End Sub


Is there something I am forgetting to close or clear?
 
B

Barry

Welcome to MS graphics!!!! (I am not a big fan of .NET graphics).

You are closing your file stream in the subroutine. Once you do that,
the picturebox cannot manipulate the image or even re-draw it.

Since the file must stay open so the picturebox can use it, try
this....

Private Sub LoadIcon()
'set up an image
Dim newimage As System.Drawing.Image
'fill from file
'but the file will stay open !!!!!
newimage = System.Drawing.Image.FromFile(lcl_filename)
'this will close the last file opened
If Not PictureBox1.Image Is Nothing Then
PictureBox1.Image.Dispose()
End If

Try
PictureBox1.Image = newimage
Catch Err As Exception
MsgBox(Err.Message)
End Try

End Sub

Now the file will be opened and locked by the picturebox, but you will
be able to do what you are trying.

HTH
Barry
Hello,
I'm trying to load different images (icons) into a PictureBox1.Image.

The first image loads just fine, but the second image always returns the
error "Invalid property used."
It doesn't matter what icons are loaded. The first always shows up and any
icons after that give me the error.

Here is the offending code:
The "DestinationPath" variable is the full path and filename of the icon
Private Sub LoadIcon()
Dim FStream As New FileStream(DestinationPath, FileMode.Open)
Try
PictureBox1.Image = System.Drawing.Image.FromStream(FStream)
Catch Err As Exception
MsgBox(Err.Message)
End Try
FStream.Close()
End Sub


Is there something I am forgetting to close or clear?

bceggersATcomcastDOTnet
 
P

Patrick Dugan

I tried the code but after it loads a few icons it gives me an out-of-memory
error.

I'm really beginning to "love" vb.net right now. Some things that were
simple are now ridiculously hard
and other things are easier.

Do I need to dispose of the newimage after it gets placed into the
Picturebox?
 
B

Barry

No, don't dispose of newimage. If you try that you will find that the
picturebox can't handle the image.

Is your out of memory error a "GDI+" error or a system error?

How many icons can you load before you get this error? I don't have
any problems with 30+ times into the subroutine.

Barry


I tried the code but after it loads a few icons it gives me an out-of-memory
error.

I'm really beginning to "love" v b.netrightnow.Somethingsthatwere
simple are now ridiculously hard
and other things are easier.

Do I need to dispose of the newimage after it gets placed into the
Picturebox?

bceggersATcomcastDOTnet
 
P

Patrick Dugan

When I step through it the error occurs on this line:

newimage = System.Drawing.Image.FromFile(DestinationPath)

This is the second time through. How can I tell what type of memory error
it is? I don't see anything in the error
message about GDI just the "Out of memory" text
 
B

Barry

Are you sure that the second file is an icon file? What happens if it
goes first (or third)? Can you just load it into the picturebox from
the IDE?

From the help file --

Image.Fromfile - Creates an Image object from the specified file.
<snip>
If the file does not have a valid image format or if GDI+ does not
support the pixel format of the file, this method throws an
OutOfMemoryException exception.

It seems as if the Icon file has something different. What is it?
What is the size, resolution, pallete, etc. ?


When I step through it the error occurs on this line:

newimage = System.Drawing.Image.FromFile(DestinationPath)

This is the second time through. How can I tell what type of memory error
it is? I don't see anything in the error
message about GDI just the "Out of memory" text

bceggersATcomcastDOTnet
 
P

Patrick Dugan

I think you are right. One of the icons looks okay but won't load into the
image. I'll have to consider it bad and if it won't load skip it and move
to the next.
 

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