www.webdeveloper.com
Results 1 to 1 of 1

Thread: Watermarking a saved image

  1. #1
    Join Date
    Oct 2008
    Posts
    3

    Question Watermarking a saved image

    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

    Code:
    /*
    * 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);
    }
    }
    Last edited by bathJmatt; 04-02-2009 at 06:27 PM. Reason: Misspelling

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles