/// <summary> /// 合併寬度同樣的圖片 /// </summary> /// <param name="imgUrls">多張圖片,按順序合併</param> /// <param name="mergeImgPath">合成圖片的具體地址</param> /// <returns></returns> public bool MergeImg(List<string> imgUrls, string mergeImgPath) { imgUrls = imgUrls.OrderBy(c => c, new SemiNumericComparer()).ToList(); using (var file = File.OpenWrite(mergeImgPath)) { try { Image image1 = System.Drawing.Image.FromFile(imgUrls[0]); var width = image1.Width; var height = image1.Height; //計算總體 for (int i = 1; i < imgUrls.Count(); i++) { using (Image tempImg = System.Drawing.Image.FromFile(imgUrls[i])) { height += tempImg.Height; } } using (Bitmap b = new Bitmap(width, height)) using (Graphics g = Graphics.FromImage(b)) { g.CompositingMode = CompositingMode.SourceCopy; //先畫第一個圖片 g.DrawImageUnscaled(image1, 0, 0); //目前的高度 var currentHeight = image1.Height; //畫多張圖片 for (int j = 1; j < imgUrls.Count(); j++) { using (Image currentImg = System.Drawing.Image.FromFile(imgUrls[j])) { g.DrawImageUnscaled(currentImg, 0, currentHeight); currentHeight += currentImg.Height; } } b.Save(file, ImageFormat.Jpeg); } return true; ; } catch (Exception ex) { return false; } } } /// <summary> /// Resize the image to the specified width and height. /// </summary> /// <param name="imagePath">The image to resize.</param> /// <param name="width">The width to resize to.</param> /// <param name="height">The height to resize to.</param> /// <returns>The resized image.</returns> public string ResizeImage(string imagePath, int width, int height) { if (imagePath.ToLower().Contains(".mp4")) { return ""; } System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath); var destRect = new Rectangle(0, 0, width, height); var destImage = new Bitmap(width, height); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } } #region 提升壓縮質量 EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = 1;//設置壓縮的比例1-100,壓縮比例越小越好 EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; #endregion #region 設置imgcodeinfo爲JPEG ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[x]; break; } } #endregion var resizeImagePath = string.Empty; if (jpegICIinfo != null) { resizeImagePath = imagePath.Replace("jpg", width + "x" + height + ".jpg"); destImage.Save(resizeImagePath, jpegICIinfo, ep); } return resizeImagePath; }