Using vb.net code to realize image watermarking technology

Source: Internet
Author: User

using vb.net code to realize image watermarking technology Author: Builder.com.cn
2006-11-23 10:5 AM

Watermark Technology is a useful technology for trademark and copyright image protection, this technology can be realized dynamically with only a small piece of code.

This article explains how to create a watermark class (watermark) that uses an image or text to create a watermark map on the original image. This article provides a watermark of the vb.net application and its complete source code and class files, you can run the program to better understand the image watermarking technology.

Image watermarking Technology is a technique that uses text or an image to embed another image, which transforms the original artwork into a overlay graph. Because the watermark is a very difficult thing to remove from the overlay, it needs good manual image editing ability, so the watermark technique is usually used for trademark or copyright protection. Unfortunately, Microsoft's. NET Framework does not contain any built-in features for dynamically running image watermarks.

To make up for this shortcoming, we constructed a class to achieve the function of creating a watermark on the original image using a picture or text. Figures A and B show an example of two watermark graphs, in which the watermark embedded in figure A is text and the image B is embedded in a picture.

Figure A

Figure B

You will notice that the watermark does not have a noticeable effect on the dark areas of the image. For darker images, embedding a text halo watermark may be better. For information and source code to create a halo, see "Add a halo to your text using the vb.net code."  

Watermark Technology

The Watermark class contains a number of properties that can be used to determine where the watermark is embedded in the original image, which type of watermark (text or image) is used, and the precise information of the watermark. First we need to define a tool class (utility) to enumerate the type and location information of the watermark (see Table A).

After defining the utility class, we can construct our Watermark Class (watermark). For text-type watermarks, we need to clearly use what text and fonts to create this watermark. For image type watermarks, you need to know the file name of the image used for the watermark. For watermarks that contain both text and image types, you need to know the file name of the original image file and the location of the watermark.

Because all the required information is already set in these properties, the main function can create a watermark image without any additional information. The program returns an instance of the System.Drawing.Bitmap type.

The Watermark program runs very well in a multi-threaded environment. Programs based on the original image and watermark can consume a lot of CPU uptime. Therefore, it may be better to put it in the background and place the main program in the foreground, which is especially important when the watermark program is part of a Web site. For example, if the site allows users to upload pictures that they want to watermark, we need to continue to run the rest of the page for the user to use, while detecting to make sure that the watermark image has been created.

The process of watermark is fairly simple. First, we create an overlay with the same size as the original, and embed the watermark where needed. Watermarks can be loaded from a picture, or you can create the font and text you want dynamically. Next, check the overlay, search for the color of each pixel, and show its transparency. When we find the color of a pixel, we use its brightness value to create a multiplier.

This multiplier system may slow down the operation, but it can make the watermark image adjust accordingly with the change of original image color. At the same time, by editing the source code or providing a multiplier, we can set the color corresponding to its original color on the same pixel point as the original image, but the red, green, and blue components of the color are multiplied by the previous factor. Because this coefficient is a number between 0-1, it will reduce the saturation of the pixel color, so that it renders the effect of the watermark. List B contains the main processing code for the watermark technology process.

The following code is noteworthy, which is used when creating a pixel multiplier.

Pixelmultiplier = 1-csng ((1-watermarkpixel.getbrightness)) * 0.25)

Changing the final value (0.25) in this line of code can adjust the watermark's strength. Multiplied by 0.25, the saturation of the watermark is limited to the original 0%-25%. In other words, if the watermark image color is dim, the effect of the watermark will be less obvious, the darkest color can only show the intensity of the original color 25%. This value should not be less than 0.0 (the display does not print effect) nor higher than 1.0 (so that where the watermark will appear white).

By testing, we found that keeping the value between 0.2-0.3 was best. Of course, we can set it to a constant value or modify the code to set its size dynamically at run time.  

Test Watermark Technology Application

< param value= "0" name= "profileport" >

The watermark program can be used for dynamic Web pages, or for editing images to achieve business purposes. By using a watermark Class (watermark), we can easily integrate the watermark technology into our application system. A watermark image can be generated by downloading and installing the entire application. The application installs class files that also contain all source code and all image watermarks for the application. We can easily use this code, integrate them into our own engineering files, and modify it to make it more consistent with our needs.

