using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; /// ///ImageUtility 的摘要说明 /// public class ImageUtility { public static string MakeThumbImage(string Path, string StPath, int width, int height, string mode) { string messages = string.Empty; System.Drawing.Image image = System.Drawing.Image.FromFile(Path); int tw = width; int th = height; int sw = image.Width; int sh = image.Height; int x = 0, y = 0; switch (mode) { case "HW": break; case "W": th = image.Height * width / image.Width; break; case "H": tw = image.Width * height / image.Height; break; case "CUT": if ((double)tw / (double)th < (double)width / (double)height) { sw = image.Width; sh = image.Width * height / tw; x = 0; y = (image.Height - sh) / 2; } else { sh = image.Height; sw = image.Height * width / th; y = 0; x = (image.Width - sw) / 2; } break; default: break; } System.Drawing.Image bitmap = new System.Drawing.Bitmap(tw, th); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(System.Drawing.Color.Transparent); g.DrawImage(image, new System.Drawing.Rectangle(x, y, tw, th), new System.Drawing.Rectangle(x, y, sw, sh), System.Drawing.GraphicsUnit.Pixel); try { bitmap.Save(StPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { messages = ex.Message; } finally { image.Dispose(); bitmap.Dispose(); g.Dispose(); if (messages == string.Empty) { messages = "成功生成!"; } } return messages; } public static string MakeThumbImage(Stream stream, string path, int width, int height, string mode) { string messages = string.Empty; System.Drawing.Image image = System.Drawing.Image.FromStream(stream); int tw = width; int th = height; int sw = image.Width; int sh = image.Height; int x = 0, y = 0; switch (mode) { case "HW": if (width > sw || height > sh) { tw = image.Width; th = image.Height; } break; case "W": th = image.Height * width / image.Width; break; case "H": tw = image.Width * height / image.Height; break; case "CUT": if ((double)tw / (double)th < (double)width / (double)height) { sw = image.Width; sh = image.Width * height / tw; x = 0; y = (image.Height - sh) / 2; } else { sh = image.Height; sw = image.Height * width / th; y = 0; x = (image.Width - sw) / 2; } break; default: break; } System.Drawing.Image bitmap = new System.Drawing.Bitmap(tw, th); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(System.Drawing.Color.Transparent); g.DrawImage(image, new System.Drawing.Rectangle(x, y, tw, th), new System.Drawing.Rectangle(x, y, sw, sh), System.Drawing.GraphicsUnit.Pixel); try { bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { messages = ex.Message; } finally { image.Dispose(); bitmap.Dispose(); g.Dispose(); if (messages == string.Empty) { messages = "成功生成!"; } } return messages; } }