Click here to Skip to main content
15,892,927 members
Articles / Multimedia / GDI+

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 97.1K   2.4K   51  
Image Protector - Merge an image with a transparent image with copyright, in 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;

namespace CIProtectImage
{
	/// <summary>
	/// Descripci�n breve de Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.ListBox listFilesToProtect;
		private System.Windows.Forms.TextBox txtFileProtector;
		private System.Windows.Forms.Button btnFileProtector;
		private System.Windows.Forms.Button btnFilesToProtect;
		private System.Windows.Forms.OpenFileDialog openFileDialog2;
		private System.Windows.Forms.Button btnLoad2;
		private object temporal;
		/// <summary>
		/// Variable del dise�ador requerida.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Necesario para admitir el Dise�ador de Windows Forms
			//
			InitializeComponent();

			//
			// TODO: agregar c�digo de constructor despu�s de llamar a InitializeComponent
			//
		}

		/// <summary>
		/// Limpiar los recursos que se est�n utilizando.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region C�digo generado por el Dise�ador de Windows Forms
		/// <summary>
		/// M�todo necesario para admitir el Dise�ador. No se puede modificar
		/// el contenido del m�todo con el editor de c�digo.
		/// </summary>
		private void InitializeComponent()
		{
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.listFilesToProtect = new System.Windows.Forms.ListBox();
			this.txtFileProtector = new System.Windows.Forms.TextBox();
			this.btnFileProtector = new System.Windows.Forms.Button();
			this.btnFilesToProtect = new System.Windows.Forms.Button();
			this.openFileDialog2 = new System.Windows.Forms.OpenFileDialog();
			this.btnLoad2 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.Filter = "Fireworks Image|*.png|Gif Image|*.gif";
			// 
			// listFilesToProtect
			// 
			this.listFilesToProtect.HorizontalScrollbar = true;
			this.listFilesToProtect.Location = new System.Drawing.Point(8, 70);
			this.listFilesToProtect.Name = "listFilesToProtect";
			this.listFilesToProtect.Size = new System.Drawing.Size(416, 108);
			this.listFilesToProtect.TabIndex = 1;
			// 
			// txtFileProtector
			// 
			this.txtFileProtector.Location = new System.Drawing.Point(8, 8);
			this.txtFileProtector.Name = "txtFileProtector";
			this.txtFileProtector.Size = new System.Drawing.Size(328, 20);
			this.txtFileProtector.TabIndex = 2;
			this.txtFileProtector.Text = "";
			// 
			// btnFileProtector
			// 
			this.btnFileProtector.Location = new System.Drawing.Point(344, 8);
			this.btnFileProtector.Name = "btnFileProtector";
			this.btnFileProtector.Size = new System.Drawing.Size(80, 23);
			this.btnFileProtector.TabIndex = 3;
			this.btnFileProtector.Text = "File";
			this.btnFileProtector.Click += new System.EventHandler(this.btnFileProtector_Click);
			// 
			// btnFilesToProtect
			// 
			this.btnFilesToProtect.Location = new System.Drawing.Point(8, 40);
			this.btnFilesToProtect.Name = "btnFilesToProtect";
			this.btnFilesToProtect.Size = new System.Drawing.Size(416, 24);
			this.btnFilesToProtect.TabIndex = 3;
			this.btnFilesToProtect.Text = "Files";
			this.btnFilesToProtect.Click += new System.EventHandler(this.btnFilesToProtect_Click);
			// 
			// openFileDialog2
			// 
			this.openFileDialog2.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
			this.openFileDialog2.Multiselect = true;
			// 
			// btnLoad2
			// 
			this.btnLoad2.Location = new System.Drawing.Point(8, 184);
			this.btnLoad2.Name = "btnLoad2";
			this.btnLoad2.Size = new System.Drawing.Size(416, 23);
			this.btnLoad2.TabIndex = 4;
			this.btnLoad2.Text = "Go!";
			this.btnLoad2.Click += new System.EventHandler(this.btnLoad2_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(432, 210);
			this.Controls.Add(this.btnLoad2);
			this.Controls.Add(this.btnFileProtector);
			this.Controls.Add(this.txtFileProtector);
			this.Controls.Add(this.listFilesToProtect);
			this.Controls.Add(this.btnFilesToProtect);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Name = "Form1";
			this.Text = "Image Protector";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Punto de entrada principal de la aplicaci�n.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
		private void MergeImages(string ImageBack,string ImageFore)
		{
			try
			{
			System.Drawing.Graphics myGraphic = null;

			Image imgB;// =new Image.FromFile(ImageBack);
			imgB = Image.FromFile(ImageBack);
			Image imgF;// =new Image.FromFile(ImageBack);
			imgF = Image.FromFile(ImageFore);
			Image m;
				m = Image.FromFile(ImageBack);
			myGraphic = System.Drawing.Graphics.FromImage(m);
			myGraphic.DrawImageUnscaled(imgB,0,0);
			myGraphic.DrawImageUnscaled(imgF,0,0);
			myGraphic.Save();
				
			
			m.Save(ImageBack.Replace(".jpg",".jpeg"),System.Drawing.Imaging.ImageFormat.Jpeg);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}

		}

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

		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]);
					}
				}
			}
		}

		private void btnLoad2_Click(object sender, System.EventArgs e)
		{
			if (listFilesToProtect.Items.Count>0)
			{
				foreach(object it in listFilesToProtect.Items)
				{
					MergeImages(it.ToString(),txtFileProtector.Text);
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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