Home / Code Snippets / Blog article: Get Width And Height Of Image In C#

| RSS

Get Width And Height Of Image In C#

March 31st, 2009 | 5 Comments | Posted in Code Snippets

To get width and height of an image we can use System.Drawing.Image class. This code snippet shows how to retrieve width and height of an image.

string filePath = @"c:\deepak\MeeGo.jpg";
Image img = Image.FromFile(filePath);
 
Console.WriteLine(string.Format("Height: {0}",img.Size.Height));
 
Console.WriteLine(string.Format("Width: {0}",img.Size.Width));

Leave a Reply 15531 views, 6 so far today |
  • No Related Post
Follow Discussion

5 Responses to “Get Width And Height Of Image In C#”

  1. Eddy Ho Says:

    Errors. – pri_height and Height
    Error 1 The name ‘Height’ does not exist in the current context C:\299-TwoDShape\Program.cs 12 17 299-
    Why pri_height and Height have errors ???

    the Complete Refernce C# 4.0

    TwoDShape

    using System;

    class TwoDShape
    {
    double pri_width;
    double pri_height;

    public TwoDShape()
    {
    Width = Height = 0.0;
    }

    public TwoDShape(double w, double h)
    {
    Width = w;
    Height = h;
    }

    public TwoDShape(double x)
    {
    Width = Height = x;
    }

    public TwoDShape(TwoDShape ob)
    {
    Width = ob.Width;
    Height = ob.pri_height;
    }

    public double Width
    {
    get { return pri_width; }
    set { pri_width = value < 0 ? -value : value; }
    }

    public double Heigth
    {
    get { return pri_heigth; }
    set { pri_heigth = value < 0 ? -value : value; }
    }

  2. sankarshan parida Says:

    Check this code
    if (FileUpload1.HasFile)
    {
    string path = Server.MapPath(“Twinkle”);
    string filename = FileUpload1.FileName;
    FileUpload1.SaveAs(path + “/” + filename);
    string filePath = path + “/” + filename;
    System.Drawing.Image img = Bitmap.FromFile(filePath);

    Console.WriteLine(string.Format(“Height: {0}”, img.Size.Height));
    Console.WriteLine(string.Format(“Width: {0}”, img.Size.Width));

    }

  3. Bruzas Says:

    thanks for your take

  4. johns steve Says:

    me using the above mentioned code but getting a error like ‘System.Web.UI.WebControls.Image’ does not contain a definition for ‘FromFile’ plz help what should do for removing this error.is there any library added??

  5. Deepak Says:

    Johns,

    Use the image class from System.Drawing namespace.

Leave a Reply