C#去除图片水印之马赛克解决方案(右下角去除方法)
看下效果
处理效果如上图片,怎么样还可以吧,
大家可以根据自己的情况调整级别就行了。
代码实现如下
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
namespace AutoImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string path = string.Empty;
//选择图片
private void button1_Click(object sender, EventArgs e)
{
//选择文件
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
//文件格式
openFileDialog.Filter = "所有文件|*.*";
//还原当前目录
openFileDialog.RestoreDirectory = true;
//默认的文件格式
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
path = openFileDialog.FileName;
pictureBox1.BackgroundImage = Image.FromFile(path);
}
}
//窗体加载事件
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 马赛克处理
/// </summary>
/// <param name="bitmap">要处理的图片</param>
/// <param name="effectWidth"> 影响范围 每一个格子数 </param>
/// <returns></returns>
public System.Drawing.Bitmap AdjustTobMosaic(System.Drawing.Bitmap bitmap, int effectWidth)
{
// 差异最多的就是以照一定范围取样 玩之后直接去下一个范围
for (int heightOfffset = bitmap.Height - bitmap.Height / 3; heightOfffset < bitmap.Height; heightOfffset += effectWidth)
{
for (int widthOffset = bitmap.Width -Convert.ToInt32( bitmap.Width*0.7); widthOffset < bitmap.Width; 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);
}
}
}
}
return bitmap;
}
private void button3_Click(object sender, EventArgs e)
{
Bitmap newimag = new Bitmap(path);
pictureBox2.BackgroundImage = AdjustTobMosaic(newimag, Convert.ToInt32(textBox1.Text.Trim()));
}
}
public enum WaterPositionMode
{
LeftTop,
LeftBottom,
RightTop,
RightBottom,
Center
}
}
源码下载
图片马赛克处理.zip
(12.44 KB, 下载次数: 77)
只有年费会员,和终身VIP才能下载
|