ASP.NET generates high quality thumbnail universal function (C # code), supports multiple generation methods

xiaoxiao2021-04-02  216

[Date: 2006-2-25 17:48:17] [Source: Original] [Author: Jerry] [font: medium and small]

Generating thumbnails when developing, generating thumbnails is a very common and practical function. It used to be implemented with COM components in ASP. Now you can use the powerful class library of the framework in .NET. (With detailed notes), refer to some articles on the Internet and .NET SDK related content. The image of QQroom Network Home is used to all four ways.

///

/// Generate thumbnail /// /// Source diagram path (physical path) /// Thumbnail Path (physical path) /// thumbnail width /// thumbnail height / // generated thumbnails public static void MakeThumbnail (string originalImagePath, string thumbnailPath, int width, int height, string mode) {Image originalImage = Image.FromFile (originalImagePath); INT Towidth = Width; int = 0; int y = 0; int = originalImage.width; int oh = originalImage.Height; switch (mode) {copy "hw": // Specify high and wide zoom (Possible deformation) Break; Case "W": // Specify wide, high proportion toheight = OriginalIgne.Height * width / OriginalImage.width; Break; Case "H": // Specify high, broadly proportional toidth = OriginalImage.width * Height / OriginalImage.Height; Break; Case "CUT": // Specify high wide cut (non-deformation) IF ((Double) OriginalImage .Width / (double) OriginalImage.height> (double) Towidth / (Double) toheight) {oh = OriginalImage.Height; OW =

OriginalImage.height; 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;} // Newly built a BMP picture image bitmap = new system.drawing.bitmap (towidth, toheight); // Newly built a picture board graphics g = system.drawing. Graphics.FromImage (bitmap); // interpolation g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High quality setting; // set high, low speed exhibits a degree of smoothing g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode .Hquality; // Empty the canvas and populate g.clear (color.Transparent) in a transparent background; / / Draw the specified section of the original picture in the specified position, the specified section of the original picture, G. DrawImage (0, 0, Towidth, ToHeight), New Rectangle (X, Y, OW, OH), GraphicsUnit.pixel; Try {// Save thumbnails Bitmap.Save (thumbnailpath, system.drawing.image.iMage.jpeg) in JPG format;} catch (system.exception e) {throw e;} finally {OriginalImage.dispose (); bitmap. Dispose (); g.dispose ();}}

转载请注明原文地址:https://www.9cbs.com/read-131386.html

New Post(0)