Better way to get a bitmap file's format

  • Thread starter Thread starter Paul Loveless
  • Start date Start date
P

Paul Loveless

Hi all, I've written the following code to retrieve the file format of an
image file:

Dim objImageFormat As ImageFormat

objImageFormat = Me.SourceImage.RawFormat

If objImageFormat.Equals(Imaging.ImageFormat.Bmp) Then
Me.ImageFormat = "BMP"

ElseIf objImageFormat.Equals(Imaging.ImageFormat.Emf) Then
Me.ImageFormat = "EMF"

ElseIf objImageFormat.Equals(Imaging.ImageFormat.Exif) Then
Me.ImageFormat = "EXIF"

ElseIf objImageFormat.Equals(Imaging.ImageFormat.Gif) Then
Me.ImageFormat = "GIF"

ElseIf objImageFormat.Equals(Imaging.ImageFormat.Icon) Then
Me.ImageFormat = "ICON"

ElseIf objImageFormat.Equals(Imaging.ImageFormat.Jpeg) Then
Me.ImageFormat = "JPEG"

ElseIf objImageFormat.Equals(Imaging.ImageFormat.Png) Then
Me.ImageFormat = "PNG"

ElseIf objImageFormat.Equals(Imaging.ImageFormat.Tiff) Then
Me.ImageFormat = "TIFF"

ElseIf objImageFormat.Equals(Imaging.ImageFormat.Wmf) Then
Me.ImageFormat = "WMF"

Else
Me.ImageFormat = "Unknown"

End If

Although this works, I would like to know if there is a better way than
having all these "if" statements. In addition, if new formats were added, I
would have to add the new format to the list. Is there a way of writing the
code so that it would automatically pick up any new formats.

Regards,
Paul
 
Paul Loveless said:
Hi all, I've written the following code to retrieve the file format
of an image file:

Dim objImageFormat As ImageFormat

objImageFormat = Me.SourceImage.RawFormat

If objImageFormat.Equals(Imaging.ImageFormat.Bmp) Then
Me.ImageFormat = "BMP"

ElseIf objImageFormat.Equals(Imaging.ImageFormat.Emf) Then
Me.ImageFormat = "EMF"
[...]

Although this works, I would like to know if there is a better way
than having all these "if" statements. In addition, if new formats
were added, I would have to add the new format to the list. Is there
a way of writing the code so that it would automatically pick up any
new formats.

Me.ImageFormat = objImageFormat.ToString
 
I tried this Armin. For example, this is what I get for a jpg image.

"[ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]"

A gif looks like this.
"[ImageFormat: b96b3cb0-0728-11d3-9d7b-0000f81ef32e]"

What I am looking for is the name of the image format, not this long hex
string - looks like a guid.

Thus, the reason why I created the long list of "if" statements.

Regards,
Paul

Armin Zingler said:
Paul Loveless said:
Hi all, I've written the following code to retrieve the file format
of an image file:

Dim objImageFormat As ImageFormat

objImageFormat = Me.SourceImage.RawFormat

If objImageFormat.Equals(Imaging.ImageFormat.Bmp) Then
Me.ImageFormat = "BMP"

ElseIf objImageFormat.Equals(Imaging.ImageFormat.Emf) Then
Me.ImageFormat = "EMF"
[...]

Although this works, I would like to know if there is a better way
than having all these "if" statements. In addition, if new formats
were added, I would have to add the new format to the list. Is there
a way of writing the code so that it would automatically pick up any
new formats.

Me.ImageFormat = objImageFormat.ToString
 
Paul Loveless said:
I tried this Armin. For example, this is what I get for a jpg
image.

"[ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]"

A gif looks like this.
"[ImageFormat: b96b3cb0-0728-11d3-9d7b-0000f81ef32e]"

What I am looking for is the name of the image format, not this long
hex string - looks like a guid.

Thus, the reason why I created the long list of "if" statements.

I know what's your intention. I tried this before posting my suggestion:

Dim objImageFormat As System.Drawing.Imaging.ImageFormat

objImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
MsgBox(objImageFormat.ToString)

It works. It returns "Jpeg", not a GUID. (Framework 1.1)
 
Hi Armin,

Dim objImageFormat As ImageFormat
pic.Image = Image.FromFile ("C:\Tmp\foo.jpg")
objImageFormat = pic.Image.RawFormat
S = ImageFormat.Jpeg.ToString & ", " & objImageFormat.ToString

