1

I have a CF 2.0 app with a PictureBox on a Form. I want to move the PictureBox with mouse move and I need to add Double Buffer to the form to avoid flickering.

How can I do this?

Thanks!

VansFannel
  • 45,055
  • 107
  • 359
  • 626

1 Answers1

5

You don't need the Form double-buffered, you need the PB to be. That's not so easy to come by in CF. However, you could create your own control, PB is pretty simple. For example:

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

public class MyPictureBox : Control {
  private Image mImage;
  public Image Image {
    get { return mImage; }
    set { mImage = value; Invalidate(); }
  }
  protected override void OnPaintBackground(PaintEventArgs pevent) {
    // Do nothing
  }
  protected override void OnPaint(PaintEventArgs e) {
    using (Bitmap bmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height)) {
      using (Graphics bgr = Graphics.FromImage(bmp)) {
        bgr.Clear(this.BackColor);
        if (mImage != null) bgr.DrawImage(mImage, 0, 0);
      }
      e.Graphics.DrawImage(bmp, 0, 0);
    }
    base.OnPaint(e);
  }
}

Hopefully, I didn't use stuff that isn't available in CF...

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • PB? What is the meaning of PB? – VansFannel Feb 22 '09 at 17:21
  • It doesn't work. I need double buffer on Form because I moving the entire PictureBox (I think). – VansFannel Feb 22 '09 at 17:59
  • This is the correct code, but what you'll need to do is create a container (a panel for example) that draws like this. Worst case scenario is that you'll have to swap out a picture box for something custom you manually draw and control inside the panel. – Quibblesome Feb 23 '09 at 15:33
  • Also works like a charm when drawing complex things that take time. I Preallocate the bitmap, resize it when needed (on Form.ResizeEnd), draw the stuff (splines with 1000s of points) into the bitmap and draw the bitmap into the PB. – Gabriel Aug 31 '15 at 16:14