Listing B

    Private Function Addwatermark (ByVal originalimage as Bitmap, ByVal watermarkimage as Bitmap, ByVal Location as Imagewa
        Termarkingutilities.watermarklocation) as Bitmap Dim outputimage As Bitmap Dim watermarkoverlay As Bitmap Dim overlayeditor As Graphics Dim startingx As Integer Dim startingy As Integer Dim EndX A
        s integer Dim EndY As Integer Dim watermarkwidth As Integer Dim watermarkheight As Integer
        Dim X As Integer Dim Y As Integer Dim watermarkpixel As Color Dim originalpixel As Color Dim newpixel As Color Dim pixelmultiplier as single If watermarkimage.width > Originalimage.width Or W Atermarkimage.height > Originalimage.height Then Throw New Exception ("The watermark must be smaller than th
        E original image. ") End If outputimage = new Bitmap (CType (Originalimage.clone (), Bitmap)) Watermarkoverlay = new Bitmap (OUtputimage.width, outputimage.height) Overlayeditor = Graphics.fromimage (watermarkoverlay) WatermarkWidth = Watermarkimage.width Watermarkheight = watermarkimage.height Select case Location Case imag EWatermarkingUtilities.WatermarkLocation.TopLeft startingx = 0 startingy = 0 C ASE ImageWatermarkingUtilities.WatermarkLocation.TopCenter STARTINGX = Math.max (CInt (Outputimage.width/ 2)-(WATERMARKWIDTH/2)), 0 startingy = 0 Case Imagewatermarkingutilities.watermarklocation .
            TopRight startingx = Math.max (CInt (Outputimage.width-watermarkwidth), 0) startingy = 0 Case ImageWatermarkingUtilities.WatermarkLocation.MiddleLeft startingx = 0 Star Tingy = Math.max (CInt (OUTPUTIMAGE.HEIGHT/2)-(WATERMARKHEIGHT/2)), 0 case Imagewatermarkingutilities.wa Termarklocation.middlecEnter STARTINGX = Math.max (CInt (OUTPUTIMAGE.WIDTH/2)-(WATERMARKWIDTH/2)), 0 Starti Ngy = Math.max (CInt (OUTPUTIMAGE.HEIGHT/2)-(WATERMARKHEIGHT/2)), 0 case Imagewatermarkingutilities.wate
                Rmarklocation.middleright startingx = Math.max (CInt (Outputimage.width-watermarkwidth), 0) Startingy = Math.max (CInt (OUTPUTIMAGE.HEIGHT/2)-(WATERMARKHEIGHT/2)), 0 case Imagewatermarkingutilit ies. Watermarklocation.bottomleft startingx = 0 startingy = Math.max (CInt (Outputimage.height- Watermarkheight), 0 case ImageWatermarkingUtilities.WatermarkLocation.BottomCenter Startingx = Math.max (CInt (OUTPUTIMAGE.WIDTH/2)-(WATERMARKWIDTH/2)), 0) startingy = Math.max (CInt (outputimage. Height-watermarkheight), 0 case ImageWatermarkingUtilities.WatermarkLocation.BottomRight St ARTINGX = Math.max (CInt (Outputimage.width-watermarkwidth), 0) startingy = Math.max (CInt (outputimage.height-watermarkheight
        ), 0) End Select startingx = Math.max (StartingX-1, 0) startingy = Math.max (StartingY-1, 0) EndX = Startingx + WatermarkWidth-1 EndY = startingy + WatermarkHeight-1 Overlayeditor.drawimag
                E (Watermarkimage, STARTINGX, startingy) for X = Startingx to EndX for Y = Startingy to EndY
                    Watermarkpixel = Watermarkoverlay.getpixel (X, Y) If watermarkpixel.a > 0 Then Originalpixel = Outputimage.getpixel (X, Y) Pixelmultiplier = 1-csng ((1-watermarkpixel.getbrigh tness)) * 0.25) Newpixel = Color.FromArgb (ORIGINALPIXEL.A, CInt (ORIGINALPIXEL.R * pixelmultiplier), CI NT (ORIGINALPIXEL.G * pixelmultiplier), CInt (ORIGINALPIXEL.B * pixelmultiplier)) Outputimage.setpixel (X
         , Y, Newpixel)       End If Next Next watermarkimage = no originalimage = Nothing wate Rmarkoverlay = Nothing Overlayeditor = no return outputimage end Function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.