locked
Text over image RRS feed

  • Question

  • User1864004540 posted
    Is there anyway of putting text on a image. To take for an example putting a copyright notice on a image on website so when people view the image they can see the copyright as if it was in the picture.
    Wednesday, October 5, 2005 9:52 AM

All replies

  • User704942467 posted
    This is code that we use to write text on an image.  This is a method inside a HttpHandler that serves the image out.  If you need anymore help, let me know.  The bolded line is the method that actually writes the text on on the image.

    private static void MarkupAndSendImage( System.Drawing.Image image, HttpContext context )
    {
        using( System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( image ) )
        {
            System.Drawing.Font font = new Font("Univers 45 Light", 30, FontStyle.Regular );
            System.Drawing.Brush brush = new SolidBrush( Color.White );

            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            g.DrawString( context.Request["Text"], font, brush, 118, 8, StringFormat.GenericTypographic );

            context.Response.Cache.SetCacheability( HttpCacheability.Public );
            context.Response.Cache.SetExpires( DateTime.Now.AddHours( 10 ) );
            context.Response.Cache.VaryByParams["Text"] = true;

            image.Save( context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg );
        }
    }

    bill

    Wednesday, October 5, 2005 2:00 PM
  • User1864004540 posted
    Many thanks for this. It seems exactly as I want.

    One question though, how do it relate the text I want to display on the image to the method. I can see the image is passed in and HttpContext context, do i set the text to this or what?

    Thanks for your help.
    Thursday, October 6, 2005 4:52 PM
  • User704942467 posted
    I am passing the text through a querystring value.  This just writes the pagetitle on the image.  On my aspx page I have this as my img tag.

    <img src="WebHeaderImage.axd?Text=Main%20Start%20Page />

    The text: Main Start Page would get written out ontop of the image.

    context.Request[""] will actually look at the querystring and posted values, but I don't remember the order.

    bill
    Thursday, October 6, 2005 8:50 PM
  • User1864004540 posted
    I am passing the text through a querystring value.  This just writes the pagetitle on the image.  On my aspx page I have this as my img tag.

    <img src="WebHeaderImage.axd?Text=Main%20Start%20Page />

    The text: Main Start Page would get written out ontop of the image.

    context.Request[""] will actually look at the querystring and posted values, but I don't remember the order.

    bill


    Would it be possible to show me the method call to private static void MarkupAndSendImage( System.Drawing.Image image, HttpContext context ) as I am still confused how I manage HttpContext context.

    Thanks for all your help.

    Tuesday, October 11, 2005 6:02 AM
  • User704942467 posted
    Here is most of the code for my http handler that you will have to create.  I stripped it down to its bare essentials.

    public class HeaderImageGenerator : IHttpHandler
    {
        public void ProcessRequest( HttpContext context )
        {
           //the web.config holds the path reference to
           string file = ConfigurationSettings.AppSettings["BlankHeaderFile"];

           //this will open the image and get an Image object to pass to the method.
           //the context is passed in by the framework to the ProcessRequest method.
           using( System.Drawing.Image image = System.DrawingImage.FromFile( file ) )
           {
              MarkupAndSendImage( image, context );
           }
        }

        public bool IsReusable
        {
           get { return true; }
        }
    }


    Tuesday, October 11, 2005 2:07 PM