Noidz
Noidz

Reputation: 35

Mouse moving too fast when drawing in a graphics Object

i'm doing a C# project with my friend. We have to take a signature and save it to a JPG file. We don't have much idea of how doing this, but at least we are trying. We have 2 problems:

1.

When drawing the Graphics, if the mouse moves too fast, not all the points are caught by the mousemove Event and the result image are separated points. How can i improve this?

Here is my code:

    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. The second problem is that we have no idea of how to save the Graphics to a .jpg image. We used something like this:

        private void saveSign_Click(object sender, EventArgs e)
    {
        Bitmap signature = new Bitmap(100,100,myGraphics);
    
        signature.Save("c:\\myBitmap.bmp");
    
    }
    

But it saves a blank image.

Upvotes: 0

Views: 369

Answers (2)

Idle_Mind
Idle_Mind

Reputation: 39152

Here's something to get you started...

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);
        }
    }

}

Upvotes: 1

Graffito
Graffito

Reputation: 1718

With the DrawToBitmap() method of Control, you can save the image:

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

Upvotes: 0

Related Questions