How get Image Height Width in ASP.Net

Complete code to get the Width and height of image in asp.net C#, you have to pass FileUpload control.

 public  float GetImageWidth(FileUpload uploadedFile)
    {
        float width = 0;

        try
        {
            if (uploadedFile.PostedFile.ContentLength == 0)
            {
                throw new ArgumentNullException("Invalid image file");
            }

            if (!IsImage(uploadedFile))
            {
                throw new FormatException("Invalid file type found. Not an image file");
            }

            System.Drawing.Image oImg = System.Drawing.Image.FromStream(uploadedFile.PostedFile.InputStream);

            width = oImg.PhysicalDimension.Width;
            oImg.Dispose();
        }
        catch (Exception ex)
        {
         Response.Write(ex.Message);
        }

        return width;
    }

Code to get the height of image in asp.net C#, you have to pass FileUpload control.

 public static float GetImageHeight(FileUpload uploadedFile)
    {
        float height = 0;

        try
        {
            if (uploadedFile.PostedFile.ContentLength == 0)
            {
                throw new ArgumentNullException("Invalid Image file");
            }

            if (!IsImage(uploadedFile))
            {
                throw new FormatException("Invalid file type found. Not an image file");
            }

            System.Drawing.Image oImg = System.Drawing.Image.FromStream(uploadedFile.PostedFile.InputStream);

            height = oImg.PhysicalDimension.Height;
            oImg.Dispose();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

        return height;
    }

Related Alrticles

Add Your Business in Free Listing


FREE!!! Registration