Hey people,
I have this Custom Server Control.
"C#" Class="ShowImage" %>
using System;
using System.Web;
using System.Data.Odbc;
using System.Data;
using System.IO;
using System.Drawing;
public class ShowImage : IHttpHandler
{
const int BUFFERSIZE = 1024;
public bool IsReusable
{
get
{
return true;
}
}
public void ProcessRequest(HttpContext context)
{
Byte[] imgBytes = null;
context.Response.ContentType = "image/jpeg";
String url = Utils.Decrypt(HttpUtility.UrlDecode(context.Request.QueryString.ToString()));
String[] arguments = url.Split(new char[] { '&' });
String imagePath = arguments[0];
String width = arguments[1];
String height = arguments[2];
String cacheKey = "image=" + imagePath+";width="+width+";height="+height;
Object cachedImageBytes = context.Cache.Get(cacheKey);
if (cachedImageBytes != null)
{
imgBytes = (Byte[])cachedImageBytes;
}
else
{
context.Response.BufferOutput = false;
imgBytes = writeSingleImage(imagePath, Convert.ToInt32(width), Convert.ToInt32(height));
try
{
context.Cache.Insert(cacheKey, imgBytes, null, DateTime.Now.AddDays(14), System.Web.Caching.Cache.NoSlidingExpiration);
}
catch
{
//just ignore, dont know what happens
}
}
if (imgBytes != null)
{
context.Response.OutputStream.Write(imgBytes, 0, imgBytes.Length);
}
context.Response.End();
}
public Byte[] writeSingleImage(String ImagePath, int width, int height)
{
Byte[] returnImage = null;
Bitmap origImage;
MemoryStream m = new MemoryStream();
String filePath;
filePath = HttpContext.Current.Server.MapPath(ImagePath);
if(!File.Exists(filePath))
{
filePath = HttpContext.Current.Server.MapPath("images/no_pix.jpg");
}
try
{
origImage = new Bitmap(filePath);
origImage.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
origImage.Dispose();
returnImage = ImageUtils.resizeImage(m.GetBuffer(), width, height);
}
catch
{
}
return returnImage;
}
}
Now this all work great, but i am using this a lot of places and are working on creating a Class Library with all the controls i often use. This way i can deploy a dll with changes. Does anybody know how i should create a Web Server Control in a class library from the above code. I have created a class that inherits System.Web.UI.WebControl.WebControl, but when i try to write a resized image to the outputstream, all the pages displays is unicode chars. I used this code to write the image to the stream.
Page.Response.ContentType = "image/jpeg";
Page.Response.OutputStream.Write(imgBytes, 0, imgBytes.Length);
Any thoughts??