123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
-
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- namespace iNethinkCMS.Helper
- {
- public class Helper_Thumbnails
- {
-
-
-
-
-
-
-
-
-
- public static void CreationThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode, int quality)
- {
- System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
- int newWidth = width;
- int newHeight = height;
- int x = 0;
- int y = 0;
- int ow = originalImage.Width;
- int oh = originalImage.Height;
- if (mode == "1")
- {
- if (height * ow > width * oh)
- {
- mode = "3";
- }
- else
- {
- mode = "4";
- }
- }
- switch (mode)
- {
- case "2":
- break;
- case "3":
- newHeight = originalImage.Height * width / originalImage.Width;
- break;
- case "4":
- newWidth = originalImage.Width * height / originalImage.Height;
- break;
- case "5":
- if ((double)originalImage.Width / (double)originalImage.Height > (double)newWidth / (double)newHeight)
- {
- oh = originalImage.Height;
- ow = originalImage.Height * newWidth / newHeight;
- y = 0;
- x = (originalImage.Width - ow) / 2;
- }
- else
- {
- ow = originalImage.Width;
- oh = originalImage.Width * height / newWidth;
- x = 0;
- y = (originalImage.Height - oh) / 2;
- }
- break;
- default:
- break;
- }
- System.Drawing.Image bitmap = new System.Drawing.Bitmap(newWidth, newHeight);
- System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- g.Clear(System.Drawing.Color.Transparent);
- g.DrawImage(originalImage,
- new System.Drawing.Rectangle(0, 0, newWidth, newHeight),
- new System.Drawing.Rectangle(x, y, ow, oh),
- System.Drawing.GraphicsUnit.Pixel
- );
- System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
- ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
- System.Drawing.Imaging.ImageCodecInfo ici = GetImageEncodersInfo(Path.GetExtension(originalImagePath));
- try
- {
- bitmap.Save(thumbnailPath, ici, ep);
- }
- catch (System.Exception e)
- {
- throw e;
- }
- finally
- {
- ep.Dispose();
- originalImage.Dispose();
- bitmap.Dispose();
- g.Dispose();
- }
- }
-
-
-
-
- private static ImageCodecInfo GetImageEncodersInfo(string byExt)
- {
- switch (byExt)
- {
- case ".jpg":
- return ImageCodecInfo.GetImageEncoders()[1];
- case ".gif":
- return ImageCodecInfo.GetImageEncoders()[2];
- case ".png":
- return ImageCodecInfo.GetImageEncoders()[4];
- case ".bmp":
- return ImageCodecInfo.GetImageEncoders()[0];
- default:
- return ImageCodecInfo.GetImageEncoders()[1];
- }
- }
- }
- }
|