locked
Adding watermark or text to images on uploading or displaying RRS feed

  • Question

  • User963835749 posted

    Is there a way to process images once uploaded to my asp.net site, to put a watermark or some text across the image to copyright the images? I would like to do this server-side if possible.

    The other option I have discovered here (http://wpglamour.com/how-to-watermark-all-your-uploaded-images/) would be to store the images as they are on the server, but to manipulate the display so it appears the image is watermarked . However, this solution is in PHP. Does anyone know a good PHP to VB.NET translator?! Or does anyone have a good suggested link, idea or code? Or can I add PHP to my ASP.NET site?

    Sunday, October 31, 2010 10:46 AM

Answers

  • User1154043639 posted

    We can use Graphics.DrawStringMethod(...)

    Reference:

    Example:

    <title>Snippet</title>

    <%@ Page Language="C#" %>
    <html>
    <head id="Head1" runat="server">
        <script runat="server">
            protected void Save(object sender, EventArgs e)
            {
                System.Drawing.Image image = System.Drawing.Image.FromStream(fuFile.PostedFile.InputStream);
                int w = image.Width;
                int h = image.Height;
                System.Drawing.Bitmap b = new System.Drawing.Bitmap(w, h);
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)b);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.Clear(System.Drawing.Color.Transparent);
                g.DrawImage(image, 0, 0, w, h);
                System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
                System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
                g.DrawString("Copyrights (c) 2010", drawFont, drawBrush, 0.0F, 0.0F);
                System.Drawing.Image newImage = (System.Drawing.Image)b;
                newImage.Save(Server.MapPath("~/Uploads/" + fuFile.FileName));
                g.Dispose();
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:FileUpload runat="server" ID="fuFile" />
            <asp:Button runat="server" ID="btnSave" OnClick="Save" Text="Save" />
        </form>
    </body>
    </html>


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 31, 2010 11:40 AM
  • User-1498334129 posted

    Watermark is best implemented by using HttpHandlers

    When image is retrieved from the server, you can use the System.Drawing namespace functions to add a watermark into the image. In this way, you can still have the original image, and yet add a watermark dynamically when the image is retrieved from the server. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 1, 2010 4:44 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 1, 2010 5:02 AM

All replies

  • User1154043639 posted

    We can use Graphics.DrawStringMethod(...)

    Reference:

    Example:

    <title>Snippet</title>

    <%@ Page Language="C#" %>
    <html>
    <head id="Head1" runat="server">
        <script runat="server">
            protected void Save(object sender, EventArgs e)
            {
                System.Drawing.Image image = System.Drawing.Image.FromStream(fuFile.PostedFile.InputStream);
                int w = image.Width;
                int h = image.Height;
                System.Drawing.Bitmap b = new System.Drawing.Bitmap(w, h);
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)b);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.Clear(System.Drawing.Color.Transparent);
                g.DrawImage(image, 0, 0, w, h);
                System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
                System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
                g.DrawString("Copyrights (c) 2010", drawFont, drawBrush, 0.0F, 0.0F);
                System.Drawing.Image newImage = (System.Drawing.Image)b;
                newImage.Save(Server.MapPath("~/Uploads/" + fuFile.FileName));
                g.Dispose();
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:FileUpload runat="server" ID="fuFile" />
            <asp:Button runat="server" ID="btnSave" OnClick="Save" Text="Save" />
        </form>
    </body>
    </html>


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 31, 2010 11:40 AM
  • User-1498334129 posted

    Watermark is best implemented by using HttpHandlers

    When image is retrieved from the server, you can use the System.Drawing namespace functions to add a watermark into the image. In this way, you can still have the original image, and yet add a watermark dynamically when the image is retrieved from the server. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 1, 2010 4:44 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 1, 2010 5:02 AM
  • User963835749 posted

    Thank you everyone for your responses! Not sure whether to go for the destructive or runtime options at mo, but thanks for the options!

    Monday, November 1, 2010 2:29 PM