ARTICLE

Adding Copyright to a Graphics

Posted by John O Donnell Articles | GDI+ & Graphics September 28, 2001
With the popularity of digital cameras and the increase of digital archive web sites allowing you to buy images to use its handy to be able to add a copyright to your image.
Reader Level:

With the popularity of digital cameras and the increase of digital archive web sites allowing you to buy images to use its handy to be able to add a copyright to your image. However you dont want to just write the word copyright across your image do you? No, in the world of .Net something cleverer is needed.

So here I present a way to add a copyright to a jpeg image without obscuring the image.

Here is a picture of the application screen :

At this point all I have done is clicked the File-Open menu and selected image1 from my c:\ drive. As you can see I have displayed a thumbnail image using this code :

Image ithumbnail = originalimage.GetThumbnailImage(100, 100, null, new IntPtr());

Once the image is loaded, click the Add Copyright button and let the program do its work.

Here is the image before the program does its stuff (this image is one taken by myself in Boston )

And there it is again with the copyright added.

The result is that you have written the word DOTNET across the graphic but it appears to be transparent.

So how was it done?

Basically what I am doing is looking the pixel for every point the word DOTNET word affect at a specific font size. I then increase the values for red, green and blue by 25 to brighten them. This line of code shows how to look at a pixel. Note pattern is my bitmap used to write my word across.

if (pattern.GetPixel(x,y).ToArgb()==Color.Black.ToArgb())

So what happens if pixels red, green and blue values are already set to 255? Simple I cheat and dont touch them! In most cases this works as there will always be a pixel which is totally different in brightness. One addition I could make would be to analyze the image and work out if its a dark or bright image and adjust accordingly.

How do I know which pixel to change.? I create a second bitmap which is the same size as the original image. I will write my word on this image and use it as the pattern for the main image.

To achieve this I want to use the largest font I can to create a big word across the image. Of course my image can be any size so I cant predict the font size.

To do this I create a graphic class based upon my pattern image and use the MeaseureString method until I get a font that fits the graphic as you can below.

while(foundfont==false)
{
Font fc = new Font(" Georgia ", fontsize, System.Drawing.FontStyle.Bold);
sizeofstring=new SizeF(imagewidth,imageheight);
sizeofstring=g.MeasureString("DOTNET",fc);
if (sizeofstring.Width<pattern.Width)
{
if (sizeofstring.Height<pattern.Height)
{
foundfont=true;
g.DrawString("DOTNET", fc, new SolidBrush(Color.Black),1,1);"
}
}
else
fontsize=fontsize-1;
}

Once the pattern bitmap is created all I have to do is loop through all the pixels and if it is black which means its part of the word, I then go to the main image and increase the brightness of the pixel giving the glass-like effect.  

Hope you like the effect!

COMMENT USING

Trending up