Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
grimreeper132

How to load an image in C# Net Standard

Recommended Posts

Posted · Original PosterOP

As the tital says how do I load an image in C# using .NetStandard. The aim of the function is just to confirm the file is definately a image, be it png, bmp, ico, tiff or webp. Also because it makes it easier for me if it can only use currently inbuilt tools and no NuGet files that would be swell as I plan to build this as a libary to use this externally in other code I am running to check if files have the correct file extension

 

EDIT:

Because I forgot to say this before and a number of people have suggested it. I am readeding the magic bytes (the bytes that say Hey I'm a PNG etc.) but in an attempt to error check that these are deffinately correct I want to load it as an image. The entire point of the code is to detect file types, more specifically the file types of files in a chache which do not have file extensions and by just checking for the magic bytes does not always work correctly, e.g. you get mp4s which aren't mp4s, or pictures which aint pictures etc.


The owner of "too many" computers, called

The Lord of all Toasters (1920X 1080ti 32GB)

The Toasted Controller (i5 4670, R9 380, 24GB)

The Semi Portable Toastie machine (i7 3612QM (was an i3) intel HD 4000 16GB)'

Bread and Butter Pudding (i7 7700HQ, 1050ti, 16GB)

Pinoutbutter Sandwhich (raspberry pi 3 B)

The Portable Slice of Bread (N270, HAHAHA, 2GB)

Muffinator (C2D E6600, Geforce 8400, 6GB, 8X2TB HDD)

Toastbuster (WIP, should be cool)

loaf and let dough (A printer that doesn't print black ink)

The Cheese Toastie (C2D (of some sort), GTX 760, 3GB, win XP gaming machine)

The Toaster (C2D, intel HD, 4GB, 2X1TB NAS)

Matter of Loaf and death (some old shitty AMD laptop)

windybread (4X E5470, intel HD, 32GB ECC) (use coming soon, maybe)

And more, several more

Link to post
Share on other sites

You could just check the file extension to see if it fits your list. 


LTT Fan Fiction:

PC game list: 

 

Link to post
Share on other sites

Convert the file you get into a data stream. Load the stream into the Image class (built into .Net), if there is no error it is an image, if there is an error it is either not an image or is corrupt. 

 

Just checking the file extension wont work because I can rename any file .PNG and it would pass your filter but still not be an image. 

 

One thing to note, however, is that technically a .PDF can also be an image, and my suggested method will not work for this as .NET can't convert .PDF streams into .SVG without additional libraries (as far as I know). 

Link to post
Share on other sites
Posted · Original PosterOP
6 hours ago, fpo said:

You could just check the file extension to see if it fits your list. 

Problem is, I am trying to check to see if the file extension is wrong, as I am taking a large number of files with no file extension and trying to find it's file extension, so I need to error check that any image that I try to get is actually an image. I am also going to  do a similar thing with video and audio.

 

 

3 hours ago, FlappyBoobs said:

Convert the file you get into a data stream. Load the stream into the Image class (built into .Net), if there is no error it is an image, if there is an error it is either not an image or is corrupt. 

 

Just checking the file extension wont work because I can rename any file .PNG and it would pass your filter but still not be an image. 

 

One thing to note, however, is that technically a .PDF can also be an image, and my suggested method will not work for this as .NET can't convert .PDF streams into .SVG without additional libraries (as far as I know). 

I am checking the magic bytes as well and this is an add on to this that I am doing to check that the guess is correct, as the guessing from magic bytes is still a massive guess.

So in theory changing your file extension of a file to png still be flagged as the old file type (e.g. jpg, bmp, txt, other), so yea.

And thanks I will try that. And I have not got onto pdfs yet, but I will be going on to them later, after sorting other file types out.


The owner of "too many" computers, called

The Lord of all Toasters (1920X 1080ti 32GB)

The Toasted Controller (i5 4670, R9 380, 24GB)

The Semi Portable Toastie machine (i7 3612QM (was an i3) intel HD 4000 16GB)'

Bread and Butter Pudding (i7 7700HQ, 1050ti, 16GB)

Pinoutbutter Sandwhich (raspberry pi 3 B)

The Portable Slice of Bread (N270, HAHAHA, 2GB)

Muffinator (C2D E6600, Geforce 8400, 6GB, 8X2TB HDD)

Toastbuster (WIP, should be cool)

loaf and let dough (A printer that doesn't print black ink)

The Cheese Toastie (C2D (of some sort), GTX 760, 3GB, win XP gaming machine)

The Toaster (C2D, intel HD, 4GB, 2X1TB NAS)

Matter of Loaf and death (some old shitty AMD laptop)

windybread (4X E5470, intel HD, 32GB ECC) (use coming soon, maybe)

And more, several more

Link to post
Share on other sites
56 minutes ago, grimreeper132 said:

Problem is, I am trying to check to see if the file extension is wrong, as I am taking a large number of files with no file extension and trying to find it's file extension, so I need to error check that any image that I try to get is actually an image. I am also going to  do a similar thing with video and audio.

 

 

I am checking the magic bytes as well and this is an add on to this that I am doing to check that the guess is correct, as the guessing from magic bytes is still a massive guess.

So in theory changing your file extension of a file to png still be flagged as the old file type (e.g. jpg, bmp, txt, other), so yea.

And thanks I will try that. And I have not got onto pdfs yet, but I will be going on to them later, after sorting other file types out.

C# won't be able to read ICO, TIFF and WEBP with the Bitmap loader. ICO need to use another class and you can work your way around. TIFF and WEBP you will need third party libraries to detect.

 

If you want to detect images like Photoshop (psd, etc) i did it in the past and you need to pay an insane price to use their library which is C++ i believe and work well using DLLImport. I my case it was the most important format to figure out so our company bought the library. I think it was 5 figure price because i remember saying "at that price i should buy a MSSQL server altogether"

Link to post
Share on other sites

You would be better off just opening the file as a regular binary file, and reading the first let's say 100 bytes or so in a byte buffer.

PNG, BMP and WEBP have specific formats, with some bytes always have exact values.

For example PNG files always have "PNG" string in the first 4 bytes, and the data is arranged in "blocks" where each block has a 4 byte signature, and typically you have IHDR (header) block at the start of the file and the actual data is in the IDAT block..

BMP start with "BM" ..

WEBP pictures use the RIFF container (like AVI files) ... there's a 4 byte keyword at the beginning : https://developers.google.com/speed/webp/docs/riff_container

ICO files don't have a magic sequence of bytes but the format is super basic so you could parse a few bytes just to see if you read sane values (like width and height of icons multiples of 8 ex 16x16, 32x32, 48x48, 256x256 etc)

TIFF is more difficult, as it supports loads of "codecs" but i think it has two signatures, depending on little endian or big endian ...

 

See https://en.wikipedia.org/wiki/List_of_file_signatures

 

If you don't mind having to load third party DLLs you could always resort to using ImageMagick in your application ... see https://github.com/dlemstra/Magick.NET

 

 

Link to post
Share on other sites
Posted · Original PosterOP
23 minutes ago, mariushm said:

You would be better off just opening the file as a regular binary file, and reading the first let's say 100 bytes or so in a byte buffer.

PNG, BMP and WEBP have specific formats, with some bytes always have exact values.

For example PNG files always have "PNG" string in the first 4 bytes, and the data is arranged in "blocks" where each block has a 4 byte signature, and typically you have IHDR (header) block at the start of the file and the actual data is in the IDAT block..

BMP start with "BM" ..

WEBP pictures use the RIFF container (like AVI files) ... there's a 4 byte keyword at the beginning : https://developers.google.com/speed/webp/docs/riff_container

ICO files don't have a magic sequence of bytes but the format is super basic so you could parse a few bytes just to see if you read sane values (like width and height of icons multiples of 8 ex 16x16, 32x32, 48x48, 256x256 etc)

TIFF is more difficult, as it supports loads of "codecs" but i think it has two signatures, depending on little endian or big endian ...

 

See https://en.wikipedia.org/wiki/List_of_file_signatures

 

If you don't mind having to load third party DLLs you could always resort to using ImageMagick in your application ... see https://github.com/dlemstra/Magick.NET

 

 

I am reading those exact values the magic bytes, for example the first 7 bytes of a PNG are 137, 80, 78, 71, 13, 10, 26, 10, BUT what I want to do is a second error check to make sure that the file is actually a PNG and not a diffrent file with similar magic bytes at the start. Which is done by loading it as an image, and seeing if that works.

 

I was trying not to use any 3rd party dlls as that means that any code built using this as a reffrenence also download said dlls, (unless I am mistaken) but I have given up and am used NuGet to add System.Drawing.Common, which has what I need.

And Tiffs ones are
Big endian 73, 73, 42, 0
Little endian 77, 77, 0, 42 (if I remember correctly)


The owner of "too many" computers, called

The Lord of all Toasters (1920X 1080ti 32GB)

The Toasted Controller (i5 4670, R9 380, 24GB)

The Semi Portable Toastie machine (i7 3612QM (was an i3) intel HD 4000 16GB)'

Bread and Butter Pudding (i7 7700HQ, 1050ti, 16GB)

Pinoutbutter Sandwhich (raspberry pi 3 B)

The Portable Slice of Bread (N270, HAHAHA, 2GB)

Muffinator (C2D E6600, Geforce 8400, 6GB, 8X2TB HDD)

Toastbuster (WIP, should be cool)

loaf and let dough (A printer that doesn't print black ink)

The Cheese Toastie (C2D (of some sort), GTX 760, 3GB, win XP gaming machine)

The Toaster (C2D, intel HD, 4GB, 2X1TB NAS)

Matter of Loaf and death (some old shitty AMD laptop)

windybread (4X E5470, intel HD, 32GB ECC) (use coming soon, maybe)

And more, several more

Link to post
Share on other sites
Posted · Original PosterOP
1 hour ago, Franck said:

C# won't be able to read ICO, TIFF and WEBP with the Bitmap loader. ICO need to use another class and you can work your way around. TIFF and WEBP you will need third party libraries to detect.

 

If you want to detect images like Photoshop (psd, etc) i did it in the past and you need to pay an insane price to use their library which is C++ i believe and work well using DLLImport. I my case it was the most important format to figure out so our company bought the library. I think it was 5 figure price because i remember saying "at that price i should buy a MSSQL server altogether"

Yea the point here is I don't wanna pay for it, as I am skint and so am coding it myself, and need to load the images, audio, etc as an error checking step


The owner of "too many" computers, called

The Lord of all Toasters (1920X 1080ti 32GB)

The Toasted Controller (i5 4670, R9 380, 24GB)

The Semi Portable Toastie machine (i7 3612QM (was an i3) intel HD 4000 16GB)'

Bread and Butter Pudding (i7 7700HQ, 1050ti, 16GB)

Pinoutbutter Sandwhich (raspberry pi 3 B)

The Portable Slice of Bread (N270, HAHAHA, 2GB)

Muffinator (C2D E6600, Geforce 8400, 6GB, 8X2TB HDD)

Toastbuster (WIP, should be cool)

loaf and let dough (A printer that doesn't print black ink)

The Cheese Toastie (C2D (of some sort), GTX 760, 3GB, win XP gaming machine)

The Toaster (C2D, intel HD, 4GB, 2X1TB NAS)

Matter of Loaf and death (some old shitty AMD laptop)

windybread (4X E5470, intel HD, 32GB ECC) (use coming soon, maybe)

And more, several more

Link to post
Share on other sites
15 minutes ago, grimreeper132 said:

Yea the point here is I don't wanna pay for it, as I am skint and so am coding it myself, and need to load the images, audio, etc as an error checking step

the PSD is very slow to validate.

Bitmap is quite fast using a try catch but slow on large amount of files.

Audio can be insanely fast if you don't care about partial corruption as you can play the first few byte and stop if it doesn't crash but you won't be able to run advanced codec if you do not use third party. Luckily there is a couple great audio libraries for .net and i like NAudio very much and it support most format and can even read some audio stream from HD video.

Link to post
Share on other sites

At least for PNG it would be super simple... just parse the file: http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html

 

valid png file would have

* magic sequence at the beginning

* IHDR chunk must be first

* IEND chunk must be last

* for each chunk,

** read length of chunk

** is length value bigger than the remaining file data size - bad file (or incomplete)

** compute CRC checksum of chunk data, compare with the last 4 bytes of the chunk which contain CRC - comparison fails, file probably not png or is png but corrupt.

 

bmp files also have kind of basic structure, read header , use width and height and color info (bytes per color) to estimate how much data you'd have in the file, if the file size is smaller than that value then bmp is probably corrupt.

Link to post
Share on other sites
Posted · Original PosterOP
3 minutes ago, mariushm said:

At least for PNG it would be super simple... just parse the file: http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html

 

valid png file would have

* magic sequence at the beginning

* IHDR chunk must be first

* IEND chunk must be last

* for each chunk,

** read length of chunk

** is length value bigger than the remaining file data size - bad file (or incomplete)

** compute CRC checksum of chunk data, compare with the last 4 bytes of the chunk which contain CRC - comparison fails, file probably not png or is png but corrupt.

 

bmp files also have kind of basic structure, read header , use width and height and color info (bytes per color) to estimate how much data you'd have in the file, if the file size is smaller than that value then bmp is probably corrupt.

what about tiffs, jpgs, and other file types


The owner of "too many" computers, called

The Lord of all Toasters (1920X 1080ti 32GB)

The Toasted Controller (i5 4670, R9 380, 24GB)

The Semi Portable Toastie machine (i7 3612QM (was an i3) intel HD 4000 16GB)'

Bread and Butter Pudding (i7 7700HQ, 1050ti, 16GB)

Pinoutbutter Sandwhich (raspberry pi 3 B)

The Portable Slice of Bread (N270, HAHAHA, 2GB)

Muffinator (C2D E6600, Geforce 8400, 6GB, 8X2TB HDD)

Toastbuster (WIP, should be cool)

loaf and let dough (A printer that doesn't print black ink)

The Cheese Toastie (C2D (of some sort), GTX 760, 3GB, win XP gaming machine)

The Toaster (C2D, intel HD, 4GB, 2X1TB NAS)

Matter of Loaf and death (some old shitty AMD laptop)

windybread (4X E5470, intel HD, 32GB ECC) (use coming soon, maybe)

And more, several more

Link to post
Share on other sites

I don't have any experience with TIFF. All I know is that it's a super complex format, considering it can be LE or BE (little/big endian), that it was used for faxes and for scans, that it supports loads of bit depths, that it can hold multiple images like GIF files etc etc

GIF files are fairly easy to parse

JPG ... it's complicated... there's at least two ways of storing data in a .jpg file see  https://en.wikipedia.org/wiki/JPEG and https://en.wikipedia.org/wiki/JPEG_File_Interchange_Format

There are multiple libraries that decode JPG files .. you could compile them and then use dlls in your project... see https://forum.doom9.org/showpost.php?p=1868051&postcount=126

 

 

Link to post
Share on other sites
34 minutes ago, mariushm said:

I don't have any experience with TIFF. All I know is that it's a super complex format, considering it can be LE or BE (little/big endian), that it was used for faxes and for scans, that it supports loads of bit depths, that it can hold multiple images like GIF files etc etc

GIF files are fairly easy to parse

JPG ... it's complicated... there's at least two ways of storing data in a .jpg file see  https://en.wikipedia.org/wiki/JPEG and https://en.wikipedia.org/wiki/JPEG_File_Interchange_Format

There are multiple libraries that decode JPG files .. you could compile them and then use dlls in your project... see https://forum.doom9.org/showpost.php?p=1868051&postcount=126

 

 

JPEG works in .net by default. as long as the load doesn't crash it's valid. They already implemented that. But TIFF yeah it's a *****. There was a third party DLL i used in the past with .net 2.0. There is probably newer ones.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×