Sergio Tapia
Sergio Tapia

Reputation: 41228

How can I get the width and height of an Image?

My plan is to load a picture from a users hard drive and then do some wizardry on it in memory.

First thing is first, how can I get the height and width from an Image?

Also, say I wanted to select a rectangular piece from the image, how would I do that in memory (no GUI)?

Thank you for your guidance.

Upvotes: 0

Views: 2155

Answers (2)

Ed Swangren
Ed Swangren

Reputation: 124790

You will need to add a refernce to the System.Drawing namespace.

using( Image image = Image.FromFile( path ) );
{
    // use image.Width and image.Height
}

You can then use the Clone method with a Rectangle as the argument to get a sub-section of the image, or you can just loop through the pixels in the Rectangle of interest (you'll want to use the Bitmap class for that, maybe LockBits and a pointer depending on how large the image is and how fast this needs to be).

Upvotes: 2

Arseni Mourzenko
Arseni Mourzenko

Reputation: 52381

What are you using? Outside WPF, you can use System.Drawing.Bitmap class Width and Height properties to get the size of an image. Clone method will enable you to copy a rectangular piece.

You can also use image classes used in GUI applications outside WPF/Windows Forms application.

Upvotes: 0

Related Questions