using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bt
{
    public partial class Form1 : Form
    {
        string selname;
        Image myimage;

        public Form1()
        {
            InitializeComponent();
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                // 设置对话框标题
                openFileDialog.Title = "选择要打开的文件";

                // 添加过滤器(如果需要)
                openFileDialog.Filter = "图像文件 (*.jpg; *.png)|*.jpg;*.png";

                // 判断用户点击了确定按钮
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取用户选择的文件路径
                    string filePath = openFileDialog.FileName;
                    selname = openFileDialog.SafeFileName;
                    //MessageBox.Show(filePath);

                    try
                    {
                        myimage = Image.FromFile(filePath); // 根据文件路径加载图像
                        pictureBox1.Image = myimage; // 设置 PictureBox 控件的 Image 属性为加载的图像
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("无法加载图片:" + ex.Message); // 若发生错误,则显示提示信息
                    }
                }
                else
                {
                    
                }
            }
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            //Graphics g = Graphics.FromHwnd(pictureBox1.Handle);
            Graphics g = Graphics.FromImage(myimage);
            g.DrawString("单击了", this.Font, Brushes.Black, e.Location.X, e.Location.Y);
            pictureBox1.Image = myimage;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            textBox1.Text =  e.Location.X.ToString() + "," + e.Location.Y.ToString();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g1 = e.Graphics;

            g1.DrawString("测试", this.Font, Brushes.Black, 100, 50);
            g1.DrawString(selname, this.Font, Brushes.Black, 150, 100);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                using (SaveFileDialog saveFileDialog = new SaveFileDialog())
                {
                    saveFileDialog.Filter = "JPEG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png";
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        string savePath = saveFileDialog.FileName; // 获取保存路径及文件名

                        // 根据选择的格式保存图像
                        switch (System.IO.Path.GetExtension(savePath).ToLowerInvariant())
                        {
                            case ".jpg":
                            case ".jpeg":
                                ((Bitmap)this.pictureBox1.Image).Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                                break;

                            case ".png":
                                ((Bitmap)this.pictureBox1.Image).Save(savePath, System.Drawing.Imaging.ImageFormat.Png);
                                break;

                            default:
                                MessageBox.Show("Invalid file format!");
                                return;
                        }
                        MessageBox.Show("Image saved successfully.");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred while saving the image:\n\n{ex.Message}");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            myimage.Save("E:\\temp\\draw1.jpg");
        }
    }
}

首先是在图片控件的paint事件里输出文字"测试"和文件名,还没加载图片的时候会触发paint事件,输出文字"测试",此时还没有打开图片,文件名是空;

C#在图片上输出文字以及保存的问题-LMLPHP

打开图片之后又触发paint事件,同时输出了文件名;

C#在图片上输出文字以及保存的问题-LMLPHP 

    但是这只是输出在图片控件上,并没有绘制在图片上,如果保存图片不会保存文字;

在图片控件鼠标单击事件中输出文字;

如果从图片控件获取Graphics对象进行绘制,

    Graphics g = Graphics.FromHwnd(pictureBox1.Handle);
    g.DrawString("单击了", this.Font, Brushes.Black, e.Location.X, e.Location.Y);

对于当前图片效果如下;

C#在图片上输出文字以及保存的问题-LMLPHP 

保存图片有两种方法,一种是调用图片控件的save方法,一种是调用Image对象的save方法;此时保存图片只会保存原图,不带有任何绘制内容;

如果从Image对象获取Graphics对象进行绘制,执行下面三句,

            Graphics g = Graphics.FromImage(myimage);
            g.DrawString("单击了", this.Font, Brushes.Black, e.Location.X, e.Location.Y);
            pictureBox1.Image = myimage;

对于当前图片效果如下,输出的文字小一些;每次绘制之后要重新赋值一下,pictureBox1.Image = myimage;

C#在图片上输出文字以及保存的问题-LMLPHP 

此时调用2种保存方法效果是一样的,都保存了绘制的文字;

C#在图片上输出文字以及保存的问题-LMLPHP 

02-02 18:13