C#简单的在图片上添加文字并保存

来源:互联网 发布:大数据神经网络 编辑:程序博客网 时间:2024/05/16 01:11

二话不说;直接上代码:


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.Net;


namespace 图片加水印
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
         static int x=0, y=0, h = 1368, w = 768;
        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            SolidBrush mybrush;
                mybrush = new SolidBrush(Color.Lime);  //设置默认画刷颜色
            Font myfont;
                myfont = new Font("黑体", 14);         //设置默认字体格式
            g.DrawString(textBox2.Text, myfont, mybrush, new Rectangle(x +500, y+50, w+500, h+50));
            pictureBox1.Refresh();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
            openFileDialog1.ShowDialog();
            if (openFileDialog1.FileName != null)
            {
                textBox1 .Text  = openFileDialog1.FileName;
                pictureBox1.Image = Image.FromFile(textBox1.Text);
            }
        }


        private void button3_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "JPG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image.Save(saveFileDialog1.FileName);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox2.Text  = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault<IPAddress>(a => a.AddressFamily.ToString().Equals("InterNetwork")).ToString();
            pictureBox1.Image = Image.FromFile(textBox1.Text);
        }
        }

}