在鼠标拖动C#上绘图时形状不可见
Posted
技术标签:
【中文标题】在鼠标拖动C#上绘图时形状不可见【英文标题】:Shapes not visible while drawing on mouse drag C# 【发布时间】:2015-09-22 03:09:33 【问题描述】:我是一名新开发人员。我正在尝试制作一个简单的程序,我们可以在其中绘制多个形状,例如矩形、椭圆、直线等。我现在面临的问题是,当我单击鼠标并拖动以在 mouseDown 和 mouseMove 事件上绘制我无法看到形状。仅当我离开鼠标时,该形状才会出现在图片框上。 我希望形状在用户绘制时出现并随鼠标移动,就像在 Microsoft 的绘图应用程序中一样。 我在绘图时尝试使用 Refresh() 函数,但它删除了已经在图片框中绘制的旧形状。 下面是我正在使用的代码。需要一些建议。
bool draw = false;
bool isMouseDown = false;
int x, y, lx, ly = 0;
Item currItem;
Pen pen = new Pen(Color.Red,1);
public enum Item
Rectangle, Ellipse, Line, Pencil, Eraser
private void btnROIDraw_Click(object sender, EventArgs e)
if (menuStripRoi.Visible == false)
menuStripRoi.Visible = true;
else
menuStripRoi.Visible = false;
draw = false;
private void pictureBoxROI_MouseDown(object sender, MouseEventArgs e)
x = e.X;
y = e.Y;
isMouseDown = true;
//draw = true;
private void pictureBoxROI_MouseUp(object sender, MouseEventArgs e)
lx = e.X;
ly = e.Y;
if (draw)
Graphics g = pictureBoxROI.CreateGraphics();
switch (currItem)
case Item.Rectangle:
g.DrawRectangle(pen, x, y, e.X - x, e.Y - y);
break;
case Item.Ellipse:
g.DrawEllipse(pen, x, y, e.X - x, e.Y - y);
break;
case Item.Line:
g.DrawLine(pen, x, y, e.X, e.Y);
break;
g.Dispose();
isMouseDown = false;
private void pictureBoxROI_MouseMove(object sender, MouseEventArgs e)
if (isMouseDown)
if (draw)
Graphics g = pictureBoxROI.CreateGraphics();
switch (currItem)
case Item.Pencil:
g.FillEllipse(new SolidBrush(Color.Red), e.X - x + x, e.Y - y + y, 2, 2);
break;
case Item.Eraser:
g.FillEllipse(new SolidBrush(pictureBoxROI.BackColor), e.X - x + x, e.Y - y + y, 20, 20);
break;
g.Dispose();
private void pictureBoxROI_Paint(object sender, PaintEventArgs e)
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
pictureBoxROI.Refresh();
private void rectangleToolStripMenuItem_Click(object sender, EventArgs e)
currItem = Item.Rectangle;
draw = true;
private void ellipseToolStripMenuItem_Click(object sender, EventArgs e)
currItem = Item.Ellipse;
draw = true;
private void lineToolStripMenuItem_Click(object sender, EventArgs e)
currItem = Item.Line;
draw = true;
private void pencilToolStripMenuItem_Click(object sender, EventArgs e)
currItem = Item.Pencil;
draw = true;
private void eracerToolStripMenuItem_Click(object sender, EventArgs e)
currItem = Item.Eraser;
draw = true;
private void newToolStripMenuItem_Click(object sender, EventArgs e)
pictureBoxROI.Refresh();
pictureBoxROI.Image = null;
private void openToolStripMenuItem_Click(object sender, EventArgs e)
OpenFileDialog o = new OpenFileDialog();
o.Filter = "Png files|*.png|jpeg files|*jpg|bitmaps|*.bmp";
if (o.ShowDialog() == System.Windows.Forms.DialogResult.OK)
pictureBoxROI.Image = (Image)Image.FromFile(o.FileName).Clone();
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
Bitmap bmp = new Bitmap(pictureBoxROI.Width, pictureBoxROI.Height);
Graphics g = Graphics.FromImage(bmp);
Rectangle rect = pictureBoxROI.RectangleToScreen(pictureBoxROI.ClientRectangle);
g.CopyFromScreen(rect.Location, Point.Empty, pictureBoxROI.Size);
g.Dispose();
SaveFileDialog s = new SaveFileDialog();
s.Filter = "Png files|*.png|jpeg files|*jpg|bitmaps|*.bmp";
if (s.ShowDialog() == System.Windows.Forms.DialogResult.OK)
if (File.Exists(s.FileName))
File.Delete(s.FileName);
if (s.FileName.Contains(".jpg"))
bmp.Save(s.FileName, ImageFormat.Jpeg);
else if (s.FileName.Contains(".png"))
bmp.Save(s.FileName, ImageFormat.Png);
else if (s.FileName.Contains(".bmp"))
bmp.Save(s.FileName, ImageFormat.Bmp);
【问题讨论】:
【参考方案1】:找到了我的问题的解决方案。 本文详细解释了有关使用 GDI+ 进行绘图的所有内容。值得一看 http://www.codeproject.com/Articles/17893/Extensions-to-DrawTools
【讨论】:
以上是关于在鼠标拖动C#上绘图时形状不可见的主要内容,如果未能解决你的问题,请参考以下文章