Click here to Skip to main content
15,884,425 members
Articles / Multimedia / GDI+
Article

Image Protector - Merge an Image with a Transparent Image with Copyright Programmed in C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (14 votes)
25 Jul 2005CPOL3 min read 97K   2.4K   51   11
Image Protector - Merge an image with a transparent image with copyright, in C#
Sample screenshot

Introduction

If you have a site with a lot of images to upload, like an album or something like that, you need to protect your images before upload. You can do it with Fireworks or Photoshop and it is easy. But if you have hundreds of photos or images to protect, Photoshop or Fireworks is an option that is not so fast.

Here is a demo of how to protect your images with an easy program coded in C# to protect all your images in one click!

Objective

The objective of this demo is just to let the code merge two images and an example of a very useful thing (protect a lot of photos before upload) to webmasters, designers or developers.

The Code

We need to load two types of images, the protection transparent image and some images to protect.

To load, use the openfiledialog and assign the path in a textbox:

C#
private void btnFileProtector_Click(object sender, System.EventArgs e)
{
 if (openFileDialog1.ShowDialog()==DialogResult.OK)
 {
  txtFileProtector.Text=openFileDialog1.FileName;
 }
}

To load the images, use the openfiledialog with multiple selection property enabled and fill a list, each path as an item with a "for" function from 0 to the number of selected items in the openfile dialog box.

This number is in the property "FileNames.Length".

C#
private void btnFilesToProtect_Click(object sender, System.EventArgs e)
{
 listFilesToProtect.Items.Clear();
 if (openFileDialog2.ShowDialog()==DialogResult.OK)
 {
  if (openFileDialog2.FileNames.Length>0)
  {
   for(int i=0;i<openFileDialog2.FileNames.Length;i++)
   {
    listFilesToProtect.Items.Add(openFileDialog2.FileNames[i]);
   }
  }
 }
}

Now we need to use some assemblies in the using section. Add:

C#
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

Then, declare a System.Drawing.Graphics and this is the object to manipulate image containers. You can use a picturebox to view the result or an "Image" class to save image to disk. In this case, use the Image.

Declare two instances of "Image" class and each will load an image from file, the first loads the image to protect or the background, and the second is to load the transparent image, or the image protection.

You can use a transparent GIF with a legend or a PNG. I recommend PNG because PNG is more detailed and does not show a white stroke.

Declare a 3rd instance of "Image" to use this as a container of the two merged images and to save in a file on the hard disk, and load the fifth image, or the background.

This is to get the properties of the original image, and save the modified file with the same properties than the original (size, depth, resolution, etc.).

C#
 System.Drawing.Graphics myGraphic = null;
 Image imgB;
 imgB = Image.FromFile(ImageBack);
 Image imgF;
 imgF = Image.FromFile(ImageFore);
 Image m;
 m = Image.FromFile(ImageBack);

When the images are loaded and it is time to merge, use the myGraphic instance of Graphics to draw in the "mImage the first or unscaled background image starting in position zero (0x,0y) , and immediately draw the second in the "m" Image too. Then save it.

Original image to protectCopyright image with transparency
Original image to protectCopyright Image with transparency
C#
 myGraphic = System.Drawing.Graphics.FromImage(m);
 myGraphic.DrawImageUnscaled(imgB,0,0);
 myGraphic.DrawImageUnscaled(imgF,0,0);
 myGraphic.Save();

Finally

Finally, you have an image manipulated by the Mygraphic and containing the two images, one above the other. You just need to save to disk.

In this demo, the program protects a baby image file (kung-fubaby.jpg) with a GIF file (proteccion.gif) and replaces the extension to prevent writing to the file when it is in use (by this program:) of course).

C#
m.Save(ImageBack.Replace(".jpg",".jpeg"),System.Drawing.Imaging.ImageFormat.Jpeg);

or:

C#
m.Save(ImageBack.Replace(".jpg","_copy.jpg"),System.Drawing.Imaging.ImageFormat.Jpeg);

The image result looks like this:

Sample screenshot

Using this, you can protect a lot of images in different directories with just a push of a button. Enjoy!

History

  • 25th July, 2005: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Cimar Solutions
Mexico Mexico
Ing. F. Marcelo Lujan alias El Bebe Dot Net. Hola, yo desarrollo de forma independiente en C#. ASP.NET y Win32 Diseño Macromedia etc. con mas de 10 años de experiencia en informática y soporte a sistemas, así como desarrollo de software y nuevos productos.

Espero que ayude la informacion que pongo a su disposicion.
I Hope this information that i upload to codeproject helps you.
Atte: Marcelo Lujan

Comments and Discussions

 
QuestionHi Pin
dvirz28-Jun-11 3:29
dvirz28-Jun-11 3:29 
GeneralMy vote of 4 Pin
raizor212-Apr-11 3:15
raizor212-Apr-11 3:15 
QuestionMerging two icons Pin
Toby Haddon1-Feb-09 17:34
Toby Haddon1-Feb-09 17:34 
GeneralKey image not scaled properly [modified] Pin
Member 337866713-Nov-08 11:01
Member 337866713-Nov-08 11:01 
GeneralRe: Key image not scaled properly Pin
alej_souza18-Oct-09 13:07
alej_souza18-Oct-09 13:07 
GeneralGreat Tool ! Pin
Sondre Kjell Selnes2-Mar-07 2:36
Sondre Kjell Selnes2-Mar-07 2:36 
GeneralRe: Great Tool ! Pin
kiquenet.com16-Sep-10 9:54
professionalkiquenet.com16-Sep-10 9:54 
Generalthis is only one side Pin
santosh8214-Nov-06 23:50
santosh8214-Nov-06 23:50 
QuestionGeneric Error in GDI+ Pin
FrozenSolutions25-Aug-06 4:05
FrozenSolutions25-Aug-06 4:05 
GeneralLooks like you have some more work to do ..... Pin
fwsouthern26-Jul-05 7:33
fwsouthern26-Jul-05 7:33 
GeneralRe: Looks like you have some more work to do ..... Pin
Marcelo Lujan [El Bebe.Net ]26-Jul-05 12:50
Marcelo Lujan [El Bebe.Net ]26-Jul-05 12:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.