专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

C#开发winform去除图片水印小工具

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 1:18:10       共计:3629 浏览



效果图:


思路:


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);


}

}


版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:C#去除图片水印之马赛克解决方案 | ·下一条:使用C#处理PPT水印去除水印效果(文本水印、图片水印)

Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有