using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Windows.Forms; namespace LSFileClient { public static class ImageUtil { public enum ImgDeep { D8, D32 } public static void Format(string path, string savePath, ImageFormat format, ImgDeep deep = ImgDeep.D32, int nwidth = 0, int nheight = 0) { //string a = path + "\r\n" + savePath + "\r\n" + format + "\r\n" + deep + "\r\n" + nwidth + "\r\n" + nheight; //MessageBox.Show(a); Bitmap srcImage = null; if (nwidth > 0 && nheight > 0) { srcImage = ReSize(path, nwidth, nheight); } else { srcImage = new Bitmap(path); } if (deep == ImgDeep.D8) { // Bitmap b8Img = srcImage.Clone(new Rectangle(0, 0, srcImage.Width, srcImage.Height), PixelFormat.Format8bppIndexed); string temp = savePath + ".temp"; srcImage.Format(temp, ImageFormat.Gif);//gif为8位图 Format(temp, savePath, format); File.Delete(temp); // b8Img.Format(savePath,format); return; } srcImage.Format(savePath, format); } public static Bitmap ReSize(string path,int nwidth,int nheight) { System.Drawing.Image iSource = System.Drawing.Image.FromFile(path); int sW = 0, sH = 0; Size temSize = new Size(iSource.Width, iSource.Height); if (temSize.Width > nheight || temSize.Width > nwidth) { if ((temSize.Width * nheight) > (temSize.Height * nwidth)) { sW = nwidth; sH = (nwidth * temSize.Height) / temSize.Width; } else { sH = nheight; sW = (temSize.Width * nheight) / temSize.Height; } } else { sW = temSize.Width; sH = temSize.Height; } Bitmap ob = new Bitmap(sW, sH); Graphics g = Graphics.FromImage(ob); // g.Clear(Color.WhiteSmoke); g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; // g.InterpolationMode = InterpolationMode.Default; g.DrawImage(iSource, new Rectangle(0, 0, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel); g.Dispose(); return ob; } public static void Format(this Bitmap bmp, string filename, ImageFormat format) { EncoderParameters encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); bmp.Save(filename, GetEncoder(format), encoderParameters); bmp.Dispose(); encoderParameters.Param[0].Dispose(); encoderParameters.Dispose(); } public static void Format(this Bitmap bmp, Stream stream, ImageFormat format) { EncoderParameters encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); bmp.Save(stream, GetEncoder(format), encoderParameters); bmp.Dispose(); encoderParameters.Param[0].Dispose(); encoderParameters.Dispose(); } public static ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { return codec; } } return null; } } }