Often when placing photographic images on a website it is necessary to permanently overlay a watermark and/or Copyright statement on top of that image. Such an insertion helps to identify the person that took the photograph and indicate the organization that holds the Copyright. Doing this task by hand can become time consuming and is often inconsistent. Using some simple techniques there is an easy way to accomplish this programmatically using C# and GDI+.
I am going to show you a variety of techniques for manipulating an image. The following is a high level list of some of those techniques:
我将为你展示操作图片的各种各样的技术。以下是这些技术的high level list：
Inserting text on top of an image positioned relative to the size on an image
Dynamically choosing a System.Drawing.Font size to maximize readability
Manipulating the opacity of a String of text
Replacing a specific color in a bitmap to achieve transparency
Changing the opacity of an image through a 5x5 ColorMatrix
(Photo courtesy of AP wire)
（The first step in this process is to load a photographic image for which you would like to apply the watermark. This image can be virtually any size and resolution. For this example we will use an image that has a width of 449 pixels and a height of 346. The resolution is 72 dpi.
When the Main method is instantiated, the two variable of type string are defined. The first will define where to find the photograph, watermark and output the new image. The second will define the Copyright string we will use as part of our watermark.
The following creates an Image object from the specified file and then defines a variable for both its Width and Height. These dimensions are then used to build a Bitmap object with a 24 bits per pixel format for the color data. Lastly this Bitmap is then used to create a new Graphics object from the specified Bitmap image.
To maximize the size of the Copyright message we will test 7 different Font sizes to determine the largest possible size we can use for the width of our Photograph. To effectively do this, we will define an array of integers then iterate through those values measuring the Copyright string in the various point sizes. Once we have determined the largest possible size we will exit the loop and draw the text.
Since all photographs will have varying heights, determine a position 5% from the bottom of the image. Use the Copyright strings height to determine an appropriate y-coordinate for which to draw the string. Determine its x-coordinate by calculating the centre of the image then define a StringFormat object and set the StringAlignment to Center.
Now that we have all of the necessary positioning coordinates create a SolidBrush with a Color of 60% Black (alpha value of 153). Draw the Copyright string at the appropriate position offset 1 pixel to the right and 1 pixel down. This offset will create a shadow effect. Repeat this process using a White Brush drawing the same text directly on top of the previously drawn string.
To achieve a translucent watermark we will apply two color manipulations by defining an ImageAttributes object and setting two of its properties. The first step in manipulating the watermark image is to replace the background color with one that is transparent (Alpha=0, R=0, G=0, B=0). To do this we will use a Colormap and define a RemapTable. As previously shown my watermark was defined with a background of 100% Green this will be the color we search for and replace with transparency.
The second color manipulation is used to change the opacity of the watermark. This is done by applying a 5x5 matrix that contains the coordinates for the RGBA space. By setting the 3rd row and 3rd column to 0.3f we achieve a level of opacity. The result is a watermark which slightly shows the underlying image.
With both color manipulations added to the imageAttributes object we can now draw the watermark in the upper right hand corner of the photograph. We will offset the image 10 pixels down and 10 pixels to the left.
That's it! Compile the project, run it, and see what happens! The code is fairly straightforward if it all makes sense then these techniques can be used for 100's of different image manipulations. The possibilities are endless.
Joel is a Software Engineer with a Fortune 500 media company. He started programming in 1996 as a consultant and has spent most of his time involved in Web Application Development (IIS, ASP, VB). His latest projects have focused on C# and .NET where he has written various API and Console based applications for use in the media industry.
In his free time, he enjoys Mountain Biking through out Arizona and spending time with his wife and two dogs.
Joel是Fortune 500媒体公司的软件工程师。他在1996年作为一个顾问开始编程，大多数时间花在了Web Application Development (IIS, ASP, VB).他最新的项目集中在C#和.NET,他已经使用C#和.NET为多媒体工业写了各种各样的 API和控制台程序。