C# .NET - check an tiff file for it is coloured image or black and white image

Asked By paulraj benjamin on 12-Nov-11 05:55 AM
Dear All
I need to find a tiff file whether it is fully black and white or it is coloured using C#.
Any help would be appreciated.
Kirtan Patel replied to paulraj benjamin on 12-Nov-11 06:21 AM
Validation server control is used to validate the data of an input control. If the data does not pass validation, it will display an error message to the user.

 
<form runat="server">
  Name: <asp:TextBox id="name" runat="server" />
  <br />
  Age: <asp:TextBox id="age" runat="server" />
  <br />
  <br />
  <asp:Button runat="server" Text="Submit" />
  <br />
  <br />
  <asp:RequiredFieldValidator ControlToValidate="name" Text="The name field is required!" runat="server" />
</form>
 
 
Chintan Vaghela replied to paulraj benjamin on 12-Nov-11 06:40 AM
Hello,

  1. Transform the image to grayscale, using well a known colormatrix.
  2. Use SetThreshold method of the ImageAttributes class to set the threshold that separates black from white.
using (Graphics gr = Graphics.FromImage(SourceImage)) // SourceImage is a Bitmap object
        {                
            var gray_matrix = new float[][] { 
                new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, 
                new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, 
                new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, 
                new float[] { 0,      0,      0,      1, 0 }, 
                new float[] { 0,      0,      0,      0, 1 } 
            };

            var ia = new System.Drawing.Imaging.ImageAttributes();
            ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix));
            ia.SetThreshold(0.8); // Change this threshold as needed
            var rc = new Rectangle(0, 0, SourceImage.Width, SourceImage.Height);
            gr.DrawImage(SourceImage, rc, 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, ia);                
        }

Hope this helpful