代码改变世界

图片水印

2019-03-25 16:13  .net小跟班(杜)  阅读(223)  评论(0编辑  收藏  举报

水印文字

//加水印
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dia = openFileDialog1.ShowDialog();
            if (dia == DialogResult.OK)
            {
                string text = txtFont.Text;//水印内容
                //需要操作的图片路径
                string path = openFileDialog1.FileName;
                //图片对象的创建
                Bitmap img = new Bitmap(path);
                //画布工具
                Graphics g = Graphics.FromImage(img);
                Font f = new Font("微软雅黑",20);
                Brush b = new SolidBrush(Color.Red);
                int x = img.Width / 2;
                int y = img.Height / 2;
                g.DrawString(text,f,b,x,y);
                string xinPath = path.Substring(0,path.LastIndexOf(".")+1)+".jpg";
                img.Save(xinPath,ImageFormat.Jpeg);
                MessageBox.Show("加水完成");
            }
        }

水印图片

public Form1()
        {
            InitializeComponent();
        }
        string waterImg;//水印图
        string mainImg;//主图
        //水印图
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dia = openFileDialog1.ShowDialog();
            if (dia == DialogResult.OK)
            {
                waterImg = openFileDialog1.FileName;
                txtWater.Text = openFileDialog1.FileName;
            }
        }
        //主图
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dia = openFileDialog1.ShowDialog();
            if (dia == DialogResult.OK)
            {
                mainImg = openFileDialog1.FileName;
                txtMain.Text = openFileDialog1.FileName;
            }
        }
        //合并图片
        private void button3_Click(object sender, EventArgs e)
        {
            //主图
            Bitmap main = new Bitmap(mainImg);
            Image image = Image.FromFile(waterImg);
            Bitmap water = new Bitmap(image,100,100);//水印
            Graphics g = Graphics.FromImage(main);
            g.DrawImage(water,main.Width-water.Width,main.Height-water.Height);

            string path = mainImg.Substring(0,mainImg.LastIndexOf(".")+1)+".jpg";
            main.Save(path,ImageFormat.Jpeg);
            MessageBox.Show("合成成功");
        }