: Check uploaded image is Grayscale or not - ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial : Check uploaded image is Grayscale or not

In this article I am going to explain how to check uploaded image is Grayscale or not in

Some of application form only accept Grayscale or black & white scanned photograph and signature. In this example I am going to check image size, dimensions and color.


 HTML Markup:

<html xmlns="">
<head runat="server">
 <script type="text/javascript" >
     function ValidateFilesize() {
         var maxFileSize = 20480;
         var uploadedFile = document.getElementById("<%=FileUpload1.ClientID %>");
         if (uploadedFile.files[0].size > maxFileSize)
             alert('File size should be less than 20 KB')
             return false;
          return true;
    <form id="form1" runat="server">
            <tr><td>Upload File :</td><td><asp:FileUpload ID="FileUpload1" runat="server" accept=".jpg,.png,.jpeg"/> </td></tr>
            <tr><td></td><td> <asp:Button ID="Button1" runat="server" Text="Save" OnClientClick="return ValidateFilesize();"  OnClick="Button1_Click" /></td></tr>

First of all import the namespace.

C# Code:
using System.Drawing; Code:
Imports System.Drawing

On button write the below given code:

C# Code:

bool CheckGrayScale;
    protected void Page_Load(object sender, EventArgs e)

    public bool IsGrayScale(Bitmap bmap, int RGB)
        Color pixelColor = Color.Empty;
        int colorRGB;
        for (int x = 0; x < bmap.Width; x++)
            for (int y = 0; y < bmap.Height; y++)
                pixelColor = bmap.GetPixel(x, y);
                colorRGB = Math.Abs(pixelColor.R - pixelColor.G) + Math.Abs(pixelColor.G - pixelColor.B) + Math.Abs(pixelColor.B - pixelColor.R);
                if (RGB < colorRGB)
                    return false;
        return true;
    protected void Button1_Click(object sender, EventArgs e)
            if (FileUpload1.FileBytes.Length < 20480)
                Bitmap bitmap = new Bitmap(FileUpload1.PostedFile.InputStream);
                if (bitmap.Width < 100 || bitmap.Height < 120)
                    CheckGrayScale = IsGrayScale(bitmap, 8);
                    if (CheckGrayScale == true)
                        string filepath = Server.MapPath("~/images/") + Guid.NewGuid() + FileUpload1.FileName;
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Image uploaded successfully')", true);
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please upload Grayscale or black & white image')", true);
        catch (Exception ex) { }
    } Code:

Dim CheckGrayScale As Boolean
    Public Function IsGrayScale(bmap As Bitmap, RGB As Integer) As Boolean
        Dim pixelColor As Color = Color.Empty
        Dim colorRGB As Integer
        For x As Integer = 0 To bmap.Width - 1
            For y As Integer = 0 To bmap.Height - 1
                pixelColor = bmap.GetPixel(x, y)
                colorRGB = Math.Abs(pixelColor.R - pixelColor.G) + Math.Abs(pixelColor.G - pixelColor.B) + Math.Abs(pixelColor.B - pixelColor.R)
                If RGB < colorRGB Then
                    Return False
                End If
        Return True
    End Function

    Protected Sub Button1_Click1(sender As Object, e As EventArgs) Handles Button1.Click
            If FileUpload1.FileBytes.Length < 20480 Then
                Dim bitmap As New Bitmap(FileUpload1.PostedFile.InputStream)
                If bitmap.Width < 100 OrElse bitmap.Height < 120 Then
                    CheckGrayScale = IsGrayScale(bitmap, 8)
                    If CheckGrayScale = True Then
                        Dim filepath As String = Server.MapPath("~/images/") + Guid.NewGuid().ToString() + FileUpload1.FileName
                        ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "alertMessage", "alert('Image uploaded successfully')", True)
                        ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "alertMessage", "alert('Please upload Grayscale or black & white image')", True)
                    End If
                End If
            End If
        Catch ex As Exception
        End Try
    End Sub

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.