Click here to Skip to main content
Click here to Skip to main content

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

By , 25 Jul 2005
 
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:

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

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:

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

 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 protect Copyright image with transparency
Original image to protect Copyright Image with transparency
 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).

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

or:

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)

About the Author

Marcelo Lujan [El Bebe.Net ]
Founder Cimar Solutions
Mexico Mexico
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHimemberdvirz28 Jun '11 - 3:29 
how can i determine all the sizes of the files to protect before im adding the png protector?
thanks in advance
GeneralMy vote of 4memberraizor212 Apr '11 - 3:15 
Very helpful. Thanks.
QuestionMerging two iconsmemberToby Haddon1 Feb '09 - 17:34 
Marcelo,
Do you have a code snippet that can take two icons of the same size (icon1 and icon2) and overlay icon2 over icon1 (icon1 has transparent pixels). The resulting merged image also needs to be held in an Icon class.
I need it to merge a couple of icons for use in a NotifyIcon.
Thanks
Toby
GeneralKey image not scaled properly [modified]memberMember 337866713 Nov '08 - 11:01 
I tried this code with two PNG files but the key image comes out smaller than the other image. The key is a PNG with transparent areas. Both images are the same dimension but the key image turns out smaller in the final result.
 
It can be resolved by using the DrawImage method supplying the desired image dimenstions instead of DrawImageUnscaled as in the example below.
 
int width = 800;
int height = 600;
mergeGraphics.DrawImage(imgF, 0, 0, width, height);
 
I'm curious as to why DrawImageUnscaled doesn't work. Sounds like it works for you but not for me with two PNGs. And thanks for the code!
 
modified on Thursday, November 13, 2008 6:41 PM

GeneralRe: Key image not scaled properlymemberalej_souza18 Oct '09 - 13:07 
For me works in this way:
 
Image imgB = Image.FromFile(ImageBack);
 
using (Graphics grfx = Graphics.FromImage(imgB))
{
Image imgF = Image.FromFile(ImageFore);
grfx.DrawImageUnscaledAndClipped(imgF, new Rectangle(0,0,imgF.Size.Width,imgF.Size.Height));
}
 
imgB.Save(ImageBack.Replace(".jpg", ".jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);
 

I use DrawImageUnscaledAndClipped, and works ok, thanks for the code
GeneralGreat Tool !memberSondre Kjell Selnes2 Mar '07 - 2:36 
Smile | :)
 
... and I have - 35000 - portrait photos that needs this great software of Yours.
 
Thanks !
GeneralRe: Great Tool !memberalhambra-eidos16 Sep '10 - 9:54 
Sondre, any sample code for manage many images ??
AE

Generalthis is only one sidemembersantosh8214 Nov '06 - 23:50 
hw to get the original file back.
QuestionGeneric Error in GDI+memberFrozenSolutions25 Aug '06 - 4:05 
Have you ever had any problems with GDI+ errors?
 
Chris
GeneralLooks like you have some more work to do .....memberfwsouthern26 Jul '05 - 7:33 
1. Both your source & demo downloads are the same (source, no demo)
2. Your download is missing a referenced file: FormularioValid.htm (appears that this is extraneous to your application & should be deleted)
3. Your last image ("Image protected") is missing
4. Would be nice to have a sample image set (one to use to protect -- a "Fireworks Image", a/k/a "png", and one to be protected)
5. You do not account for enlarging the form at run time -- real small area to include a large set of files to be protected
6. You might consider adding thumbnail images, before and after views, and confirmation to "protect" selected or all files

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 25 Jul 2005
Article Copyright 2005 by Marcelo Lujan [El Bebe.Net ]
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid