#region Using directives

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;

#endregion

namespace IIS7Demo //Rename demo_namespace
{
    public class ImageCopyrightHandler : IHttpHandler  //Rename demo_Handler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (!System.Web.Security.Roles.IsUserInRole("Familie"))
            {
                HandleImage(context);
            }
            else context.Response.WriteFile(context.Request.PhysicalPath);
        }

        void HandleImage(HttpContext context)
        {
            try
            {

                //Get bitmap from request information
                string strPath = context.Request.PhysicalPath;
                Bitmap bitBitmap;
                bitBitmap = new Bitmap(strPath);


                //Add copyright message to bitmap
                Graphics g = Graphics.FromImage(bitBitmap);
                int fontSize = 40;
                Font f = new Font("Arial", fontSize, GraphicsUnit.Pixel);
                string copyrightText = "© 2006 Jonas Follesų Pics. All rights reserved.";

                while ((fontSize > 1) && (g.MeasureString(copyrightText, f).Width > (float)bitBitmap.Width))
                {
                    fontSize--;
                    f = new Font("Arial", fontSize, GraphicsUnit.Pixel);
                }

                SolidBrush sb = new SolidBrush(Color.FromName("Yellow"));

                g.DrawString(copyrightText,
                                f,
                                sb,
                                5,
                                bitBitmap.Height - f.Height - 5);

                f.Dispose();
                g.Dispose();


                //Save bitmap into response
                Bitmap newbitBitmap = new Bitmap((Image)bitBitmap);
                context.Response.ContentType = "image/jpeg";
                newbitBitmap.Save(
                                    context.Response.OutputStream,
                                    System.Drawing.Imaging.ImageFormat.Jpeg
                                 );
                newbitBitmap.Dispose();

            }
            catch (Exception e)
            {
                context.Response.Write(e.Message);
            }
        }


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