C # compress the image. You can specify the height and width of the image template.

Source: Internet
Author: User
Tags bmp image

To process images in a web application today, specify the image size and height. Google has a piece of information. I think this method is quite good. You can use it for reference. If your younger brother writes something wrong, please give us some advice: the following code is written in winform and can be used for testing on the web.

Code

// Create a thumbnail Function
// Sequence parameters: source image file stream, thumbnail storage address, template width, and template height
// Note: the thumbnail size is controlled in the template area.
Public static void makesmallimg (system. Io. Stream fromfilestream, string filesaveurl, system. Double templatewidth, system. Double templateheight)
{
// Obtain the image object from the file and use the color management information embedded in the stream
System. Drawing. Image myimage = system. Drawing. image. fromstream (fromfilestream, true );
// Width and height of the thumbnail
System. Double newwidth = myimage. Width, newheight = myimage. height;
// The horizontal chart that is larger than the Template
If (myimage. width> myimage. Height | myimage. width = myimage. Height)
{
If (myimage. width> templatewidth)
{
// Scale the width by template and the height by scale
Newwidth = templatewidth;
Newheight = myimage. Height * (newwidth/myimage. width );
}
}
// Portrait of a template
Else
{
If (myimage. Height> templateheight)
{
// High by template, width by proportional Scaling
Newheight = templateheight;
Newwidth = myimage. Width * (newheight/myimage. Height );
}
}
// Obtain the image size
System. Drawing. Size mysize = new size (INT) newwidth, (INT) newheight );
// Create a BMP Image
System. Drawing. Image bitmap = new system. Drawing. Bitmap (mysize. Width, mysize. Height );
// Create a canvas
System. Drawing. Graphics G = system. Drawing. Graphics. fromimage (Bitmap );
// Set a high quality Interpolation Method
G. interpolationmode = system. Drawing. drawing2d. interpolationmode. High;
// Set high quality and smooth Low Speed
G. smoothingmode = system. Drawing. drawing2d. smoothingmode. highquality;
// Clear the canvas
G. Clear (color. White );
// Draw a picture at a specified position
G. drawimage (myimage, new system. Drawing. rectangle (0, 0, bitmap. Width, bitmap. Height ),
New system. Drawing. rectangle (0, 0, myimage. Width, myimage. Height ),
System. Drawing. graphicsunit. pixel );
/// Text watermark
// System. Drawing. Graphics G = system. Drawing. Graphics. fromimage (Bitmap );
// System. Drawing. Font F = new font ("", 10 );
// System. Drawing. Brush B = new solidbrush (color. Black );
// G. drawstring ("myohmine", F, B, 10, 10 );
// G. Dispose ();
/// Image Watermark
// System. Drawing. Image copyimage = system. Drawing. image. fromfile (system. Web. httpcontext. Current. server. mappath ("PIC/1.gif "));
// Graphics A = graphics. fromimage (Bitmap );
//. Drawimage (copyimage, new rectangle (bitmap. width-copyImage.Width, bitmap. height-copyImage.Height, copyimage. width, copyimage. height), 0, 0, copyimage. width, copyimage. height, graphicsunit. pixel );
// Copyimage. Dispose ();
// A. Dispose ();
// Copyimage. Dispose ();
// Save the thumbnail
Bitmap. Save (filesaveurl, system. Drawing. imaging. imageformat. JPEG );
G. Dispose ();
Myimage. Dispose ();
Bitmap. Dispose ();
}

 

Code

Private void button2_click (Object sender, eventargs E)
{
Openfiledialog filedialog = new openfiledialog ();
Filedialog. Title = "select image file ";
// Filedialog. Filter = "Excel files (*. xls) | *. xls ";
Filedialog. filterindex = 1;
If (filedialog. showdialog () = system. Windows. Forms. dialogresult. OK)
{

System. Io. filestream file = system. Io. file. Open (filedialog. filename, system. Io. filemode. Open );
System. Io. Stream strea = file;
File. Close ();
Makesmallimg (strea, "image .jpg", 150,150 );
// File. Close ();

}
}

 

 

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.