Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, October 15, 2009 8:16 AM
Hello !
i try to mark my Images with another Image (watermark image). Now i stuck with the main function of my tool - the watermarking.
here is what i got:
public void Watermarking(string workingDir)
{
//set a working directory
string m_WorkingDirectory = workingDir;
try
{
//create a image object containing the photograph to watermark
foreach (string imgPath in m_AllePhotoPfade)
{
//Image imgPhoto = Image.FromFile(m_WorkingDirectory + "\\watermark_photo.jpg");
Image imgPhoto = Image.FromFile(imgPath);
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
//create a Bitmap the Size of the original photograph
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//load the Bitmap into a Graphics object
Graphics grPhoto = Graphics.FromImage(bmPhoto);
//create a image object containing the watermark
//Image imgWatermark = new Bitmap(m_WorkingDirectory + "\\watermark.jpg");
Image imgWatermark = new Bitmap(m_WaterMarkImage);
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;
float opac = 0;
string sOpacity = cboOpacity.Text;
// Determine the opacity of the watermark
switch (sOpacity)
{
case "100%":
opac = 255; // 1 * 255
break;
case "75%":
opac = 191; // .75 * 255
break;
case "50%":
opac = 127; // .5 * 255
break;
case "25%":
opac = 64; // .25 * 255
break;
case "10%":
opac = 25; // .10 * 255
break;
default:
opac = 127; // default at 50%; .5 * 255
break;
}
int xPosOfWm = 0;
int yPosOfWm = 0;
if (optBottom.Checked && optLeft.Checked)
{
xPosOfWm = 10;
yPosOfWm = (phHeight - wmHeight) - 10;
}
if(optBottom.Checked && optRight.Checked)
{
xPosOfWm = ((phWidth - wmWidth) - 10);
yPosOfWm = (phHeight - wmHeight) - 10;
}
if(optTop.Checked && optLeft.Checked)
{
xPosOfWm = 10;
yPosOfWm = 10;
}
if(optTop.Checked && optRight.Checked)
{
xPosOfWm = ((phWidth - wmWidth) - 10);
yPosOfWm = 10;
}
#region MehrPositionen
//case ContentAlignment.BottomCenter:
// xPosOfWm = (phWidth/2) - (wmWidth/2);
// yPosOfWm = (phHeight - wmHeight) - 10;
// break;
//case ContentAlignment.MiddleCenter:
// xPosOfWm = (phWidth/2) - (wmWidth/2);
// yPosOfWm = (phHeight/2) - (wmHeight/2);
// break;
//case ContentAlignment.MiddleLeft:
// xPosOfWm = 10;
// yPosOfWm = (phHeight/2) - (wmHeight/2);
// break;
//case ContentAlignment.MiddleRight:
// xPosOfWm = ((phWidth - wmWidth)-10);
// yPosOfWm = (phHeight/2) - (wmHeight/2);
// break;
//case ContentAlignment.TopCenter:
// xPosOfWm = (phWidth/2) - (wmWidth/2);
// yPosOfWm = 10;
// break;
#endregion
//Set Opacity of Watermark
Image watermark = SetImgOpacity(imgWatermark, opac);
InsertTextCopyright(grPhoto, imgPhoto, phWidth, phHeight);
InsertWatermarkImage(bmPhoto, imgPhoto, phWidth, phHeight, wmWidth, wmHeight, watermark, grPhoto, m_WorkingDirectory);
MessageBox.Show("Alle Bilder mit Wasserzeichen wurden in das Verzeichnis: \r\n\r\n"
+"- " + workingDir + "\r\n\r\n gespeichert.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
as you can see i try also to set the oppacity of the image (search for "switch(sOpacity)" ). but now i stuck with this part - i try to create a new Image (watermark) and fill it with the result of eht Method "SetImageOpacity(imgWatermark, opac)".
now i dont know what is wrong - i guess is somekind with the flow in my software. so, it wont watermark my image with the watermark image.
Here is the Method "SetImgOpacity()" which i get from somewhere of the internet:
public
static
Image SetImgOpacity(Image imgPic, float
imgOpac)
{
Bitmap bmpPic = new
Bitmap(imgPic.Width, imgPic.Height);
Graphics gfxPic = Graphics.FromImage(bmpPic);
ColorMatrix cmxPic = new
ColorMatrix();
cmxPic.Matrix33 = imgOpac;
ImageAttributes iaPic = new
ImageAttributes();
iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
gfxPic.DrawImage(imgPic, new
Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic);
gfxPic.Dispose();
return
bmpPic;
}
Thanks for Help in any way !
regards,
streezer
All replies (5)
Saturday, October 17, 2009 5:30 PM ✅Answered | 1 vote
You need to divide the imgOpac argument by 255. ColorMatrix coefficients are normally 0..1
Hans Passant.
Saturday, October 17, 2009 5:46 PM ✅Answered | 2 votes
Use the TextureBrush class.
Edit: Forget the TextureBrush. It doesn't make watermarking any easier.
Strat a new Windows Forms application and replace the Form1 code with this:
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private PictureBox PicBox = new PictureBox();
public Form1()
{
InitializeComponent();
PicBox.Parent = this;
PicBox.Dock = DockStyle.Fill;
PicBox.SizeMode = PictureBoxSizeMode.Zoom;
Bitmap Jpg = new Bitmap("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert Landscape.jpg");
using (Bitmap Bmp = new Bitmap("C:\\Users\\Public\\Pictures\\Sample Pictures\\Dock.jpg"))
{
using (Bitmap WatermarkBmp = new Bitmap(Bmp, Bmp.Width / 4, Bmp.Height / 4))
{
PicBox.Image = WatermarkImage(Jpg, WatermarkBmp, new Point(400, 100), 0.5F);
}
}
}
public Bitmap WatermarkImage(Bitmap ImageToWatermark, Bitmap Watermark, Point WatermarkPosition, float Opacity)
{
using (Graphics G = Graphics.FromImage(ImageToWatermark))
{
using (ImageAttributes IA = new ImageAttributes())
{
ColorMatrix CM = new ColorMatrix();
CM.Matrix33 = Opacity;
IA.SetColorMatrix(CM);
G.DrawImage(Watermark, new Rectangle(WatermarkPosition, Watermark.Size), 0, 0, Watermark.Width, Watermark.Height, GraphicsUnit.Pixel, IA);
}
}
return ImageToWatermark;
}
}
}
It should work without modification on Vista. Select different pictures on XP.
Saturday, October 17, 2009 4:14 PM | 2 votes
Hi Streezer,
Here is the sample windows application for you:
http://www.codeproject.com/KB/GDI-plus/watermarker.aspx
Using GDI and .NET:
http://www.codeproject.com/KB/GDI-plus/watermark.aspx
Regards,
Jai
Monday, October 19, 2009 12:03 PM
thank you for the links - one project i've already known - but the other helped me a lot, escpecially with the position of the watermark image.
Thank you also for the code snippet - i try to insert all the informations now in my project...
it's very complex to do image editing in c# - so my opinion ;)
i'll write again if there's something new...
Friday, October 30, 2009 2:05 PM
hey there,
I had no problems with the method from JohnWein but now i get one: "not valid paraemter".
i get this exception if i call the method like that:
Bitmap bmpWatermark = (Bitmap)imgWatermark;
Bitmap bmpImgPath = (Bitmap)Image.FromFile(imgPath);
completeImageWithWatermark = WatermarkImage(bmpImgPath, bmpWatermark, newPoint, opac);
the first image is getting watermarked - but then at the second one i get this message without meaning "not valid parameter".
anyone can help me solving that? any tipps?