Asp.net generates image instance code

Source: Internet
Author: User

Here, we use asp.net to generate an image based on parameters such as p, x, y, and x.

Here, we use asp.net to generate an image based on parameters such as p, x, y, and x.

<% @ WebHandler Language = "c #" Class = "ImgCropper_WebHandler" Debug = "true" %>

Using System;
Using System. Web;
Using System. Drawing;
Using System. IO;

Public class ImgCropper_WebHandler: IHttpHandler
{
Public void ProcessRequest (HttpContext context)
{
String Pic = Convert. ToString (context. Request ["p"]);
Int PointX = Convert. ToInt32 (context. Request ["x"]);
Int PointY = Convert. ToInt32 (context. Request ["y"]);
Int CutWidth = Convert. ToInt32 (context. Request ["w"]);
Int CutHeight = Convert. ToInt32 (context. Request ["h"]);
Int PicWidth = Convert. ToInt32 (context. Request ["pw"]);
Int PicHeight = Convert. ToInt32 (context. Request ["ph"]);

Context. Response. ContentType = "image/jpeg ";
ResetImg (System. Web. HttpContext. Current. Server. MapPath (Pic), PicWidth, PicHeight, PointX, PointY, CutWidth, CutHeight). WriteTo (context. Response. OutputStream );
}

Public MemoryStream ResetImg (string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight)
{
Image imgPhoto = Image. FromFile (ImgFile );
Bitmap BMP hoto = new Bitmap (CutWidth, CutHeight, System. Drawing. Imaging. PixelFormat. Format24bppRgb );

Graphics gbmPhoto = Graphics. FromImage (BMP hoto );
GbmPhoto. drawImage (imgPhoto, new Rectangle (0, 0, CutWidth, CutHeight), PointX * imgPhoto. width/PicWidth, PointY * imgPhoto. height/PicHeight, CutWidth * imgPhoto. width/PicWidth, CutHeight * imgPhoto. height/PicHeight, GraphicsUnit. pixel );

MemoryStream ms2 = new MemoryStream ();
BMP hoto. Save (ms2, System. Drawing. Imaging. ImageFormat. Jpeg );

ImgPhoto. Dispose ();
GbmPhoto. Dispose ();
BMP hoto. Dispose ();

Return ms2;
}


Public bool IsReusable
{
Get
{
Return false;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.