在移动或调整位图的大小时,我遇到了麻烦。我基本上是在做一个绘画类型的应用程序,该应用程序还将包含与photoshop相似的图层。
我已经设置了滚轮来缩放/调整位图的大小,并设置了鼠标中键来在按住面板并移动的同时移动面板。
我有一个面板,面板上的方格背景图像设置为平铺。这样可以使背景图案在调整大小时保持相同的大小。 该面板主要用于设置位图的大小并具有背景。
以下是有关此问题的视频: numpy reference ceil
这是在不相关内容被删除的情况下进行绘制和移动的脚本。
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;
}
}
所以基本上,这里发生的是列表包含位图。 draw事件中的循环绘制这些位图。一旦全部绘制完毕,然后将手柄绘制在顶部。
我已经尝试过两次缓冲并且无休止地尝试着,但是没有成功。
有人能帮忙吗?
谢谢