0

I have found this code online, and I want to use it to draw a little blue box, on top of my drawing. But When I click on the picturebox the blue box appears, but it redraws my entire picture box, this is a problem because I have a very complex picture, ans it takes a few seconds to redraw. Is there any way around the Invalidate? Perhaps a picturebox inside a picturebox, with the backpicturebox my complex drawing, and the frontpicturebox the blue box with a refresh? I cant seem to get the front picturebox backgroundcolor to transparent, could anybody help me?

private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    // Determine the initial rectangle coordinates...
    if (MeasureMentimported && !selectieBezig)
    {
        RectStartPoint = e.Location;
        //Invalidate();
        selectieBezig = true;
    }
}

// Draw Rectangle
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    //Als er niet links wordt geklikt, dan gewoon weer terug.
    if (e.Button != MouseButtons.Left)
        return;

    //Alleen als de measurementfile is geladen, gaan we kijken of er iets te selecteren valt.
    if (MeasureMentimported && selectieBezig)
    {
        Point tempEndPoint = e.Location;
        int X1 = Math.Min(RectStartPoint.X, tempEndPoint.X);
        int Y1 = Math.Min(RectStartPoint.Y, tempEndPoint.Y);
        int X2 = Math.Max(RectStartPoint.X, tempEndPoint.X);
        int Y2 = Math.Max(RectStartPoint.Y, tempEndPoint.Y);

        Rect.Location = new Point(X1, Y1);
        Rect.Size = new Size(X2 - X1, Y2 - Y1);

        picTekenvlak.Invalidate();

        //Rect.Location = new Point(
        //    Math.Min(RectStartPoint.X, tempEndPoint.X),
        //    Math.Min(0, 1000));
        //Rect.Size = new Size(
        //    Math.Abs(RectStartPoint.X - tempEndPoint.X),
        //    Math.Abs(1000));
        // MessageBox.Show("k");
    }
}

// Draw Area
//
private void pictureBox1_Paint1(object sender, System.Windows.Forms.PaintEventArgs e)
{
    if (MeasureMentimported)
    {
        if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
        {
            e.Graphics.FillRectangle(selectionBrush, Rect);
        }
    }
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    selectieBezig = false;
    if (e.Button == MouseButtons.Right)
    {
        if (Rect.Contains(e.Location))
        {
            Console.WriteLine("Right click");
        }
    }
}
Red
  • 2,728
  • 1
  • 20
  • 22
  • Here are some tips that might be helpful 1. you might try to cache your complex image into a buffer, so it does not take much time to re-draw; 2. as far as I remember, you need to set the Windows' (the image host) background brush to transparent, so you'll avoid flickering on re-drawing (and maybe you'll have to override re-drawing behavior of that Window); 3. Maybe instead of drawing on top, you can create another window of rectangular shape and put it on top of your image container – Serge Semenov Mar 19 '16 at 19:26
  • 1
    For a control in winforms to be transparent it __must be nested__ in the one below. For a picturebox you __need to do this in code__: `pbox1.parent = pbox0;` - You can try to invalidate only the outerbound of the old and the new rectangle: `invalidate(Rectangle.Union(oldRect, newRect));` Also: what is a 'complex ' image?? Large? How large? And : what is `picTekenvlak`? – TaW Mar 19 '16 at 19:34

1 Answers1

0

The OnPaint event sends a PaintEventArgs object, which describes how much to repaint based on PaintEventArgs.ClipRectangle. You can control that by sending the coordinates of the blue rectangle in the call to Invalidate.

You will then need to use those coordinates to determine how much you repaint. This will also reduce the amount your application does for any kind of overlay, such as dragging another application over the PictureBox, etc.

You may want to try drawing your complex image to a Bitmap and then assign that bitmap to the PictureBox.Image. I don't know if this will work though, since you're drawing directly on the PictureBox image surface, but if you can make it work, Windows will handle the clipping to render the image for you, which is ideal.

Matt Jordan
  • 2,133
  • 9
  • 10