Two Functions of generating images and checking the image size in ASP. NET

Source: Internet
Author: User

/// <Summary>
/// Generate a thumbnail
/// </Summary>
/// <Param name = "strsourcefilename"> source file </param>
/// <Param name = "strdesfilename"> thumbnail file </param>
/// <Param name = "intwidth"> thumbnail width </param>
/// <Param name = "intheight"> thumbnail height </param>
/// <Param name = "isscale"> whether the aspect ratio is guaranteed </param>
Public static void miniimage (string strsourcefilename, string strdesfilename, int intwidth, int intheight, bool isscale)
{
// Int intwidth = 95;
// Int intheight = 80;
If (isscale)
{
Bitmap BP = new Bitmap (strsourcefilename );
Double douwidth = (double) BP. width/intwidth;
Double douheight = (double) BP. Height/intheight;

If (douwidth)> (douheight ))
{
// Use width as the standard
Intheight = intwidth * bp. Height/BP. width;
}
Else
{
// Based on height
Intwidth = intheight * bp. width/BP. height;
}
}

Bitmap BPDEs = new Bitmap (intwidth, intheight );
Bitmap bpsource = new Bitmap (strsourcefilename );

Graphics mygraphics = graphics. fromimage (BPDEs );

// ------- High-quality image

Mygraphics. interpolationmode = interpolationmode. High;
Mygraphics. smoothingmode = smoothingmode. highquality;

Mygraphics. drawimage (bpsource, 0, 0, intwidth, intheight );
BPDEs. Save (strdesfilename, imageformat. JPEG );
BPDEs. Dispose ();
Bpsource. Dispose ();

}


/// <Summary>
/// Verify the image size
/// </Summary>
/// <Param name = "strfilename"> source image file </param>
/// <Param name = "intwidth"> </param>
/// <Param name = "intheight"> </param>
/// <Returns> </returns>
Public static bool checkimagesize (string strfilename, int intwidth, int intheight)
{

Bitmap BP = new Bitmap (strfilename );
If (BP. Height = intheight & BP. width = intwidth)
{
BP. Dispose ();
Return true;
}
Else
{
BP. Dispose ();
Return false;
}
}

========================================================== ====

The following is a reprinted message from another blog.

Generating thumbnails is a common and practical function during website development. previously, ASP can only be implemented using COM components. net can be easily implemented using the powerful class libraries of the framework. post the complete code (with detailed comments) in the following post, and refer to some articles on the Internet and.. Net SDK. pictures of QQ room network homes are uploaded using all four generation methods.

/// <Summary>
/// Generate a thumbnail
/// </Summary>
/// <Param name = "originalimagepath"> source image path (physical path) </param>
/// <Param name = "thumbnailpath"> thumbnail path (physical path) </param>
/// <Param name = "width"> thumbnail width </param>
/// <Param name = "height"> thumbnail height </param>
/// <Param name = "Mode"> how to generate a thumbnail </param>
Public static void makethumbnail (string originalimagepath, string thumbnailpath, int width, int height, string Mode)
{
Image originalimage = 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
Image bitmap = new system. Drawing. Bitmap (towidth, toheight );

// Create a canvas
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 (color. Transparent );

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

Try
{
// Save the thumbnail in JPG format
Bitmap. Save (thumbnailpath, system. Drawing. imaging. imageformat. JPEG );
}
Catch (system. Exception E)
{
Throw E;
}
Finally
{
Originalimage. Dispose ();
Bitmap. Dispose ();
G. Dispose ();
}
}

For the key method graphics. drawimage, see ms-help: // Ms. netframeworksdkv1.1.chs/cpref/html/frlrfsystemdrawinggraphicsclassdrawimagetopic11.htm.

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.