473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Better way to get a bitmap file's format

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

Nov 20 '05 #1
8 7413
"Paul Loveless" <pa****@rogers.com> schrieb
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
--
Armin

Nov 20 '05 #2
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" <az*******@freenet.de> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
"Paul Loveless" <pa****@rogers.com> schrieb
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
--
Armin

Nov 20 '05 #3
"Paul Loveless" <pa****@rogers.com> schrieb
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)
--
Armin
Nov 20 '05 #4
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
Nov 20 '05 #5
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" <az*******@freenet.de> wrote in message
news:ON*************@TK2MSFTNGP09.phx.gbl...
"Paul Loveless" <pa****@rogers.com> schrieb
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)
--
Armin

Nov 20 '05 #6
"Fergus Cooney" <fi******@tesco.net> schrieb

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"?
--
Armin

Nov 20 '05 #7
"Paul Loveless" <pa****@rogers.com> schrieb
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.
--
Armin

Nov 20 '05 #8
> 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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:ud**************@TK2MSFTNGP11.phx.gbl... "Paul Loveless" <pa****@rogers.com> schrieb
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.
--
Armin

Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoreyMas | last post by:
Hello Again, Here is what I want to do: I have an image that is 100 px by 100 px I want to parse each pixel in said image, and store the color of that pixel in a file (the reasons for...
6
by: charsh | last post by:
Hi, I using the code below to draw a text in a pictureBox1. //Start--------------------------------------------------------------- private void button1_Click(object sender, System.EventArgs e)...
2
by: active | last post by:
I find Bitmap.Save works for WMF files but Bitmap.FromFile does not. If I use FromFile on a WMF file that came with VS I get an exception. If I use it on a WMF file created with Bitmap.Save I...
2
by: Bruce D | last post by:
I'm having a problem saving my bitmap in my MySQL database. Apparently it's too big...but it shouldn't be. Here's what I got: Dim bmpDocument As Bitmap = Bitmap.FromHbitmap(hDibCopy) ' get...
0
by: AlVis1515 | last post by:
We had a VB6 program that was used to take a scanned signature image as a set of screen coordinates. The VB6 program then converted this into a bitmap file which we used to embed the signature...
0
by: AMDRIT | last post by:
Hello Everyone, Just curious how far off the mark I am with my Working Dialog. Any improvements are welcomed. Scenario, we have an application (vb'05) that at known times performs tasks that...
1
by: Smokey Grindel | last post by:
I have a bitmap object I want to return as a JPEG image with a compression set at 90% and progressive passes enabled, how can I do this in .NET 2.0? Progressive passes are not necessary but the...
6
by: \Frank\ | last post by:
On the Internet there are many descriptions of the memory layout for a DIB. But none that I can find for a Bitmap. Is that because a Bitmap's layout depends on a related device. If that's...
2
by: Piotrekk | last post by:
Hi I have a problem. I open bitmap file, load it to memory decrease color palette by proceeding Euclidean distance. As a result i have image reduced to - for example 18 colors. Once i save it -...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.