Change the image size

Source: Internet
Author: User
Tags bmp image

Note: first create a directory

# Do not change the shape written by region
/// <Summary> create an image of the specified size
/// The source image can only be in JPG format
/// </Summary>
/// <Param name = "opath"> absolute path of the source image </param>
/// <Param name = "tpath"> Generate the absolute path of the image </param>
/// <Param name = "width"> width of the generated image </param>
/// <Param name = "height"> height of the generated image </param>
Public void createimage (string opath, string tpath, int width, int height)
{
Bitmap originalbmp = new Bitmap (opath );
// Position of the source image in the new image
Int left, top;

If (originalbmp. width <= width & originalbmp. height <= height)
{
// The width and height of the original image are smaller than the size of the generated image.
Left = (INT) math. Round (decimal) (width-originalbmp. width)/2 );
Top = (INT) math. Round (decimal) (height-originalbmp. Height)/2 );

// The final generated image
Bitmap bmpout = new Bitmap (width, height );
Using (Graphics graphics = graphics. fromimage (bmpout ))
{
// Set a high quality Interpolation Method
Graphics. interpolationmode = interpolationmode. highqualitybicubic;
// Clear the canvas and fill it with a white background color
Graphics. Clear (color. White );
// Map the source image to the new canvas.
Graphics. drawimage (originalbmp, left, top );
}
Bmpout. Save (tpath );
Bmpout. Dispose ();

Return;
}

// The width and height of the new image, such as an image of 400*200. to generate an image of 160*120 without deformation,
// The generated image should be 160*80, and then draw the 160*80 image to the canvas of 160*120
Int newwidth, newheight;
If (width * originalbmp. height {
Newwidth = width;
Newheight = (INT) math. Round (decimal) originalbmp. Height * width/originalbmp. width );
// Scale to the same width as the predefined width, that is, Left = 0. Calculate the top
Left = 0;
Top = (INT) math. Round (decimal) (height-newheight)/2 );
}
Else
{
Newwidth = (INT) math. Round (decimal) originalbmp. Width * Height/originalbmp. Height );
Newheight = height;
// Scale to the same height as the predefined height, that is, Top = 0. Calculate left
Left = (INT) math. Round (decimal) (width-newwidth)/2 );
Top = 0;
}

// Generate a scaled chart, for example, a 160*80 graph.
Bitmap bmpout2 = new Bitmap (newwidth, newheight );
Using (Graphics graphics = graphics. fromimage (bmpout2 ))
{
Graphics. interpolationmode = interpolationmode. highqualitybicubic;
Graphics. fillrectangle (brushes. White, 0, 0, newwidth, newheight );
Graphics. drawimage (originalbmp, 0, 0, newwidth, newheight );
}
// Add the picture to the pre-defined canvas with a width and height, for example, 160*120.
Bitmap lastbmp = new Bitmap (width, height );
Using (Graphics graphics = graphics. fromimage (lastbmp ))
{
// Set a high quality Interpolation Method
Graphics. interpolationmode = interpolationmode. highqualitybicubic;
// Clear the canvas and fill it with a white background color
Graphics. Clear (color. White );
// Map the source image to the new canvas.
Graphics. drawimage (bmpout2, left, top );
}
Lastbmp. Save (tpath );
Lastbmp. Dispose ();
}
# Endregion

 

Public static void makethumbnail (string originalimagepath, string thumbnailpath, int width, int height, string Mode)
{
System. Drawing. Image originalimage = system. Drawing. image. fromfile (originalimagepath );

Int towidth = width;
Int toheight = height;

Int x = 0;
Int y = 0;
Int ow = originalimage. width;
Int Oh = originalimage. height;

Switch (Mode)
{
Case "HW": // specify high-width Scaling (possibly deformed)
Break;
Case "W": // specify the width, and the height is proportional.
Toheight = originalimage. Height * width/originalimage. width;
Break;
Case "H": // specify the height. The width is proportional.
Towidth = originalimage. Width * Height/originalimage. height;
Break;
Case "cut": // specify the height and width (not deformed)
If (double) originalimage. width/(double) originalimage. Height> (double) towidth/(double) toheight)
{
Oh = originalimage. height;
Ow = originalimage. Height * towidth/toheight;
Y = 0;
X = (originalimage. Width-ow)/2;
}
Else
{
Ow = originalimage. width;
Oh = originalimage. Width * Height/towidth;
X = 0;
Y = (originalimage. Height-OH)/2;
}
Break;
Default:
Break;
}

// Create a BMP Image
System. Drawing. Image bitmap = new system. Drawing. Bitmap (towidth, toheight );

// 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 and fill it with a transparent background color
G. Clear (system. Drawing. color. Transparent );

// Draw the specified part of the original image at the specified position and in the specified size
G. drawimage (originalimage, new system. Drawing. rectangle (0, 0, towidth, toheight ),
New system. Drawing. rectangle (X, Y, ow, oh ),
System. Drawing. graphicsunit. pixel );

// Save the thumbnail in JPG format
Bitmap. Save (thumbnailpath, system. Drawing. imaging. imageformat. JPEG );

Originalimage. Dispose ();
Bitmap. Dispose ();
G. Dispose ();

}

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.