Today, in the background of a website, we have a function to generate thumbnails for uploaded images. The method was previously written by ourselves. As a result, we found that many images were not correctly generated, so I debugged what I wrote more than two years ago.CodeAlthough I wrote a lot of comments in that year, it took me a lot of time because I spent a lot of time proving a mathematical formula. This formula should be junior high school knowledge! I think it's about three or four years.ProgramIn addition to elementary school arithmetic, it seems that no data knowledge is used.
This is an inequality.
Set a = (y * A/X)-B, B = (x * B/y)-A, where Y, X, A, and B are mostly real numbers greater than zero,
It is proved that a and B must exist in a> = 0 and B <= 0, or a <= 0 and B> = 0
The proof is not difficult, because it is indeed an unequal formula in junior high school. The following is the source of the problem.
Code for producing thumbnails in proportion to images
//------------------------------
Public static bool thumimage (string srcpath, string destpath, double width, double height)
{
System. Drawing. Image IMG = new Bitmap (srcpath );
// The size of the generated image must be smaller than that of the source image.
If (IMG. width <width)
{
Width = IMG. width;
}
If (IMG. height {
Height = IMG. height;
}
// The height and width of the deletion.
Double cutwidth, cutheight;
Cutwidth = (IMG. Width * Height/IMG. Height-width); // width cutting, height Scaling
Cutheight = (IMG. Height * width/IMG. Width-height); // height cutting, width Scaling
Byte flag = 0; // 0 cut height, 1 cut width
// The width here indicates that the width of the thumbnail will be intercepted, not the source image,
// 1. based on the ratio of the original image, select the height of the thumbnail. If the calculated width is greater than the specified width, the thumbnail is generated based on the height and the calculated width, and then captured based on the given size.
// 2. based on the ratio of the original image, select the width of the thumbnail. If the calculated height is greater than the specified height, the thumbnail is generated based on the width and height calculated, and then captured based on the given size.
// 3. the aspect ratio can only be {1,> 1, <1 }.
Flag = (byte) (cutheight <= cutwidth? 0: 1 );
// System. Drawing. image. getthumbnailimageabort mycallback = new system. Drawing. image. getthumbnailimageabort (thumbnailcallback );
System. Drawing. Image thumimg;
If (flag = 0)
Thumimg = new Bitmap (IMG, (INT) width, (INT) height + (INT) cutheight); // IMG. getthumbnailimage (INT) width, (INT) height + (INT) cutheight, mycallback, intptr. zero );
Else
Thumimg = new Bitmap (IMG, (INT) width + (INT) cutwidth, (INT) height); // IMG. getthumbnailimage (INT) width + (INT) cutwidth, (INT) height, mycallback, intptr. zero );
System. Drawing. Bitmap destimg = new Bitmap (INT) width, (INT) height );
Graphics G = graphics. fromimage (destimg );
Rectangle rect = new rectangle (0, 0, (INT) width, (INT) height );
G. drawimage (thumimg, rect, rect, graphicsunit. pixel );
G. Save ();
Destimg. Save (destpath, getformat (destpath ));
Thumimg. Dispose ();
IMG. Dispose ();
Destimg. Dispose ();
Return true;
}
//----------------------------
The idea of this method is that the ratio of the length and width of the source image is not necessarily the same as that of the thumbnail to be generated. If the ratio of the length and width is different, the direct generation will cause the image to be deformed,
To prevent this problem, we need to generate an image based on the ratio of the original image. First, we need to generate an image slightly larger than the thumbnail, then, the image is captured in the specified size. to achieve this, calculate the width that matches the ratio of the original image based on the height of the specified thumbnail, and compare the width with the width of the thumbnail to be generated, if the calculated width is greater than the width of the specified thumbnail, a bitmap is created based on the calculated width and height of the thumbnail, and then captured based on the desired thumbnail size. If the calculated width is less than the width of the specified thumbnail, the scaled height is calculated based on the width of the specified thumbnail. The height must be greater than the height of the thumbnail to be generated, in this case, the width of the thumbnail is generated based on the calculated height, because the height and width must meet the preceding requirements. The Code is as follows:
If (flag = 0)
Thumimg = new Bitmap (IMG, (INT) width, (INT) height + (INT) cutheight); // IMG. getthumbnailimage (INT) width, (INT) height + (INT) cutheight, mycallback, intptr. zero );
Else
Thumimg = new Bitmap (IMG, (INT) width + (INT) cutwidth, (INT) height); // IMG. getthumbnailimage (INT) width + (INT) cutwidth, (INT) height, mycallback, intptr. zero );
System. Drawing. Bitmap destimg = new Bitmap (INT) width, (INT) height );
Graphics G = graphics. fromimage (destimg );
Rectangle rect = new rectangle (0, 0, (INT) width, (INT) height );
G. drawimage (thumimg, rect, rect, graphicsunit. pixel );