将图片保存到bmp

时间:2013-12-10 13:50:54

标签: c# visual-studio-2010 save paint

我有这段代码:

Dictionary<int, Class1> MyDict = new Dictionary<int, Class1>();

我得到鼠标位置

private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {               Mycl.X1 = e.X;
                    Mycl.X2 = e.Y;}
private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
                    Mycl.X3 = e.X;
                    Mycl.X4 = e.Y;
        MyDict.Add(MyDict.Count + 1, Mycl);
        panel1.Invalidate();
        Mycl = new Class1();
    }
private void panel1_Paint(object sender, PaintEventArgs e)
    {
        if (panel1.BackgroundImage == null)
        {
            panel1.BackgroundImage = new Bitmap(this.Width, this.Height);
        }
        Pen MyPen = new Pen(Color.Black);
        Graphics G = Graphics.FromImage(panel1.BackgroundImage);
        foreach (KeyValuePair<int, Class1> kvp in MyDict)
        {
            Point pl1 = new Point(MyDict[kvp.Key].X1, MyDict[kvp.Key].X2);
            Point pl2 = new Point(MyDict[kvp.Key].X3, MyDict[kvp.Key].X4);
            //e.Graphics.DrawLine(MyPen, pl1, pl2);
            G.DrawLine(MyPen, pl1, pl2);
        }
    }

但是当我画这幅画时,我遇到了麻烦。

当我使用e.graphics.drawline()行绘制时

但是G.drawline()没有线。

最后,我需要保存我的照片,但是e.drawline我有一张黑色照片。

2 个答案:

答案 0 :(得分:0)

您需要区分屏幕图形和位图对象图形......

这是一个例子:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Button button1;
        private PictureBox pictureBox1;
        private Bitmap bmp;
        private Point? p1 = null;


        public Form1()
        {
            InitializeComponent();
            bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
            }

            pictureBox1.Image = bmp;
        }

        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Location = new System.Drawing.Point(0, 23);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(477, 352);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
            this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
            this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
            // 
            // button1
            // 
            this.button1.Dock = System.Windows.Forms.DockStyle.Top;
            this.button1.Location = new System.Drawing.Point(0, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(477, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "write bitmap to file";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(477, 375);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.button1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Name = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }



        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            p1 = pictureBox1.PointToClient(MousePosition);
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Point p2 = pictureBox1.PointToClient(MousePosition);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawLine(Pens.Black, p1.Value, pictureBox1.PointToClient(MousePosition));
            }
            p1 = null;
            pictureBox1.Invalidate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (p1 != null)
            {
                e.Graphics.DrawLine(Pens.Black, p1.Value, pictureBox1.PointToClient(MousePosition));
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox1.Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var dialog=new SaveFileDialog();
            dialog.DefaultExt = "bmp";
            dialog.ValidateNames = true;
            dialog.Filter = "Bitmap|*.bmp";
            dialog.AddExtension = true;
            dialog.OverwritePrompt=true;
            var result=dialog.ShowDialog(this);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                using (var fs = dialog.OpenFile())
                {
                    bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp);
                }
            }
        }


    }
}

在示例中,您可以看到2个不同的DrawLine(...)调用...屏幕绘制中的paint事件句柄中的那个调用,它不会保留在位图中...鼠标中的一个处理修改在位图......

答案 1 :(得分:0)

试试这个...注意差异,我还为panel1添加了一个MouseMove()处理程序:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private class Class1
    {
        public int X1, X2, X3, X4;
    }

    private Class1 Mycl = new Class1();
    private Dictionary<int, Class1> MyDict = new Dictionary<int, Class1>();

    private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Mycl.X1 = e.X;
            Mycl.X2 = e.Y;
            Mycl.X3 = e.X;
            Mycl.X4 = e.Y;

            Point pt1 = panel1.PointToScreen(new Point(Mycl.X1, Mycl.X2));
            Point pt2 = panel1.PointToScreen(new Point(Mycl.X3, Mycl.X4));
            ControlPaint.DrawReversibleLine(pt1, pt2, panel1.BackColor);
        }
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Point pt1 = panel1.PointToScreen(new Point(Mycl.X1, Mycl.X2));
            Point pt2 = panel1.PointToScreen(new Point(Mycl.X3, Mycl.X4));
            ControlPaint.DrawReversibleLine(pt1, pt2, panel1.BackColor);

            Mycl.X3 = e.X;
            Mycl.X4 = e.Y;
            pt2 = panel1.PointToScreen(new Point(Mycl.X3, Mycl.X4));
            ControlPaint.DrawReversibleLine(pt1, pt2, panel1.BackColor);
        }
    }

    private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Mycl.X3 = e.X;
            Mycl.X4 = e.Y;
            MyDict.Add(MyDict.Count + 1, Mycl);
            panel1.Invalidate();
            Mycl = new Class1();
        }
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        using (Pen MyPen = new Pen(Color.Black))
        {
            foreach (KeyValuePair<int, Class1> kvp in MyDict)
            {
                Point pl1 = new Point(kvp.Value.X1, kvp.Value.X2);
                Point pl2 = new Point(kvp.Value.X3, kvp.Value.X4);
                e.Graphics.DrawLine(MyPen, pl1, pl2);
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
        panel1.DrawToBitmap(bmp, new Rectangle(new Point(0,0), panel1.Size));
        bmp.Save(@"C:\Users\Mike\Pictures\SomeImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    }

}