简体   繁体   中英

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.

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM