asp.net 2.0,c#----Image Special effects processing _ practical skills

Source: Internet
Author: User
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 ();
}
}

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.