Adding Copyright Information to a Drawn Image

With the popularity of digital cameras and the increase of digital archive Web sites that allow you to buy images, it's handy to be able to add a copyright to your image. Not only that, you can also add text specifying the date and place of the photograph.

In this section we will create an application with support for the display of copyright information on displayed images. First we create a Windows application and add a File | Open menu item, a button with text "Add Copyright," and a picture box. The final form looks like Figure 15.4.

Figure 15.4. A graphics copyright application


After adding the controls, we add a reference to the System.Drawing.Imaging namespace to the application. Then we add a class Image variable to the application as follows:

Image origImage;


The File | Open menu allows us to browse images and view a thumbnail of a specific image. The code for the menu click event handler is given in Listing 15.6. After reading the name of the image, we create an Image object from the file name using the Image.FromFile static method. After creating one Image object, we create another Image object using the GetThumbnailImage method of the Image class. GetThumbnailImage returns a thumbnail image. After that we simply set the Image property of PictureBox to display the image.

Listing 15.6 Browsing images

private void menuItem2_Click(object sender,
 System.EventArgs e)
 // Open file dialog
 OpenFileDialog fileDlg = new OpenFileDialog();
 fileDlg.InitialDirectory = "c:\" ;
 fileDlg.Filter= "All files (*.*)|*.*";
 fileDlg.FilterIndex = 2 ;
 fileDlg.RestoreDirectory = true ;
 if(fileDlg.ShowDialog() == DialogResult.OK)
 // Create image from file
 string fileName = fileDlg.FileName.ToString();
 origImage = Image.FromFile(fileName);
 // Create thumbnail image
 Image thumbNail =
 origImage.GetThumbnailImage(100, 100,
 null, new IntPtr());
 // View image in picture box
 pictureBox1.Image = thumbNail;

If we run the application and open a file using the Open menu item, the image will be displayed. The output looks like Figure 15.5.

Figure 15.5. Thumbnail view of an image


Once the image has been loaded, we click the Add Copyright button and let the program do its work. Basically we need to create an image on the fly, add text to the image using the DrawString method, and then save the image. To give the text a different shade, we need to change the color of the pixels that draw the text. In other words, we must change the brightness of the pixels that represent the text to distinguish the text pixels from the image pixels. We increase the values for the red, green, and blue component of the color by 25 to brighten the text pixels. We use the MeasureString method of the Graphics class to set the size and font of the text. (We discussed MeasureString in detail in Chapter 3.)

The maximum value for each of the red, green, and blue components of a color is 255. What happens if these values are already set to 255? Do we still increase their value by 25? No. In that case we cheat and don't touch these pixels. In most cases this approach works because there is always a pixel that is totally different in brightness. One additional thing we could do would be to analyze the image, determine whether it's a dark or bright image, and adjust it accordingly.

To find out which pixels to change, we create a second bitmap that is the same size as the original image. We write "Add Copyright Info" on this image and use it as the pattern for the main image.

We also want to use the largest font we can to create a big word across the image. Of course, the image can be any size, so we can predict the font size. To do this we create a graphics class based on our pattern image and use the MeasureString method until we get a font that fits the graphic, as in Listing 15.7.

Listing 15.7 Adding the copyright text

while(foundfont==false) { Font fc = new Font("Georgia", fntSize, System.Drawing.FontStyle.Bold); sizeofstring = new SizeF(imgWidth,imgHeight); sizeofstring = g.MeasureString("Copyright GDI+ Inc.,",fc); if (sizeofstring.Width

The complete code for the Add Copyright button click event handler is given in Listing 15.8. We read the image size and create a Bitmap object from the original size of the image. Then we create a Graphics object on the fly using this Bitmap object. Once the pattern bitmap has been created, all we have to do is loop through all the pixels and if a pixel is black (which means that it's part of the word), we go to the main image and increase its brightness, producing a glasslike effect.

Listing 15.8 Adding copyright to an image

private void button1_Click(object sender,
 System.EventArgs e)
 if(origImage == null)
 MessageBox.Show("Open a file");
 int imgWidth;
 int imgHeight;
 int fntSize=300;
 int x,y;
 int a,re,gr,bl,x1,y1,z1;
 int size;
 Bitmap pattern;
 SizeF sizeofstring;
 bool foundfont;
 imgWidth = origImage.Width;
 imgHeight = origImage.Height;
 pattern = new Bitmap(imgWidth,imgHeight);
 Bitmap temp = new Bitmap(origImage);
 Graphics g = Graphics.FromImage(pattern);
 Graphics tempg = Graphics.FromImage(origImage);
 // Find a font size that will fit in the bitmap
 foundfont = false;
 Font fc = new Font("Georgia",
 fntSize, System.Drawing.FontStyle.Bold);
 sizeofstring = new SizeF(imgWidth,imgHeight);
 sizeofstring =
 g.MeasureString("Add Copyright Info",fc);
 if (sizeofstring.Width0)

 if (y1-25>0)

 if (z1-25>0)

 tempg.DrawEllipse(new Pen(
 new SolidBrush(Color.Black)),
 x, y+1, 3, 3);
 tempg.DrawEllipse(new Pen(
 new SolidBrush(
 x, y, 1, 1);
 MessageBox.Show("Output file is output.jpeg",

Now we can run the application and browse images. When we click the Add Copyright button, we will get a message when the program is done adding text. The result creates what is commonly known as a watermark in the image (see Figure 15.6).

Figure 15.6. An image after copyright has been added to it


GDI+: The Next-Generation Graphics Interface

Your First GDI+ Application

The Graphics Class

Working with Brushes and Pens

Colors, Fonts, and Text

Rectangles and Regions

Working with Images

Advanced Imaging

Advanced 2D Graphics



Developing GDI+ Web Applications

GDI+ Best Practices and Performance Techniques

GDI Interoperability

Miscellaneous GDI+ Examples

Appendix A. Exception Handling in .NET

GDI+ Programming with C#
GDI+ Programming with C#
ISBN: 073561265X
Year: 2003
Pages: 145 © 2008-2020.
If you may any questions please contact us: