C# .net 高清压缩图片 合并图片方法
/// <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; }