S is "Jpeg, [ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]"

Regards,
Fergus
 
Not quite Armin. Your example works, but it still doesn't do the right job.
You see, after creating an Image object, I use Bitmap.FromFile to get an
image from disk and load it into memory. To determine what the image format
is, I inspect the RawFormat property. I don't know beforehand that the image
is a jpg or a gif for example, thus I can't set "objImageFormat =
System.Drawing.Imaging.ImageFormat.Jpeg". As you and I have discovered,
calling ToString on an ImageFormat object returns the guid, not the name of
the format, which is what I want. Hence, I must test the RawFormat property
against the list of formats in the if statements to get the correct name of
the format. To me, code like this is pretty ugly and I was hoping that there
would be another better way of accomplishing the same thing. I decided not
to inspect the filename extension, because it may not always be correct. For
example, it's still possible to load a gif into memory if it was called
"picture.abc" instead of "picture.gif".

I can live with the code if I have to, but I was wondering if there was a
more elegent, less amateurish solution that other people in this group may
know of. If you have any more ideas, I'd like to hear them. Or if you need
me to further clarify my intent, let me know.

BTW, thanks for the prompt responses.

Regards,
Paul

Armin Zingler said:
Paul Loveless said:
I tried this Armin. For example, this is what I get for a jpg
image.

"[ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]"

A gif looks like this.
"[ImageFormat: b96b3cb0-0728-11d3-9d7b-0000f81ef32e]"

What I am looking for is the name of the image format, not this long
hex string - looks like a guid.

Thus, the reason why I created the long list of "if" statements.

I know what's your intention. I tried this before posting my suggestion:

Dim objImageFormat As System.Drawing.Imaging.ImageFormat

objImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
MsgBox(objImageFormat.ToString)

It works. It returns "Jpeg", not a GUID. (Framework 1.1)
 
Fergus Cooney said:
Dim objImageFormat As ImageFormat
pic.Image = Image.FromFile ("C:\Tmp\foo.jpg")
objImageFormat = pic.Image.RawFormat
S = ImageFormat.Jpeg.ToString & ", " &
objImageFormat.ToString

S is "Jpeg, [ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]"

??
You want to say that ImageFormat.Jpeg.ToString also returns "Jpeg"?
 
Paul Loveless said:
Not quite Armin. Your example works, but it still doesn't do the
right job. You see, after creating an Image object, I use
Bitmap.FromFile to get an image from disk and load it into memory. To
determine what the image format is, I inspect the RawFormat property.
I don't know beforehand that the image is a jpg or a gif for example,
thus I can't set "objImageFormat =
System.Drawing.Imaging.ImageFormat.Jpeg".

I know that you can't assign System.Drawing.Imaging.ImageFormat.Jpeg, but I
just wanted to show that ToString method of a
System.Drawing.Imaging.ImageFormat.Jpeg object returns "Jpeg".
As you and I have
discovered, calling ToString on an ImageFormat object returns the
guid, not the name of the format, which is what I want.

No, it does return the name. The problem is that the object returned by the
RawFormat property does not return one of the IamgeFormat members. When I
load a jpeg file using Image.FromFile, comparing the RawFormat property of
the loaded image to ImageFormat.Jpeg, it returns false.
Hence, I must
test the RawFormat property against the list of formats in the if
statements to get the correct name of the format.


To me, code like
this is pretty ugly and I was hoping that there would be another
better way of accomplishing the same thing. I decided not to inspect
the filename extension, because it may not always be correct. For
example, it's still possible to load a gif into memory if it was
called "picture.abc" instead of "picture.gif".

I can live with the code if I have to, but I was wondering if there
was a more elegent, less amateurish solution that other people in
this group may know of. If you have any more ideas, I'd like to hear
them. Or if you need me to further clarify my intent, let me know.


I did some more checks. I executed

?img.RawFormat is Imaging.ImageFormat.Jpeg

in the immediate window and it returns False, whereas

objImageFormat.Equals(Imaging.ImageFormat.Jpeg)

returns True. I didn't know this. So, you are right, I am wrong. :-)

Ok, I can only suggest to add the different formats to an array(list) and
compare them in a loop.
 
Ok, I can only suggest to add the different formats to an array(list) and
compare them in a loop.

Hmm...that is one way. Maybe I'll try that.

Thanks Armin.
Paul
 
Back
Top