Hello, I have the code below which will help you for creating a code for the custom HTTP Handler. So, just make use of it and solve the problem of yours.
Code:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
public class HttpHandlerTesting : IHttpHandler {
public void RequestProcessing (HttpContext Hcontext)
{
Hcontext.Response.ContentType = "image/png";
string str = Hcontext.Request.QueryString["str"];
int height = Convert.ToInt32(Hcontext.Request.QueryString["height"]);
int width = Convert.ToInt32(Hcontext.Request.QueryString["width"]);
Bitmap bitmap;
Color color = Color.FromArgb(255, 253, 244);
MemoryStream memoryStream = new MemoryStream();
switch (str)
{
default:
int pt = 1;
while (Hcontext.Request.QueryString["P" + pt.ToString()] != null)
pt++;
decimal[] vals = new Decimal[pt];
for (int i = 0; i < pt; i++)
vals[i] = Convert.ToInt32(
Hcontext.Request.QueryString["P" + i.ToString()]);
PieChart pichart = new PieChart();
bitmap = pichart.Draw(color, width, height, vals);
break;
}
bitmap.Save(memoryStream, ImageFormat.Png);
memoryStream.WriteTo(Hcontext.Response.OutputStream);
}
public bool IsReusable
{
get {
return false;
}
}
}
public class PieChart
{
public Bitmap Draw(Color color, int width, int height, decimal[] vals)
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
SolidBrush sdbrush = new SolidBrush(color);
g.FillRectangle(sdbrush, 0, 0, width, height);
sdbrush.Dispose();
SolidBrush[] sdbrush = new SolidBrush[10];
sdbrush[0] = new SolidBrush(Color.Yellow);
sdbrush[1] = new SolidBrush(Color.Green);
sdbrush[2] = new SolidBrush(Color.Blue);
sdbrush[3] = new SolidBrush(Color.Cyan);
sdbrush[4] = new SolidBrush(Color.Magenta);
sdbrush[5] = new SolidBrush(Color.Red);
sdbrush[6] = new SolidBrush(Color.Black);
sdbrush[7] = new SolidBrush(Color.Gray);
sdbrush[8] = new SolidBrush(Color.Maroon);
sdbrush[9] = new SolidBrush(Color.LightBlue);
decimal total = 0.0m;
foreach (decimal val in vals)
total += val;
float first = 0.0f;
float last = 0.0f;
decimal curr = 0.0m;
for (int i = 0; i < vals.Length; i++)
{
curr += vals[i];
first = last;
last = (float)(curr / total) * 360.0f;
g.FillPie(sdbrush[i % 10], 0.0f, 0.0f, width,
height, first, last - first);
}
foreach (SolidBrush cleanBrush in sdbrush)
cleanBrush.Dispose();
return bitmap;
}
}
Bookmarks