My ashx stopped working (Works locally, but not online)
- by Madi D.
i have a simple ASHX file that returns pictures upon request (mainly a counter),
previously the code simply fetched pre-made pictures(already uploaded and available) and sent them to the requester..
i just modified the code,so it would take a base picture, do some modifications on it, save it to the server, then serve it to the user.. tested it locally and it worked like a charm.
however when i uploaded the code to my hosting service (Godaddy..) it didnt work.
Can someone point the problem out to me?
Note: ASHX worked with me before,so i know the web.config and IIS are handling them properly, however i think i am missing something ..
Code:
<%@ WebHandler Language="C#" Class="NWEmpPhotoHandler" %>
using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
public class NWEmpPhotoHandler : IHttpHandler
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext ctx)
{
try
{
//Some Code to fetch # of vistors from DB
int x = 10,000; // # of Vistors, fetched from DB
string numberOfVistors = (1000 + x).ToString();
string filePath = ctx.Server.MapPath("counter.jpg");
Bitmap myBitmap = new Bitmap(filePath);
Graphics g = Graphics.FromImage(myBitmap);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
StringFormat strFormat = new StringFormat();
g.DrawString(numberOfVistors , new Font("Tahoma", 24), Brushes.Maroon,
new RectangleF(55, 82, 500, 500),null);
string PathToSave = ctx.Server.MapPath("counter-" + numberOfVistors + ".jpg");
saveJpeg(PathToSave, myBitmap, 100);
if (File.Exists(PathToSave))
{
ctx.Response.ContentType = "image/jpg";
ctx.Response.WriteFile(PathToSave);
//ctx.Response.OutputStream.Write(picByteArray, 0, picByteArray.Length);
}
}
catch (ArgumentException exe)
{
}
}
private ImageCodecInfo getEncoderInfo(string mimeType)
{
// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
// Find the correct image codec
for (int i = 0; i < codecs.Length; i++)
if (codecs[i].MimeType == mimeType)
return codecs[i];
return null;
}
private void saveJpeg(string path, Bitmap img, long quality)
{
// Encoder parameter for image quality
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);
// Jpeg image codec
ImageCodecInfo jpegCodec = this.getEncoderInfo("image/jpeg");
if (jpegCodec == null)
return;
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
img.Save(path, jpegCodec, encoderParams);
}
}