Click to See Complete Forum and Search --> : Watermarking a saved image


bathJmatt
04-02-2009, 05:26 PM
Hello

I'd like to watermark a saved image with a text string and, by reusing code from a 3rd party application, I've come up with this method.

However, no watermarking is performed and I don't think its possible to get the ASP.NET debugger working for a Windows Forms application, even when I set breakpoints, so I can't see what's going wrong in my form.

Can I please have 3 questions answered, as well as some tips and advice on my code which follows:

Do I need to use threading? I'm changing the file and saving it at the same time, so I think the answer is yes.
Am I using the correct library classes from the System.Drawing object
Is there a problem with using the Graphics class for this as neither DrawString() or DrawRectangle appear to do anything on my image?


Many thanks


/*
* Allows selected photo to be watermarked with "RBC Jersey" after saving
*/
private static void WatermarkPhoto(Form1 f1, string userID)
{
System.Threading.Monitor.Enter(f1);

try
{
Bitmap bmOriginal = null;
string photo = string.Format("//mscs-zgn0//SharedData/MBEWERS/Photos/{0}.jpg", userID);
bmOriginal = (Bitmap)Image.FromFile(photo);

Graphics g = Graphics.FromImage(bmOriginal);

// Create a solid brush to write the watermark text on the image
Brush myBrush = new SolidBrush(Color.FromArgb(125, Color.IndianRed));

// Calculate the size of the text
Font f = new Font("Arial", 20, FontStyle.Bold);
SizeF sz = g.MeasureString("RBC Jersey", f);

Pen p = new Pen(myBrush);

// draw the watermark text onto the image
g.DrawString("Jersey Cows", f, myBrush, new Point(0, 0));
g.DrawRectangle(p, 10, 10, 10, 100);
g.Save();

EncoderParameters encoderParams = new EncoderParameters();

encoderParams.Param[0] = new EncoderParameter
(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
ImageCodecInfo codecInfo = GetEncoderInfo("image/jpeg");

// Finally, dispose of the drawing objects
g.Dispose();
bmOriginal.Dispose();
}

catch (FileNotFoundException fe)
{
DialogResult notfound =
MessageBox.Show("Photo could not be found " + fe.Message + " ", "File not found",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}

finally
{
System.Threading.Monitor.Exit(f1);
}
}