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!
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.
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
private void btnFileProtector_Click(object sender, System.EventArgs e)
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 "
private void btnFilesToProtect_Click(object sender, System.EventArgs e)
Now we need to use some assemblies in the
using section. Add:
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
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;
imgB = Image.FromFile(ImageBack);
imgF = Image.FromFile(ImageFore);
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 "
Image the first or unscaled background image starting in position zero (
0x,0y) , and immediately draw the second in the "
Image too. Then save it.
|Original image to protect
||Copyright image with transparency
myGraphic = System.Drawing.Graphics.FromImage(m);
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).
The image result looks like this:
Using this, you can protect a lot of images in different directories with just a push of a button. Enjoy!
- 25th July, 2005: Initial post