Adding Copyright Information to Drawn Image in GDI+


This article has been excerpted from book "Graphics Programming with GDI+"

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.

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;

Figure 15.4.gif

FIGURE 15.4: A graphics copyright application

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Copyright
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Image origImage;

        private void fileToolStripMenuItem_Click(object sender, 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
                    pictureBox.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.

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.

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.

Figure 15.5.jpg

FIGURE 15.5: Thumbnail view of an 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 patter image and use the MeasureString method until we get a font that fits the graphics, 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);
sizofstring =
g.MeasureString ("Copyright GDI+ Inc., ", fc);
if (sizeofstring.Width<pattern.Width)
{
if (sizeofstring.Height<pattern.Height)
{
foundfont = true;
g.DrawString("Copyright GDI+ Inc.,",
fc, new SolidBrush(Color.Black), 1, 15);
}
}
else
fntSize = fntSize – 1;
}


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 mean 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");
return;
}
int imgWidth;
int imgHeight;
int fntSize = 300;
int x, y;
int a, re, gr, b1, x1, y1, z1;
int size;
Bitmap pattern;
SizeF sizeofstring;
bool foundfont;
imgWidth = origImage.Width;
imgHeight = origImage.Height;
size=imgWidth*imgHeight;
patter = 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;
g.Clear(Color.White);
while(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.Width<pattern.Width)
{
if (sizeofstring.Height<pattern.Height)
{
foundfont = true;
g.DrawString ("Add Copyright Info",
fc, new SolidBrush (Color.Black),
1, 15);
}
}
else
fntSize = fntSize–1;
}
MessageBox.Show ("Creating new graphic",
"GraphicsCopyright");
for (x=1; x<pattern.Width; x++)
{
for (y=1; y<pattern.Height; y++) //
{
if (pattern.GetPixel (x,y).ToArgb() == Color.Black.ToArgb())
{
a= temp.GetPixel(x,y).A;
re= temp.GetPixel(x,y).R;
gr= temp.GetPixel(x,y).G;
b1= temp.GetPixel(x,y).B;
 
x1=re;
y1=gr;
z1=b1;
if(b1+25<255)
b1=b1+25;

if(gr+25<255)
gr=gr+25;

if(re+25<255)
gr=gr+25;

if(x1-25>0)
x1=x1-25;

if (y1-25>0)
y1=y1-25;

if (z1-25>0)
z1=z1-25;

tempg.DrawEllipse (new Pen (
new SolidBrush (Color.Black)),
x, y+1, 3,3 );
tempg.DrawEllipse (new Pen (
new SolidBrush (
Color.FromArgb (a, x1, y1, z1))),
x, y, 1, 1);
}
}
}
MessageBox.Show ("Output file is output.jpeg",
"GraphicsCopyright");
tempg.Save();
origImage.Save ("output.jpeg", ImageFormat.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 watermark in the image (see Figure 15.6).

Figure 15.6.jpg


FIGURE 15.6: An image after copyright has been added to it
 
book.gif

erver'>

Recommended Free Ebook
Similar Articles