본문 바로가기

CSharp/Windows

Text & Image Watermark with C# (이미지 워터마크 만들기)

 

C#으로다가 이미지, 혹은 텍스트 워터마크 만들기
파일 다이알 로그로다가 워터 마크 이미지 받아서.

클립보드에 들어 있는 이미지에 워터 마크 삽입..

항상 컴퓨터에 상주시켜 놓고 복사할때마다 워터마크 자동으로 넣어주는 프로그램을 만들고 싶으나.

귀찮아서 패스~

ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ

아~ 개으름뱅이 

 

try
{
	OpenFileDialog ofd = new OpenFileDialog();
	ofd.Filter = "Images (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
	ofd.Multiselect = false;
	ofd.RestoreDirectory = true;
	ofd.CheckFileExists = true;
	ofd.CheckPathExists = true;
	ofd.InitialDirectory = @"D:\Angeleyes\SlrClub Images Template";
	DialogResult di = ofd.ShowDialog();
	if (di.Equals(DialogResult.OK))
	{
		//System.IO.Stream st = ofd.OpenFile();
		//Bitmap bit = new Bitmap(st);

		Image img = Clipboard.GetImage();
		if (img != null)
		{
			//Graphics gra = Graphics.FromImage(img);
			//Image watermark = Image.FromStream(ofd.OpenFile());
			//Bitmap logo = new Bitmap(watermark);

			//logo.SetResolution(img.HorizontalResolution, img.VerticalResolution);

			//gra.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
			//gra.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.GammaCorrected;
			//gra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
			//gra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
			//gra.DrawImage(logo, new Point(70, 70));

			////img.Save(@"C:\test.jpg");
			//pictureBox1.Image = img;

			string Copyright = "Copyright � 2011 - Angeleyes.kr";

			Image imgPhoto = Clipboard.GetImage();
			int phWidth = imgPhoto.Width; int phHeight = imgPhoto.Height;

			using (Bitmap bmPhoto = new Bitmap(phWidth, phHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
			{
				bmPhoto.SetResolution(72, 72);

				using (Graphics grPhoto = Graphics.FromImage(bmPhoto))
				{
					Image imgWatermark = new Bitmap(Image.FromStream(ofd.OpenFile()));
					int wmWidth = imgWatermark.Width;
					int wmHeight = imgWatermark.Height;

					grPhoto.DrawImage(
						imgPhoto,
						new Rectangle(0, 0, phWidth, phHeight),
						0,
						0,
						phWidth,
						phHeight,
						GraphicsUnit.Pixel);

					#region // Copyright //
					int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
					Font crFont = null;
					SizeF crSize = new SizeF();
					for (int i = 0; i < 7; i++)
					{
						crFont = new Font("arial", sizes[i], FontStyle.Regular);
						crSize = grPhoto.MeasureString(Copyright, crFont);

						if ((ushort)crSize.Width < (ushort)phWidth)
							break;
					}

					int yPixlesFromBottom = (int)(phHeight * .05);
					float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));
					float xCenterOfImg = (phWidth / 2);

					StringFormat StrFormat = new StringFormat();
					StrFormat.Alignment = StringAlignment.Center;

					SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));

					grPhoto.DrawString(Copyright,
						crFont,
						semiTransBrush2,
						new PointF(xCenterOfImg + 1, yPosFromBottom + 1),
						StrFormat);

					SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));

					grPhoto.DrawString(Copyright,
						crFont,
						semiTransBrush,
						new PointF(xCenterOfImg, yPosFromBottom),
						StrFormat); 
					#endregion

					Bitmap bmWatermark = new Bitmap(bmPhoto);
					bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

					Graphics grWatermark = Graphics.FromImage(bmWatermark);

					System.Drawing.Imaging.ImageAttributes imageAttributes =
							new System.Drawing.Imaging.ImageAttributes();
					System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();

					colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
					colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
					System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };

					imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);

					float[][] colorMatrixElements = {
												new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f}
												,new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f}
												,new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f}
												,new float[] {0.0f,  0.0f,  0.0f,  0.3f, 0.0f}
												,new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}
											};

					System.Drawing.Imaging.ColorMatrix wmColorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);

					imageAttributes.SetColorMatrix(wmColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);

					int xPosOfWm = ((phWidth - wmWidth) - 10);
					int yPosOfWm = 10;

					grWatermark.DrawImage(imgWatermark,
						new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight),
						0,
						0,
						wmWidth,
						wmHeight,
						GraphicsUnit.Pixel,
						imageAttributes);

					imgPhoto = bmWatermark;
				}
			}

			pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
			pictureBox1.Image = imgPhoto;
			Clipboard.SetImage(imgPhoto);

			img.Dispose();
			imgPhoto.Dispose();
		}
		else
			MessageBox.Show("img Null");
	}
	else
		MessageBox.Show(di.ToString());
}
catch (Exception ex)
{
	MessageBox.Show(ex.ToString());
}