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

Source: Internet
Author: User

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 *&

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.