I am getting a web.config file error in sharepoint trying to add an ihttphandler
I also added to my safecontrols
<SafeControl Assembly="MY.RNET.ImageHan
dler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=38c82c65bfb
6cec0" TypeName="*" Safe="True" />
<httpHandlers>
<remove verb="GET,HEAD,POST" path="*" />
<add verb="GET" type="MY.RNET.ImageHandler
, Version=1.0.0.0, Culture=neutral, PublicKeyToken=38c82c65bfb
6cec0" path="*.jpg,*.png,*.gif,*.
bmp"/>
<add verb="GET,HEAD,POST" path="*" type="Microsoft.SharePoint
.Applicati
onRuntime.
SPHttpHand
ler, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e
9429c" />
Here is my code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.
WebParts;
using System.Web.UI.HtmlControls
;
using System.Net.Mime;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace MY.RNET.ImageHandler
{
class ImageHandler : IHttpHandler
{
public ImageHandler()
{
}
public string GetContentType(String path)
{
switch (Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: break;
}
return String.Empty;
}
public ImageFormat GetImageFormat(String path)
{
switch (Path.GetExtension(path).T
oLower())
{
case ".bmp": return ImageFormat.Bmp;
case ".gif": return ImageFormat.Gif;
case ".jpg": return ImageFormat.Jpeg;
case ".png": return ImageFormat.Png;
default: return null;
}
}
protected byte[] WatermarkImage(HttpContext
context)
{
byte[] imageBytes = null;
if (File.Exists(context.Reque
st.Physica
lPath))
{
// Normally you'd put this in a config file somewhere.
string watermark = "John Doe - © EXAMPLE Company 2007";
Image image = Image.FromFile(context.Req
uest.Physi
calPath);
Graphics graphic;
if (image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppInde
xed && image.PixelFormat != PixelFormat.Format4bppInde
xed && image.PixelFormat != PixelFormat.Format1bppInde
xed)
{
// Graphic is not a Indexed (GIF) image
graphic = Graphics.FromImage(image);
}
else
{
/* Cannot create a graphics object from an indexed (GIF) image.
* So we're going to copy the image into a new bitmap so
* we can work with it. */
Bitmap indexedImage = new Bitmap(image);
graphic = Graphics.FromImage(indexed
Image);
// Draw the contents of the original bitmap onto the new bitmap.
graphic.DrawImage(image, 0, 0, image.Width, image.Height);
image = indexedImage;
}
graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;
Font myFont = new Font("Arial", 15);
SolidBrush brush = new SolidBrush(Color.FromArgb(
80, Color.White));
/* This gets the size of the graphic so we can determine
* the loop counts and placement of the watermarked text. */
SizeF textSize = graphic.MeasureString(wate
rmark, myFont);
// Write the text across the image.
for (int y = 0; y < image.Height; y++)
{
for (int x = 0; x < image.Width; x++)
{
PointF pointF = new PointF(x, y);
graphic.DrawString(waterma
rk, myFont, brush, pointF);
x += Convert.ToInt32(textSize.W
idth);
}
y += Convert.ToInt32(textSize.H
eight);
}
using (MemoryStream memoryStream = new MemoryStream())
{
image.Save(memoryStream, GetImageFormat(context.Req
uest.Physi
calPath));
imageBytes = memoryStream.ToArray();
}
}
return imageBytes;
}
#region IHttpHandler Members
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext
context)
{
context.Response.Clear();
context.Response.ContentTy
pe = GetContentType(context.Req
uest.Physi
calPath);
byte[] imageBytes = WatermarkImage(context);
if (imageBytes != null)
{
context.Response.OutputStr
eam.Write(
imageBytes
, 0, imageBytes.Length);
}
else
{
// No bytes = no image which equals NO FILE. :)
// Therefore send a 404 - not found response.
context.Response.StatusCod
e = 404;
}
context.Response.End();
}
#endregion
}
}