Working with Images
Using the Image Class
The Image class is usually your first port of call when using images in .NET programming. It
enables you to store a picture in memory so that it can be manipulated, and it exposes many pieces
of information about that picture, including its size and how many colors are used. Image is the
base class from which another class, Bitmap, is derived. We'll be looking at Bitmap in a while, but
we should look at Image's capabilities first.
The Image class comes packed with lots of useful functionality, such as:
- Loading an image from a file and exposing information about it.
- Saving a picture to a file in a specified format such as JPEG or GIF.
- Rotating or flipping a picture.
- Enabling detailed manipulation of the picture by using other classes in conjunction with the Image object. For example, you can draw lines on the picture by using an associated Graphics object.
There are of course many other properties and methods of the Image class but the ones outlined
above are by far the most commonly used. To introduce the Image class we'll use three of the
above features in one go!
Besides being a programmer, I am also an amateur photographer, and I like to take personal
portraits. I often take several dozen photographs on my digital camera over the course of a day.
One problem I face is that I hold my camera at 90 degrees to the horizontal; so that I can fit the
chosen subject in the viewfinder properly.
This is annoying because all the saved pictures on my camera are stored sideways and my camera
doesn't have an auto-rotate feature. So every day I have to open all the new photographs on my
computer, rotate them by 90 degrees to make them appear the right way up and then save them
again. I like to save them as JPEG files to save space.
How could I use C# to make my life easier? Simple, just by creating a method that will rotate the
image 90 degrees.
private void RotateAndSaveImage(String input, String output)
{
//create an object that we can use to examine an image file
Image img = Image.FromFile(input);
//rotate the picture by 90 degrees
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
//re-save the picture as a Jpeg
img.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg);
//tidy up after we've finished
img.Dispose();
}
That's an outline of the whole process of loading, rotating, and saving a picture, all wrapped in a
short method; exception handling and parameter validation were omitted for clarity. Let us look at
it in detail.
Firstly, the RotateAndSaveImage() method takes two string parameters: input and output. The
input parameter is the path of the picture file we want to use. The output parameter is the name
of the new file that will be created when the Image.Save() method is called.
Further I could use a for loop to iterate through all the files within a folder on my computer.
I can even try this on my camera's memory card directly and pass each filename to this
RotateAndSaveImage() method. So in doing this, I can save valuable time, being free to travel
further from home in pursuit of my hobby.
ليتك تحلو والحياة مريره وليتك ترضى والانام غضاب
وليت الذى بينى وبينك عامر وبينى وبين العالمين خراب
إذا صحّ منك الود فالكل هيّن وكل الذى فوق التراب تراب
اللهم ارحم موتانا وموتى جميع المسلمين
اللهم أنزل على قبورهم الضياء والنور والفسحة والسرور وجازهم بالاحسان إحسان وبالسيئات عفوا وغفرانا