Click here to Skip to main content
16,020,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code that draws over pictureBox but after draw the line shifts the image loaded into pictureBox. I wish the image loaded in pictureBox whole time he was properly scaled. For example picture 3000x2000 visible in the 800x600 pictureBox.

Thank you for any help.

I have code to draw lines


C#
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
       {
           pointArray[iNumberofClicks].X = e.X;
           pointArray[iNumberofClicks].Y = e.Y;
           Pen PenSpikes = new Pen(Color.Green);
           SolidBrush solidBrush = new SolidBrush(Color.Blue);
           iNumberofClicks++;

           if (iNumberofClicks > 1)
           {
               Point[] CurrentpointArray = new Point[iNumberofClicks];

               for (int i = 0; i < iNumberofClicks; i++)
               {
                   CurrentpointArray[i].X = pointArray[i].X;
                   CurrentpointArray[i].Y = pointArray[i].Y;
               }

               Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height);
               Graphics offScreenDC = Graphics.FromImage(canvas);

               // New line!
               offScreenDC.DrawImage(pictureBox1.Image, new Point());
               offScreenDC.DrawLines(PenSpikes, CurrentpointArray);
               pictureBox1.Image = canvas;
               offScreenDC.Dispose();
           }

       }
Posted

1 solution

I think you need to draw set of lines while you put points on a picture_Box by clicking on it. What you do with this code is, you collect the set of points as the mouse clicks, copy the set of points in to a local array, create an image with drawing lines with the local array and then set the image to the pictureBox1.

I think the best way is to do this is drawing lines with in the "Paint" event of the picture box.

change the mouse event as the follow.

C#
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
    pointArray[iNumberofClicks].X = e.X;
    pointArray[iNumberofClicks].Y = e.Y;
    iNumberofClicks++;
    pictureBox1.Refresh();
}

//this is the registered paint event of the pictureBox1
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if(iNumberofClicks >1)
    {
        Pen PenSpikes = new Pen(Color.Green);
        Graphics g = e.Graphics;
        g.DrawLines(PenSpikes, pointArray);
    }
}


When ever you put a new point, you can see the new line. If you need to scale up the drawing you can simply multiply the x and y coordinates of the points in the array by the scale factor you need and then call pictureBox1.Refresh().

C#
private void scaleDrawing(double factor)
{
    for (int i = 0; i < iNumberofClicks; i++)
    {
        pointArray[i].X = (int)pointArray[i].X*factor;
        pointArray[i].Y = (int)pointArray[i].Y*factor;
    }
    pictureBox1.Refresh();
}


This is how I understand your question. Please clarify if I'm wrong.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900