在图形对象中绘制时鼠标移动太快

时间:2015-07-04 22:15:30

标签: c#

我正在和我的朋友做一个C#项目。我们必须签名并将其保存到JPG文件中。我们不知道如何做到这一点,但至少我们正在努力。我们有两个问题:

1

绘制图形时,如果鼠标移动太快,则鼠标移动事件并非所有点都被捕获,结果图像是分离的点。我怎么能改善这个?

这是我的代码:

    private void ingresoFirma_Load(object sender, EventArgs e)
    {
        myBrush = new SolidBrush(Color.Black);
        myGraphics = panel1.CreateGraphics();

    }

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        isDrawing = true;
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDrawing)
        {
            myGraphics.FillEllipse(myBrush, e.X, e.Y, 10, 10);
        }
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        isDrawing = false;
    }
  1. 第二个问题是我们不知道如何将图形保存为.jpg图像。我们使用了这样的东西:

        private void saveSign_Click(object sender, EventArgs e)
    {
        Bitmap signature = new Bitmap(100,100,myGraphics);
    
        signature.Save("c:\\myBitmap.bmp");
    
    }
    
  2. 但它保存了一张空白图片。

2 个答案:

答案 0 :(得分:1)

这是让你入门的东西......

enter image description here

public partial class ingresoFirma : Form
{

    private List<Point> stroke = null;
    private List<List<Point>> Strokes = new List<List<Point>>();

    public ingresoFirma()
    {
        InitializeComponent();
    }

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            stroke = new List<Point>();
            stroke.Add(new Point(e.X, e.Y));
        }
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            stroke.Add(new Point(e.X, e.Y));
            if (stroke.Count == 2)
            {
                Strokes.Add(stroke);
            }
            panel1.Refresh();
        }
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            stroke = null;
        }
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        foreach(List<Point> curStroke in Strokes)
        {
            e.Graphics.DrawLines(Pens.Black, curStroke.ToArray());
        }
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        Strokes.Clear();
        panel1.Refresh();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "JPG Files(*.JPG)|*.JPG";
        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
            panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
            bmp.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }

}

答案 1 :(得分:0)

使用Control的DrawToBitmap()方法,您可以保存图像:

     Bitmap theBitmap = new Bitmap(panel1.Width, panel1.Height)) ;
     panel1.DrawToBitmap(theBitmap, new Rectangle(0, 0, bmp.Width, bmp.Height));
     theBitmap.Save("c:\\myBitmap.bmp");