縮圖
using System; using System.Drawing; using System.IO; /// <summary> /// 縮圖 /// </summary> public class ResizeImage { public string ImgSrc; public string ImgOut; public string copyrsrc; public string Width; public string Height; public string cut; public string ThumbRatio; public void Do() { string strExtension = System.IO.Path.GetExtension(FC.VirtualFolder + ImgSrc); if ((strExtension.ToUpper() == ".JPG") | (strExtension.ToUpper() == ".GIF")) { if (string.IsNullOrEmpty(ImgOut)) { ImgOut = ImgSrc; } ImgSrc = FC.VirtualFolder + ImgSrc; ImgOut = FC.VirtualFolder + ImgOut; ImgSrc = ImgSrc.Replace("//", "/"); ImgOut = ImgOut.Replace("//", "/"); System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile(ImgSrc); int imageHeight = imageToBeResized.Height; int imageWidth = imageToBeResized.Width; int maxHeight = int.Parse(Width); int maxWidth = int.Parse(Height); imageHeight = (imageHeight * maxWidth) / imageWidth; imageWidth = maxWidth; if (imageHeight > maxHeight) { imageWidth = (imageWidth * maxHeight) / imageHeight; imageHeight = maxHeight; } Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight); imageToBeResized.Dispose(); if (File.Exists(ImgOut) == true) { FileInfo fi = new FileInfo(ImgOut); fi.Delete(); } bitmap.Save(ImgOut, System.Drawing.Imaging.ImageFormat.Jpeg); bitmap.Dispose(); } } // 計算維持比例的縮圖大小 private int[] getThumbnailImageScale(int maxWidth, int maxHeight, int oldWidth, int oldHeight) { int[] result = new int[] { 0, 0 }; float widthDividend, heightDividend, commonDividend; widthDividend = (float)oldWidth / (float)maxWidth; heightDividend = (float)oldHeight / (float)maxHeight; commonDividend = (heightDividend > widthDividend) ? heightDividend : widthDividend; result[0] = (int)(oldWidth / commonDividend); result[1] = (int)(oldHeight / commonDividend); return result; } }