C# 图片进行马赛克处理

 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>
        /// 马赛克处理图片 by chen_pp ,xuejie
        /// 2018-07-06
        /// </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;

        }

    }
}


这里写图片描述

转载于:https://www.cnblogs.com/xxxxue/p/11153606.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值