ASP. NET 2.0, C # -- image special effect Processing

Source: Internet
Author: User

Using the classes provided by. net, such as drawing. bitmap and drawing. bitmap, you can easily process images. This includes watermarking, enlarging, downgrading, and other operations.

Public partial class webform4: system. Web. UI. Page
{
// Original image path
Private string path;
Private system. Drawing. Bitmap bitmap;
Private system. Drawing. Graphics graphics;
String message = "<SCRIPT> alert (\" {0} \ "); </SCRIPT> ";
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
This.txt picpath. Text = server. mappath ("/test.jpg ");
}
Path = this.txt picpath. Text. Trim ();
If (! System. Io. file. exists (PATH ))
{
Messageshow ("the specified source file does not exist! ");
Return;
}
}
// Watermark the logo
Protected void btnlogo_click (Object sender, eventargs E)
{
String log = txtlog. Text. Trim ();
If (log. Length <1)
{
Messageshow ("Enter the watermark character! ");
Return;
}

Bitmap = new Bitmap (PATH );
Graphics = graphics. fromimage (Bitmap );
Graphics. drawstring (log, new font ("", 16), system. drawing. brushes. greenyellow, new pointf (bitmap. width/2-(log. length) * 5, bitmap. height/2 ));
Try
{
Bitmap. Save (server. mappath ("./_log.jpg"), system. Drawing. imaging. imageformat. JPEG );
Messageshow ("watermark image generated, path:" + @ server. mappath ("./_log.jpg"). Replace ("\\","\\\\"));

}
Catch (exception ex)
{
Messageshow ("Image Generation error! "+ Ex. Message );
Throw;
}
Graphics. Dispose ();
Bitmap. Dispose ();
}
Private void messageshow (string MSG)
{
Page. clientscript. registerstartupscript (page. GetType (), "message", String. Format (message, MSG ));

}
// Zoom in x
Protected void btnbig_click (Object sender, eventargs E)
{
Int I = int. parse (txtbig. Text. Trim ());
System. Drawing. Image IMG = system. Drawing. image. fromfile (PATH );
Bitmap = new Bitmap (IMG. Width * I, IMG. Height * I );
Graphics = graphics. fromimage (Bitmap );
Graphics. drawimage (IMG, 0, 0, IMG. Width * I, IMG. Height * I );
Try
{
Bitmap. Save (server. mappath ("./_big.jpg"), system. Drawing. imaging. imageformat. JPEG );
Messageshow ("generated image, path:" + @ server. mappath ("./_big.jpg"). Replace ("\\","\\\\"));

}
Catch (exception ex)
{
Messageshow ("Image Generation error! "+ Ex. Message );
Throw;
}
Graphics. Dispose ();
Bitmap. Dispose ();
}

// Scale down to 1/(x) of the original image)
Protected void btnsmall_click (Object sender, eventargs E)
{
Float I = float. parse (txtbig. Text. Trim ());
System. Drawing. Image IMG = system. Drawing. image. fromfile (PATH );
Int W = convert. toint32 (IMG. width/I );
Int H = convert. toint32 (IMG. Height/I );

// Prevent excessive deformation
If (W <1) W = 10;
If (H <1) h = 0;
Bitmap = new Bitmap (W, H );
Graphics = graphics. fromimage (Bitmap );
Graphics. drawimage (IMG, 0, 0, W, H );
Try
{
Bitmap. Save (server. mappath ("./_small.jpg"), system. Drawing. imaging. imageformat. JPEG );
Messageshow ("generated image, path:" + @ server. mappath ("./_small.jpg"). Replace ("\\","\\\\"));

}
Catch (exception ex)
{
Messageshow ("Image Generation error! "+ Ex. Message );
Throw;
}
Graphics. Dispose ();
Bitmap. Dispose ();
}
// Tilt (right turn 90 degrees)
Protected void btnincline_click (Object sender, eventargs E)
{
System. Drawing. Image IMG = system. Drawing. image. fromfile (PATH );
// Image rotation. You can use the enumeration value of rotatefliptype. During programming, IDE automatically displays the meaning of each enumeration.
IMG. rotateflip (rotatefliptype. rotate90flipxy );
Bitmap = new Bitmap (IMG );
Graphics = graphics. fromimage (Bitmap );
Graphics. drawimage (IMG, new point (0, 0 ));
Try
{
Bitmap. Save (server. mappath ("./_incline.jpg"), system. Drawing. imaging. imageformat. JPEG );
Messageshow ("generated image, path:" + @ server. mappath ("./_incline.jpg"). Replace ("\\","\\\\"));

}
Catch (exception ex)
{
Messageshow ("Image Generation error! "+ Ex. Message );
Throw;
}
Graphics. Dispose ();
Bitmap. Dispose ();
}

// Flatten the image
Protected void btnstave_click (Object sender, eventargs E)
{
System. Drawing. Image IMG = system. Drawing. image. fromfile (PATH );
// The width remains unchanged.
Int W = IMG. width;
// 1/2 of the original height
Int H = IMG. Height/2;

// Prevent excessive deformation
If (W <1) W = 10;
If (H <1) h = 0;
Bitmap = new Bitmap (W, H );
Graphics = graphics. fromimage (Bitmap );
Graphics. drawimage (IMG, 0, 0, W, H );
Try
{
Bitmap. Save (server. mappath ("./_stave.jpg"), system. Drawing. imaging. imageformat. JPEG );
Messageshow ("generated image, path:" + @ server. mappath ("./_stave.jpg"). Replace ("\\","\\\\"));

}
Catch (exception ex)
{
Messageshow ("Image Generation error! "+ Ex. Message );
Throw;
}
Graphics. Dispose ();
Bitmap. Dispose ();
}
// Image Width
Protected void btnelongate_click (Object sender, eventargs E)
{
System. Drawing. Image IMG = system. Drawing. image. fromfile (PATH );
// Enlarge the width
Int W = IMG. width/2;
// The height remains unchanged.
Int H = IMG. height;

// Prevent excessive deformation
If (W <1) W = 10;
If (H <1) h = 0;
Bitmap = new Bitmap (W, H );
Graphics = graphics. fromimage (Bitmap );
Graphics. drawimage (IMG, 0, 0, W, H );
Try
{
Bitmap. Save (server. mappath ("./_elongate.jpg"), system. Drawing. imaging. imageformat. JPEG );
Messageshow ("generated image, path:" + @ server. mappath ("./_elongate.jpg"). Replace ("\\","\\\\"));

}
Catch (exception ex)
{
Messageshow ("Image Generation error! "+ Ex. Message );
Throw;
}
Graphics. Dispose ();
Bitmap. Dispose ();
}
}

Related Article

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.