Wednesday, September 30, 2009

Where To Buy Chopsticks

Slim ASPX: ASHX!

Aspx pages are unbeatable when you design Web forms. But if you want produce, for example, only a dynamic image, or to offer a file to download, but you need the "overhead" that the Web Forms bring (Page events, server-side tags, etc.) are not. There are Generic handler. This ASHX have the file extension: This example writes a vertical text as a jpeg, which could be used for example for a table heading.
a \u0026lt;% @ Language = WebHandler "C #" Class = "sample handler %>
2

using System;







3
using

System.Web;







4
using System.Drawing;


5


using System.Drawing.Imaging;


6


using System.Drawing.Drawing2D;


7





8

public

class

Handler : IHttpHandler


9
{


10




11
public


void ProcessRequest(HttpContext context)


12 {




13





14

string text = context.Request.QueryString[ "text"
];


15




16
System.IO. MemoryStream memStream = new
System.IO.
MemoryStream ();



17 Bitmap bitmap = new Bitmap(100, 20, PixelFormat.Format32bppRgb);


18




19
Graphics graphic = Graphics.FromImage(bitmap);


20



21
graphic.SmoothingMode = SmoothingMode.HighSpeed;



22
graphic.Clear(Color.White);



23 graphic.DrawString(text, new Font( "Arial"
, 12), Brushes.Black, 0, 0);
24

25
bitmap.RotateFlip (RotateFlipType.Rotate270FlipNone);
26
bitmap.Save (memStream, ImageFormat.Jpeg);
27
28 context.Response.ContentType = "image/jpeg" ;



29
context.Response.BinaryWrite(memStream.ToArray());



30
context.Response.End();



31





32
}



33





34
public
bool
IsReusable



35
{



36

get



37 {



38 return
false ;

39
}
40
} 41
}

The result of the example is an image. And all this without the Events Page and the Page LifeCycle of ASPX.

0 comments:

Post a Comment