Sunday, June 25, 2017

Asp.net : 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 asp.net.


Description:
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.

Implementation:

 HTML Markup:

<html xmlns="http://www.w3.org/1999/xhtml">
<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;
      }
   </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <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>
        </table>
    </div>
    </form>      
</body>
</html>


First of all import the namespace.

C# Code:
using System.Drawing;

VB.net 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)
    {
        try
        {
            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;
                        FileUpload1.SaveAs(filepath);
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Image uploaded successfully')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please upload Grayscale or black & white image')", true);
                    }
                }
            }
        }
        catch (Exception ex) { }
    }


VB.net 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
            Next
        Next
        Return True
    End Function

    Protected Sub Button1_Click1(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            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
                        FileUpload1.SaveAs(filepath)
                        ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "alertMessage", "alert('Image uploaded successfully')", True)
                    Else
                        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




No comments:

Post a Comment