Get File Size in KB, Dimensions (Height and Width) of Uploaded Image in ASP.Net in C#

In this article, we learn how to get size, height and width of uploaded image in asp.net in c#. As you have learned Imagecompression without losing quality in asp.net c# but If we want to compress image after specific size and height or width. We can do this very easily using both articles.


Source Code:

<fieldset style="width:350px">

                <legend>Get File Size,Height and Width of Uploaded Image </legend>

                <asp:FileUpload ID="FileUpload1" runat="server" />

                <br />                <br />

                <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="btnUpload_Click" />

            </fieldset>

 

Code Behind:

   protected void btnUpload_Click(object sender, EventArgs e)

    {

        System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);

        int height = img.Height;

        int width = img.Width;

        decimal size = Math.Round(((decimal)FileUpload1.PostedFile.ContentLength / (decimal)1024), 2);

        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Size: " + size + "KB\\nHeight: " + height + "\\nWidth: " + width + "');", true);

    }

Out-put:



Get File Size in KB, Dimensions (Height and Width) of Uploaded Image in ASP.Net in C# Get File Size in KB, Dimensions (Height and Width) of Uploaded Image in ASP.Net in C# Reviewed by NEERAJ SRIVASTAVA on 12:27:00 PM Rating: 5

No comments:

Powered by Blogger.