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.
精彩评论