Hello stackoverflow community. I already tried looking for a similar question but I only found questions about flickering, which isn't the same as the problem I am having.
I need help preventing the PictureBox
es from trailing whenever I move them across a panel. The application I am making is similar to MS Paint. When I click a PictureBox
I can click and drag it using:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pictureBox1.Left += (e.X - x);
pictureBox1.Top += (e.Y - y);
}
}
and the other pictureBoxes which I did Not click are painted to the DoubleBuffered panel, using:
private void panel1_Paint(object sender, PaintEventArgs e)
{
foreach (PictureBox pb in pboxes)
{
if (!pb.Visible)
{
e.Graphics.DrawImage(pb.BackgroundImage, new Rectangle(pb.Location, pb.Size));
}
}
}
For some reason when I drag a PictureBox
it's background Image drags across the painted panel.
Weird thing is, this only happens on the Paint event. If I were to make the panel's background Image something, the moving PictureBox
won't trail. It only happens when I'm painting Image
s onto the panel.
Here is an example
I would greatly appreciate any help, thanks.
I simplified the code so it'll be easier to understand.(The trailing effect still occurs)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int x;
int y;
public Form1()
{
InitializeComponent();
pictureBox1.Show();
pictureBox2.Hide();
pictureBox3.Hide();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
panel1.Invalidate();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pictureBox1.Left += (e.X - x);
pictureBox1.Top += (e.Y - y);
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(pictureBox2.BackgroundImage, new Rectangle(pictureBox2.Location, pictureBox2.Size));
e.Graphics.DrawImage(pictureBox3.BackgroundImage, new Rectangle(pictureBox3.Location, pictureBox3.Size));
}
}}
And it uses this doubleBuffered panel class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class DoubleBufferPanel : Panel
{
public DoubleBufferPanel()
{
// Set the value of the double-buffering style bits to true.
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();
}
}
}
Now my code calls for 3 PictureBox
, and 1 DoubleBuffered
panel.
The form is maximized, Panel1.size = (2000, 1200);
and each PictureBox
size = (700, 700)
and set each PictureBox
es background Image to a random large detailed image. The trailing effect occurs when I move pictureBox1
and I can reproduce this every time.