C#绘图后将绘制的文件已图片保存

这个目前就当我自己笔记,都没写好,别看了,哈哈哈

1、绘图首先创建Graphies对象的引用,图片都是基于这个显示

Graphics graphics;                          // 绘图界面

 并在构造函数中实例化

graphics = this.groupBox3.CreateGraphics();        // groupBox或者其他panel中显示

2、加载图片显示盒

PictureBox pictureBox1.Image = new Bitmap(1270, 776);

3、打开文件中的图片

        private void 打开文件_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = "G:/下载/恐怖图片.jpeg";
            openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg| All Files|*.*";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;

            if (DialogResult.OK == openFileDialog.ShowDialog())
            {
                m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);
                this.AutoScroll = true;
                this.AutoScrollMinSize = new Size((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom));
                this.Invalidate();

                pictureBox1.Width = m_Bitmap.Width;
                pictureBox1.Height = m_Bitmap.Height;
                pictureBox1.Image = m_Bitmap;
                groupBox3.Width = pictureBox1.Width;
                groupBox3.Height = pictureBox1.Height;

                //Graphics graphics = groupBox3.CreateGraphics();
                graphics = Graphics.FromImage(m_Bitmap);
            }
        }

4、图像绘制

graphics.DrawLine(pen, 10, 10, 500, 500);

Graphics.FromImage(pictureBox1.Image).DrawRectangle(pen, 55, 55, 100, 100);
public Form1()
{
    InitializeComponent();    // 窗体自己的初始化
    pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);    // 初始化图片
    SolidBrush b = new SolidBrush(Color.White);    // 设定背景颜色
    // 画出矩形
    Graphics.FromImage(pictureBox1.Image).FillRectangle(b, 0, 0, pictureBox1.Width, pictureBox1.Height);
}
// 屏幕上点击参数的事件
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    // 花椭圆
    Graphics.FromImage(pictureBox1.Image).DrawEllipse(new Pen(Brushes.Red), e.X, e.Y, 1, 1);
    pictureBox1.Invalidate(); 
}

private void button1_Click(object sender, EventArgs e)
{
    pictureBox1.Image.Save(@"D:\1.jpg", ImageFormat.Jpeg);
    MessageBox.Show("ok");
}

 

 

 

你可能感兴趣的:(C#)