image.fromstream error

L

Lance

hey all,

first time vb.net 2005 user, after sticking vb6 out for a long time...

anyway, using this code
======================
Dim FS As FileStream = File.OpenRead(Filename)
Dim theImage As Image
Try
theImage = Image.FromStream(FS, False, False)
Catch ex As Exception
Debug.Print(FS.Name)
FS.Close()
Exit Sub
End Try

Dim Width As Integer = theImage.Width
Dim Height As Integer = theImage.Height
theImage.Dispose()
FS.Close()
========================

works fine except for on a handful of images. all of the images are tif files that open
in evey application i have ever tried to open them in. i'm just getting the width and
height without loading the entire image into memory (they are about 150 MB each and there
are thousands of them). the debug window shows:

"A first chance exception of type 'System.ArgumentException' occurred in
System.Drawing.dll"

on the "bad" ones and if i step into, the error i get is "Parameter not valid" at the

theImage = Image.FromStream(FS, False, False)

line. what could be causing this? the code works for 99.9% of the images, but not for a
handful that are otherwise perfectly fine.

thanks,
lance
 
M

Michel van den Berg

Dear Lance,

I don't know if it will help you with answering your question, but
Image.FromStream() throws an ArgumentException when the stream does not have
a valid image format (see the docs for this). Perhaps this is the case. If I
were you, I would try to see if there are any differences in the 'good' and
the 'bad' files (also read-only or not). Also, try to change the last
parameter to true instead of false, as this parameter will validate the
'ImageData'.

Hope this helps you,

Michel van den Berg
 
L

Lance

i should have mentioned that i had already tried setting the third parameter to true. the
same thing happens, unfortunately. none of the file are read only, or opened already.
they were all actually created by the same software using the same parameters.

the handful that return these errors open fine in every other tag viewer and image
application. that's what is so frustrating.

lance
 
M

Michel van den Berg

Dear Lance,

Is the code your posted the (real) code as implemented in your app, or do
you, for instance, alter or dispose the filestream?

Michel van den Berg
 
L

Lance

michel,

that's pretty much the code, yeah. i don't alter the stream - if by "alter"
you mean change the image in any way. i'm just opening it long enough to be
able to get the width and height of a tif file. please let me know if i
missunderstood your question.

by the way, the reason the third parameter is set to false is so that this
can be done very quickly, as the are thousands of images to extract the
dimensions from. as i mentioned, though, it doesn't matter whether the
parameter is set to true, as it still produces the same error on the same
images.

lance
 
M

Michel van den Berg

Dear Lance,

Could you perhaps send me some good and bad images, so that I may try the
code for myself? You can send it to my e-mail address.

Michel van den Berg
 
L

Lance

i'll zip up a good one and a "bad" one and upload it to our ftp site and
email you the address. these are large images, but the ftp server
connection is pretty fast, so if you've got broadband, they will only take a
few minutes to download.

lance
 
L

Lance

sent...

Michel van den Berg said:
Dear Lance,

Could you perhaps send me some good and bad images, so that I may try the
code for myself? You can send it to my e-mail address.

Michel van den Berg
 
M

Michel van den Berg

Dear Lance,

As I suspected is your problem the picture itself. To be more precise:
within its compression it is using.
The working picture has no compression whereas the bad picture has. GDI+
does not support (that) compression with TIF files.
To verify my statement: I resaved the bad image without compression and it
now works.
So, I suggest, if possible, to alter the program generating these images to
output without compression.

Hope it helps,

Michel van den Berg
 
L

Lance

thanks so much for looking into this. i was assured by the image supplier that all of the
images were created using the same parameters. after your reply, i used a tag view and
can now see that the "software" key is indeed different in the bad images, as is the
compression of course.

i guess this means i'm going to need to write my own tif image info class that doesn't
make use of GDI+.

thanks,
lance
 
L

Lance

michel,

interestingly, there are some packbit compressed tif files that seem to work.

lance
 
M

Michel van den Berg

Dear Lance,

I must admit I am not sure why some work and some not. Perhaps, your
software uses different compression schemes to optimize?
In your case, I would probally go for a 3rd party toolkit like leadtools.
You could always create your own imaging library ;)

Michel van den Berg
 

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