determine the mime/ContentType of a binary stream?

A

Alex Vincent

HI all,

I'm storing images, jpgs/gifs/png/bmps and also some other binary files in a
SQL image field in a SQL 2k database.

Most of the images enter the database via a asp.net webform upload, here I
can use HttpPostedFile.ContentType to enter into the db the contenttype of
the file (because in some cases BMPs are actually renamed (by dumb users) to
..JPG which obviously causes problems...

The problem I have is where I am picking files off the file system (in a
bulk import) into a filestream, how can I tell if an image is really a JPG
or a BMP or wotever once its in a byte[] ?...

cheers if u can help..

alex
 
K

Kevin Spencer

The problem I have is where I am picking files off the file system (in a
bulk import) into a filestream, how can I tell if an image is really a JPG
or a BMP or wotever once its in a byte[] ?...

How does your Operating System know what type of file a file is? By the
extension. If the extension is wrong, your Operating System can't identify
the type of file that it is, or do anything useful with it. A binary file is
just a bunch of bits. In other words, you're going to have to use the same
technique. If a user renames a file extension through their own stupidity,
that's their problem.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Alex Vincent said:
HI all,

I'm storing images, jpgs/gifs/png/bmps and also some other binary files in a
SQL image field in a SQL 2k database.

Most of the images enter the database via a asp.net webform upload, here I
can use HttpPostedFile.ContentType to enter into the db the contenttype of
the file (because in some cases BMPs are actually renamed (by dumb users) to
.JPG which obviously causes problems...

The problem I have is where I am picking files off the file system (in a
bulk import) into a filestream, how can I tell if an image is really a JPG
or a BMP or wotever once its in a byte[] ?...

cheers if u can help..

alex
 
H

Hans Kesting

How does your Operating System know what type of file a file is? By the
extension.

Not quite true: lots of filetypes have identifying information somewhere at
the start.
For unix (which does not depend on extensions) there are tools that find the
type
of the file by inspecting this information.

Hans Kesting
 
M

Michael Pearson

I've not tried this, but I think you can load the Byte array into a
System.Drawing.Image. Once you have it loaded, check the RawFormat()
Property. I think that will look at the actual byte array and figure out
what kind of image it really is, reguardless of the extension.

Michael


Alex Vincent said:
HI all,

I'm storing images, jpgs/gifs/png/bmps and also some other binary files in a
SQL image field in a SQL 2k database.

Most of the images enter the database via a asp.net webform upload, here I
can use HttpPostedFile.ContentType to enter into the db the contenttype of
the file (because in some cases BMPs are actually renamed (by dumb users) to
.JPG which obviously causes problems...

The problem I have is where I am picking files off the file system (in a
bulk import) into a filestream, how can I tell if an image is really a JPG
or a BMP or wotever once its in a byte[] ?...

cheers if u can help..

alex
 
K

Kevin Spencer

Well, now, I didn't think of that! I have no idea whether or not it would
work, but I'm willing to bet that it would, since these are all graphics
files.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Michael Pearson said:
I've not tried this, but I think you can load the Byte array into a
System.Drawing.Image. Once you have it loaded, check the RawFormat()
Property. I think that will look at the actual byte array and figure out
what kind of image it really is, reguardless of the extension.

Michael


Alex Vincent said:
HI all,

I'm storing images, jpgs/gifs/png/bmps and also some other binary files
in
a
SQL image field in a SQL 2k database.

Most of the images enter the database via a asp.net webform upload, here I
can use HttpPostedFile.ContentType to enter into the db the contenttype of
the file (because in some cases BMPs are actually renamed (by dumb
users)
to
.JPG which obviously causes problems...

The problem I have is where I am picking files off the file system (in a
bulk import) into a filestream, how can I tell if an image is really a JPG
or a BMP or wotever once its in a byte[] ?...

cheers if u can help..

alex
 

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