static void Main(string[] args) { var tempPath = @"c:\Temp"; if (Directory.Exists(tempPath)) { Directory.Delete(tempPath,true); } Directory.CreateDirectory(tempPath); ZipFile.ExtractToDirectory(@"c:\test.xmind", tempPath); //缩略图地址 var thumbPath = tempPath + "\\Thumbnails\\thumbnail.png"; var targetThumbPath = tempPath + "\\Thumbnails\\160X120.png"; var fi=new FileInfo(targetThumbPath); if (fi.Exists) { fi.IsReadOnly = false; fi.Delete(); } int width; int height; ThumbImage.GetMicroImage(thumbPath, targetThumbPath, 160, 120, out width, out height); Console.WriteLine("缩略图:"+ targetThumbPath); Console.WriteLine("预览图:" + thumbPath + " 原图宽:" + width + ",原图高:" + height); //附件 var attachmentsPath = new DirectoryInfo(tempPath + "\\attachments"); var count = 0; foreach (var o in attachmentsPath.GetFiles()) { count++; Console.WriteLine("附件"+count+" : "+o.Name); } Console.WriteLine("成功!"); Console.ReadKey(); }
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace ConsoleApplication2 { internal class ThumbImage { /// <summary> /// 高质量缩放图片 /// </summary> /// <param name="originFilePath">源图的路径</param> /// <param name="targetFilePath">存储缩略图的路径</param> /// <param name="destWidth">缩放后图片宽度</param> /// <param name="destHeight">缩放后图片高度</param> /// <param name="width"></param> /// <param name="height"></param> /// <returns>表明此次操作是否成功</returns> public static bool GetMicroImage(string originFilePath, string targetFilePath, int destWidth, int destHeight, out int width, out int height) { width = 0; height = 0; try { using (Image imgSource = new Bitmap(originFilePath)) { width = imgSource.Width; height = imgSource.Height; //System.Drawing.Image imgSource = b; int sW = 0, sH = 0; // 按比例缩放 var sWidth = imgSource.Width; var sHeight = imgSource.Height; if (sHeight > destHeight || sWidth > destWidth) { if ((sWidth*destHeight) > (sHeight*destWidth)) { sW = destWidth; sH = (destWidth*sHeight)/sWidth; } else { sH = destHeight; sW = (sWidth*destHeight)/sHeight; } } else { sW = sWidth; sH = sHeight; } var outBmp = new Bitmap(destWidth, destHeight); var g = Graphics.FromImage(outBmp); g.Clear(Color.Transparent); // 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgSource, new Rectangle((destWidth - sW)/2, (destHeight - sH)/2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel); g.Dispose(); // 以下代码为保存图片时,设置压缩质量 var encoderParams = new EncoderParameters(); var quality = new long[1]; quality[0] = 100; var encoderParam = new EncoderParameter(Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; outBmp.Save(targetFilePath); outBmp.Dispose(); } //压缩一下PNG图 //CompressPng(targetFilePath, targetFilePath); return true; } catch (Exception) { return false; } } } }