tags:

views:

116

answers:

1

I have an asp.net page where I am letting the user upload an image file using a FileUpload control. I allow them to upload png, gif and jpeg images. I want to limit the type of image they can upload, specifically I want to only allow them to upload images which have a colour mode on RGB.

How can I check the colour mode of an uploaded file programatically in .Net?

+5  A: 

1) Load the uploaded file into a System.Drawing.Image object.

Image img = Image.FromStream( yourFileUpload.PostedFile.InputStream );

1a) Make sure to handle any exceptions here as the uploaded file may not be an image.

2) Check the Image.PixelFormat property. PixelFormat is an enumeration with a lot of values, many of which are RGB. It might make more sense to filter out the ones you specifically don't want rather than filter in the ones you do want... the formats are "16 bits per pixel RGB", "32 bits per pixel RGB", "Indexed", etc.

Adam Sills