System. Drawing. imaging;
Namespace Myhttphandler
{
Internal Class Commondeal
{
Public Static Void Dealimagerequest (httpcontext context)
{
Font enfont = New Font ( " Times New Roman " , 12 );
Memorystream MS= NewMemorystream ();
String Filepath = Context. server. mappath ( " Image " + Context. Request. Path );
Try
{
Image = Image. fromfile (filepath );
If (Ispixelformatindexed (image. pixelformat ))
{
Bitmap BMP = New Bitmap (image. Width, image. Height, pixelformat. format32bppargb );
Using (Graphics graphic = Graphics. fromimage (BMP ))
{
Graphic. interpolationmode = System. Drawing. drawing2d. interpolationmode. highqualitybicubic;
Graphic. smoothingmode = System. Drawing. drawing2d. smoothingmode. highquality;
Graphic. compositingquality = System. Drawing. drawing2d. compositingquality. highquality;
Graphic. drawimage (image, 0 , 0 );
Graphic. drawstring ( " Pangxiaoliang " , Enfont, New Solidbrush (color. lightgray ), 2 , 2 );
Graphic. drawstring ( " Pangxiaoliang " , Enfont, New Solidbrush (color. Black ), 0 , 0 );
}
BMP. Save (MS, system. Drawing. imaging. imageformat. GIF );
BMP. Dispose ();
}
Else
{
Using (Graphics graphic = Graphics. fromimage (image ))
{
Graphic. drawstring ( " Pangxiaoliang " , Enfont, New Solidbrush (color. lightgray ), 2 , 2 );
Graphic. drawstring ( " Pangxiaoliang " , Enfont, New Solidbrush (color. Black ), 0 , 0 );
}
bitmap thebitmap = New Bitmap (image );
thebitmap. save (MS, system. drawing. imaging. imageformat. JPEG);
thebitmap. dispose ();
}
Context. response. clearcontent ();
Context. response. contenttype= "Image/jpg";
Context. response. binarywrite (Ms. toarray ());
}
Catch
{
Bitmap Error = New Bitmap (context. server. mappath ( " Image/error.bmp " ));
Error. Save (MS, system. Drawing. imaging. imageformat. JPEG );
Error. Dispose ();
Context. response. clearcontent ();
Context. response. contenttype = " Image/jpg " ;
Context. response. binarywrite (Ms. toarray ());
}
Finally
{
Ms. Close ();
Ms. Dispose ();
Context. response. End ();
}
}
Public Static Void Returnerrorimage (httpcontext context)
{
Memorystream MS = New Memorystream ();
Try
{
Bitmap Error = New Bitmap (context. server. mappath ( " Error.bmp " ));
Error. Save (MS, system. Drawing. imaging. imageformat. JPEG );
Error. Dispose ();
Context. response. clearcontent ();
Context. response. contenttype = " Image/jpg " ;
Context. response. binarywrite (Ms. toarray ());
}
Catch
{
}
Finally
{
Ms. Close ();
Ms. Dispose ();
Context. response. End ();
}
}
/// <Summary>
/// The pixelformat of the graphics exception is generated.
/// </Summary>
Private Static Pixelformat [] indexedpixelformats = {Pixelformat. undefined, pixelformat. dontcare,
Pixelformat. format16bppargb1555, pixelformat. format1bppindexed, pixelformat. format4bppindexed,
Pixelformat. format8bppindexed
};
/// <Summary>
/// Determines whether the pixelformat of the image is in the pixelformat that causes an exception.
/// </Summary>
/// <Param name = "imgpixelformat"> Pixelformat of the original image </Param>
/// <Returns> </returns>
Private Static Bool Ispixelformatindexed (pixelformat imgpixelformat)
{
Foreach (Pixelformat pf In Indexedpixelformats)
{
If (PF. Equals (imgpixelformat )) Return True ;
}
Return False ;
}
}
}
1) string filepath = context. server. mappath ("image" + context. Request. Path); first obtain the absolute path of the image on the server, so that the image can be processed.
2) memorystream MS = new memorystream (); memory stream is used to respond to client requests and return the processed image to the client.
3) Use GDI + to write a name on the image and return the new image to the client as a stream.