效果图:
思路:
1.获取图片
2.处理水印
3.保存处理的图片
代码:
获取图片:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
//文件格式
openFileDialog.Filter = "所有文件|*.*";
//还原当前目录
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string oldImgPath = openFileDialog.FileName;
this.textBox1.Text = oldImgPath;
this.textBox2.Text = this.textBox2.Text += "\r\n" + "图片路径:" + oldImgPath;
}
}
处理水印并保存:
private void button2_Click(object sender, EventArgs e)
{
Image oldImg = Image.FromFile(this.textBox1.Text);
this.textBox2.Text = this.textBox2.Text += "\r\n" + "去除水印...";
if (this.textBox3.Text == "255,255,255" || this.textBox3.Text == string.Empty)
{
this.textBox2.Text += "\r\n" + "请设置水印的rgb值";
return;
}
string[] rgbItem = this.textBox3.Text.Split(',');
Image newImg = imgHelper.SetImageColorAll(oldImg, Color.FromArgb(Convert.ToInt32(rgbItem[0]),Convert.ToInt32(rgbItem[1]),Convert.ToInt32(rgbItem[2])), Color.White, 20);
this.textBox2.Text = this.textBox2.Text += "\r\n" + "去除水印成功,正在保存...";
string savPath = this.textBox1.Text.Substring(0, this.textBox1.Text.LastIndexOf('\\'));
if (this.textBox4.Text == "格式:文件名+.png/.jpg" || this.textBox4.Text == string.Empty)
{
this.textBox2.Text += "\r\n" + "未设置保存的文件名,自动保存的文件名为:cs.png";
return;
}
savPath = Path.Combine(savPath,this.textBox4.Text);//合成路径
newImg.Save(savPath);
this.textBox2.Text = this.textBox2.Text += "\r\n" + "保存成功!";
}<br>//处理水印类
public class imgHelper
{
public static Image SetImageColorAll(Image p_Image, Color p_OdlColor, Color p_NewColor, int p_Float)
{
int _Width = p_Image.Width;
int _Height = p_Image.Height;
Bitmap _NewBmp = new Bitmap(_Width, _Height, PixelFormat.Format32bppArgb);
Graphics _Graphics = Graphics.FromImage(_NewBmp);
_Graphics.DrawImage(p_Image, new Rectangle(0, 0, _Width, _Height));
_Graphics.Dispose();
//图片背景处理的操作
BitmapData _Data = _NewBmp.LockBits(new Rectangle(0, 0, _Width, _Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
_Data.PixelFormat = PixelFormat.Format32bppArgb;
int _ByteSize = _Data.Stride * _Height;
byte[] _DataBytes = new byte[_ByteSize];
Marshal.Copy(_Data.Scan0, _DataBytes, 0, _ByteSize);
int _WhileCount = _Width * _Height;
int _Index = 0;
for (int i = 0; i != _WhileCount; i++)
{
Color _Color = Color.FromArgb(_DataBytes[_Index + 3], _DataBytes[_Index + 2], _DataBytes[_Index + 1], _DataBytes[_Index]);
if (ScanColor(_Color, p_OdlColor, p_Float))
{
_DataBytes[_Index + 3] = (byte)p_NewColor.A;
_DataBytes[_Index + 2] = (byte)p_NewColor.R;
_DataBytes[_Index + 1] = (byte)p_NewColor.G;
_DataBytes[_Index] = (byte)p_NewColor.B;
}
_Index += 4;
}
Marshal.Copy(_DataBytes, 0, _Data.Scan0, _ByteSize);
_NewBmp.UnlockBits(_Data);
return _NewBmp;
}
private static bool ScanColor(Color p_CurrentlyColor, Color p_CompareColor, int p_Float)
{
int _R = p_CurrentlyColor.R;
int _G = p_CurrentlyColor.G;
int _B = p_CurrentlyColor.B;
return (_R <= p_CompareColor.R + p_Float && _R >= p_CompareColor.R - p_Float) && (_G <= p_CompareColor.G + p_Float && _G >= p_CompareColor.G - p_Float) && (_B <= p_CompareColor.B + p_Float && _B >= p_CompareColor.B - p_Float);
}
}
Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有