C # generate a class thumbnail class

xiaoxiao2021-03-02  66

There are 4 heavy load methods, there is a direct return image, there is a generated thumbnail, and save it to the specified directory!

Using system.io; using system.drawing.image;

///

/// Picture Process Class /// 1, generate a thumbnail image or change the size and picture quality /// 2 of the picture according to the ratio, and put the generated thumbnail to the specified directory ///// public class imageclass {public image resourceImage; private int imageHeight;

PUBLIC STRING ERRMESSAGE;

///

/// /// Image file full path name public imageclass (string imagefilename) {resourceImage = Image.fromfile (imagefilename); errmessage = "";

Public Bool thumbnailcallback () {Return False;}

///

/// Generates a thumbnail overload method 1, returns the image object /// /// thumbnail / // thumbnails of the Image Objects PUBLIC Image GetReducedImage (Int Width, Int Height);

Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort (ThumbnailCallback); ReducedImage = ResourceImage.GetThumbnailImage (Width, Height, callb, IntPtr.Zero); return ReducedImage;} catch (Exception e) {ErrMessage = e.Message; return null;} }

///

/// Generate a thumbnail overload method 2, save the thumbnail file to the specified path /// /// The width of the thumbnail /// thumbnail /// thumbnail saved full file name, (with path), parameter format: D: / Images / filename.jpg /// returns true if successful, otherwise returns false public bool GetReducedImage (int Width, int Height, string targetFilePath) {try {Image ReducedImage; Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort (ThumbnailCallback); ReducedImage = ResourceImage.GetThumbnailImage (Width, Height, callb, IntPtr.Zero); ReducedImage.Save (@ targetFilePath, ImageFormat.Jpeg);

Reducedimage.dispose (); return true;} catch (exception e) {errmessage = E.MESSAGE; RETURN FALSE;}}

///

/// Generates a thumbnail overload method 3, returns the image object /// /// thumbnail, such as: In 80, fill in 0.8 /// thumbnails public image getreducedImage (image reducedimage);

Image.getthumbnailimageabort callb = new image.getthumbnailImageabort (thumbnailcallback);

ImageWidth = Convert.ToInt32 (ResourceImage.Width * Percent); ImageHeight = Convert.ToInt32 (ResourceImage.Width * Percent); ReducedImage = ResourceImage.GetThumbnailImage (ImageWidth, ImageHeight, callb, IntPtr.Zero); return ReducedImage;} catch (Exception e) {errmessage = E.Message; return null;}}

///

/// Generates a thumbnail overload method 4, returns the image object /// /// thumbnail, such as: Need 100 In the 80th, fill in 0.8 /// Thumbnail saved full file name, (with path), parameter format: D: /images/filename.jpg / // successful return true, otherwise returns false public bool GetReducedImage (double Percent, string targetFilePath) {try {Image ReducedImage; Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort (ThumbnailCallback);

ImageWidth = Convert.ToInt32 (ResourceImage.Width * Percent); ImageHeight = Convert.ToInt32 (ResourceImage.Width * Percent); ReducedImage = ResourceImage.GetThumbnailImage (ImageWidth, ImageHeight, callb, IntPtr.Zero);

Reducedimage.save (@ target.jpeg); reducedimage.dispose (); return true;} catch (exception e) {errmessage = E.MESSAGE; RETURN FALSE;}}

}

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

New Post(0)