Browse DevX
Sign up for e-mail newsletters from DevX


Generate and Modify Images with GDI+  : Page 5

Use GDI+ and the System.Drawing classes to simplify the process of manipulating images.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Rotating Images
Rotation transformations are often very useful, as many digital cameras and other devices that capture or scan photographic images store images in a static orientation. Depending on the content of the image and the orientation of the content, the saved format may not be suitable for viewing. By implementing a rotation function for images in your application, you can orient the images properly for display. To rotate an image, you create an Image object and load the sample image. Then use the RotateFlip method to change the orientation:

img = System.Drawing.Image.FromFile (strFileName); img.RotateFlip (RotateFlipType.Rotate90FlipNone); img.Save(Application.StartupPath + cstrRotateFileName);

The RotateFlip method takes a RotateFlipType parameter—an enumeration member that specifies the type of rotation/flip transformation to perform on the img object. After performing the rotation, you can save and display the image as described in the cropping section.

Changing Image Formats
Changing the file format of an image is particularly important, because many applications have limited image type support. You may need to convert the original storage format for an image to share it with other people or to use it in disparate applications.

To convert the file format conversion code, all you need is an Image object and a single method call. Consider the following code fragment:

img = System.Drawing.Image.FromFile (strFileName); img.Save (Application.StartupPath + cstrConvertedFileName, System.Drawing.Imaging.ImageFormat.Jpeg);

The Image.Save method takes two parameters: the file path where you want to save the converted image, and an ImageFormat object property that corresponds to the desired image format.

Drawing Text on Images
The integration of images and text is a common requirement for applications. Here's how to apply text to the sample image file:

const string cstrTextFileName = "\\Text_Sample.gif"; System.Drawing.Image img; img = System.Drawing.Image.FromFile(strFileName); Bitmap bmp = new Bitmap(img); Graphics gphText = Graphics.FromImage(bmp); Font f = new Font ("Arial", 12, FontStyle.Bold | FontStyle.Italic); gphText.DrawString("Sample", f, Brushes.Black, new Point(10, 15)); bmp.Save(Application.StartupPath + cstrTextFileName); pbImage.Image = bmp;

To draw the text, create a Bitmap instance containing the image data, and then use the Graphics.DrawString method to superimpose the string "Sample" on the image. You pass a Font object to the DrawString method. In this case, the code sets the font family to Arial, the font size to 12 (point), and the font style to bold and italic. Figure 2 shows the result of the text drawing operation.

Figure 2: The string "Sample" superimposed on the sample image.
As demonstrated by the sample application, you can use the GDI+ classes of the .NET Framework to build image manipulation functionality into your Windows forms applications, performing features including cropping, image rotation, drawing text on images and much more. While the GDI+ classes offer a huge variety of features for managing images, the code shown in this article comprises only a marginal foray into the available functionality. You can use the demonstrated techniques to develop applications that store and manage documents stored in a variety of formats, or simple easily-deployed applications that let users manipulate images.

Randy Holloway is the founder of Winformation Systems, a technology consulting and training initiative specializing in the development of enterprise systems in the Windows environment and Web services technologies. Randy speaks on technology-related topics and writes for publications including DevX.com and Visual Studio Magazine. Contact Randy at randyholloway@winformationsystems.com.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date