I'm having trouble with a Bitmap flicking when it's moved or resized. I'm basically making a paint type application which will also include layers similar to that of photoshop.
I've set the scroll wheel to zoom/resize the Bitmap and set the middle mouse button to move the panel while it is held down and moving.
I have a panel with a checkered background image which is set to tile. This is so that the background pattern stays the same size when resizing. This panel is basically used to set the size of a Bitmap and have a background.
Here is a video of of the issue: https://youtu.be/HRcCGaPmNU0
Here is the script that draws and moves with the irrelevant stuff removed.
public Canvas()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.MouseWheel += new MouseEventHandler(Canvas_WheelZoom);
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
private void panel_DrawArea_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g;
g = e.Graphics;
//--------LAYERS--------//
layerCount = layerBMPs.Count;
if (layerCount > 0)
{
for (int i = 0; i < layerCount; i++)
{
Console.WriteLine("Draw Layer");
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
g.DrawImage(layerBMPs[i], 0, 0, canvasSize.Width, canvasSize.Height);
}
}
//--------LAYERS--------//
//--------GRID--------//
Console.WriteLine("Draw Grid");
Bitmap gridArea;
gridArea = new Bitmap(canvasSize.Width, canvasSize.Height);
Pen myPen = new Pen(Brushes.Black);
int tileCountX = canvasSize.Width / tileSize.Width;
int tileCountY = canvasSize.Height / tileSize.Height;
for (int i = 0; i < tileCountX; i++)
{
g.DrawLine(myPen, i * tileSize.Width * canvasScale, 0, i * tileSize.Width * canvasScale, canvasSize.Height);
}
for (int j = 0; j < tileCountY; j++)
{
g.DrawLine(myPen, 0, j * tileSize.Height * canvasScale, canvasSize.Width, j * tileSize.Height * canvasScale);
}
//--------GRID--------//
g.Dispose();
myPen.Dispose();
}
//Clicking the canvas
private void panel_DrawArea_MouseDown(object sender, MouseEventArgs e)
{
//Detect which mouse button is pressed
switch(MouseButtons)
{
case MouseButtons.Middle:
//Move Canvas
canvasMove = 1;
canvasMoveX = e.X;
canvasMoveY = e.Y;
break;
}
}
private void panel_DrawArea_MouseMove(object sender, MouseEventArgs e)
{
//Detect which mouse button is pressed
switch (MouseButtons)
{
case MouseButtons.Middle:
//Move Canvas
if (canvasMove == 1)
{
int xPos = Cursor.Position.X - canvasMoveX - this.Location.X;
int yPos = Cursor.Position.Y - canvasMoveY - this.Location.Y - 16;
drawAreaPos = new Point(xPos, yPos);
panel_DrawArea.Refresh(); <--- This makes it flicker really fast when moving or resizing.
//panel_DrawArea.Invalidate(); <--- Does nothing
}
break;
}
}
private void panel_DrawArea_MouseUp(object sender, MouseEventArgs e)
{
switch (MouseButtons)
{
case MouseButtons.Middle:
//Move Canvas
canvasMove = 0;
break;
}
}
So basically what happens here is that the list contains bitmaps. The loop in the draw event draws those bitsmaps. Once all of them have been drawn then the grip is drawn on top.
I've tried double buffering and endless ammounts of playing about with no success.
Is anyone able to help?
Thanks