MosaicHelper.AdjustTobMosaic(
 @"C:\Users\xxxue\Desktop\QQ图片20180704142029.jpg", 
 @"C:\Users\xxxue\Desktop\xxxx.jpg",
 20);
using System;
using System.Drawing;

namespace ConsoleApp2
{
    public class MosaicHelper
    {
        /// <summary>
        /// 马赛克处理图片  
        /// </summary>
        /// <param name="ImgFile">原图片</param>
        /// <param name="sImgPath">加码图片</param>
        /// <param name="effectWidth"> 影响范围 每一个格子数</param>
        public static string[] AdjustTobMosaic(string ImgFile, string sImgPath, int effectWidth)
        {
            //设置马赛克百分比宽高(0~1f)
            Single maWidth = 1f, maHeight = 1f;

            string[] resMsg = new[] { "成功", "null" };
            using (Bitmap bitmap = new Bitmap(Image.FromFile(ImgFile)))
            {
                try
                {
                    // 差异最多的就是以照一定范围取样 之后直接去下一个范围
                    for (int heightOfffset = 0; heightOfffset < bitmap.Height * maHeight; heightOfffset += effectWidth)//可以调整大码区域,调整打码宽高
                    {
                        for (int widthOffset = 0; widthOffset < bitmap.Width * maWidth; widthOffset += effectWidth)
                        {
                            int avgR = 0, avgG = 0, avgB = 0;
                            int blurPixelCount = 0;

                            for (int x = widthOffset; (x < widthOffset + effectWidth && x < bitmap.Width); x++)
                            {
                                for (int y = heightOfffset; (y < heightOfffset + effectWidth && y < bitmap.Height); y++)
                                {
                                    System.Drawing.Color pixel = bitmap.GetPixel(x, y);
                                    avgR += pixel.R;
                                    avgG += pixel.G;
                                    avgB += pixel.B;
                                    blurPixelCount++;
                                }
                            }
                            // 计算范围平均
                            avgR = avgR / blurPixelCount;
                            avgG = avgG / blurPixelCount;
                            avgB = avgB / blurPixelCount;
                            // 所有范围内都设定此值
                            for (int x = widthOffset; (x < widthOffset + effectWidth && x < bitmap.Width); x++)
                            {
                                for (int y = heightOfffset; (y < heightOfffset + effectWidth && y < bitmap.Height); y++)
                                {

                                    System.Drawing.Color newColor = System.Drawing.Color.FromArgb(avgR, avgG, avgB);
                                    bitmap.SetPixel(x, y, newColor);
                                }
                            }
                        }
                    }

                    //保存文件
                    bitmap.Save(sImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                catch (Exception ex)
                {
                    resMsg[0] = "失败";
                    resMsg[1] = ex.Message;
                }
            }

            return resMsg;

        }

    }
}


最后修改于 2022-01-03 22:05:22
上一篇