Using the classes provided by. NET, such as Drawing.bitmap, Drawing.bitmap, and so on, it is easy to achieve simple processing of pictures. including water printing, zoom in and out, and so on.
public partial class WebForm4:System.Web.UI.Page
{
Original picture 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.txtPicPath.Text = Server.MapPath ("/test.jpg");
}
Path = This.txtPicPath.Text.Trim ();
if (! System.IO.File.Exists (PATH))
{
Messageshow ("The specified source file does not exist!") ");
Return
}
}
Play Watermark Logo
protected void Btnlogo_click (object sender, EventArgs e)
{
String log = TxtLog.Text.Trim ();
if (log. Length < 1)
{
Messageshow ("Please enter the watermark character!") ");
Return
}
Bitmap = new Bitmap (path);
Graphics = graphics.fromimage (bitmap);
Graphics. DrawString (log, new Font ("XXFarEastFont-Arial"), 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 picture has been generated, path is" + @Server. MapPath ("./_log.jpg"). Replace ("\", "\\\\"));
}
catch (Exception ex)
{
Messageshow ("Generate Picture Error!") "+ Ex." message);
Throw
}
Graphics. Dispose ();
Bitmap. Dispose ();
}
private void Messageshow (String msg)
{
Page.ClientScript.RegisterStartupScript (Page.gettype (), "message", String. Format (Message, msg));
}
Enlarge x*x Times
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 ("The picture has been generated, the path is" + @Server. MapPath ("./_big.jpg"). Replace ("\", "\\\\"));
}
catch (Exception ex)
{
messageshow ("Generate Picture Error!") "+ Ex." message);
Throw
}
Graphics. Dispose ();
bitmap. Dispose ();
}
1/(x*x) reduced to 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 ("The picture has been generated, the path is" + @Server. MapPath ("./_small.jpg"). Replace ("\", "\\\\"));
}
catch (Exception ex)
{
Messageshow ("Generate Picture Error!") "+ Ex." message);
Throw
}
Graphics. Dispose ();
Bitmap. Dispose ();
}
Tilt (turn right 90 degrees)
protected void Btnincline_click (object sender, EventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile (path);
Image rotation, you can take advantage of the RotateFlipType enumeration value, when programming, the 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 ("The picture has been generated, the path is" + @Server. MapPath ("./_incline.jpg"). Replace ("\", "\\\\"));
}
catch (Exception ex)
{
messageshow ("Generate Picture Error!") "+ Ex." message);
Throw
}
Graphics. Dispose ();
bitmap. Dispose ();
}
Image flattening
protected void Btnstave_click (object sender, EventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile (path);
Width is unchanged
int w = img. Width;
Height is 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 ("The picture has been generated, the path is" + @Server. MapPath ("./_stave.jpg"). Replace ("\", "\\\\"));
}
catch (Exception ex)
{
Messageshow ("Generate Picture Error!") "+ Ex." message);
Throw
}
Graphics. Dispose ();
Bitmap. Dispose ();
}
Image Pull Width
protected void Btnelongate_click (object sender, EventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile (path);
Enlarge width
int w = img. WIDTH/2;
No change in height
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 ("The picture has been generated, the path is" + @Server. MapPath ("./_elongate.jpg"). Replace ("\", "\\\\"));
}
catch (Exception ex)
{
Messageshow ("Generate Picture Error!") "+ Ex." message);
Throw
}
Graphics. Dispose ();
Bitmap. Dispose ();
}
}