C#在pictureBox控件上画的直线被其他窗口挡一下,直线就被擦除掉,要重新画。
展开全部
方法1:写在Paint事件中,这样就不会消失在PictureBox中。
方法2:绘制到图形对象里,而不是简单是用画笔画线
看下面的代码,s_Bmp可以持久显示,s_pen则不行,
不好意思我这是vb.net代码,道理一样,不难看懂...
private sub s_Bmp()
Dim Bmp As Bitmap
Dim Gra As Graphics
Dim Pen As New Pen(Color.White)
Bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
Gra = Graphics.FromImage(Bmp)
Dim Dia As Single = Math.Min(PictureBox1.Height, PictureBox1.Width) * 0.5!
Gra.DrawArc(Pen, (PictureBox1.Width - Dia) / 2.0!, (PictureBox1.Height - Dia) / 2.0!, Dia, Dia, 0, 360)
Gra.DrawLine(Pen, 0, 0, PictureBox1.Width / 2.0!, PictureBox1.Height / 2.0!)
PictureBox1.Image = Bmp
end sub
private sub s_Pen()
Dim Gra As Graphics = PictureBox1.CreateGraphics()
Dim Pen As New Pen(Color.Magenta)
Dim Dia As Single = Math.Min(PictureBox1.Height, PictureBox1.Width) * 0.5!
Gra.DrawRectangle(Pen, (PictureBox1.Width - Dia) / 2.0!, (PictureBox1.Height - Dia) / 2.0!, Dia, Dia)
end sub
方法2:绘制到图形对象里,而不是简单是用画笔画线
看下面的代码,s_Bmp可以持久显示,s_pen则不行,
不好意思我这是vb.net代码,道理一样,不难看懂...
private sub s_Bmp()
Dim Bmp As Bitmap
Dim Gra As Graphics
Dim Pen As New Pen(Color.White)
Bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
Gra = Graphics.FromImage(Bmp)
Dim Dia As Single = Math.Min(PictureBox1.Height, PictureBox1.Width) * 0.5!
Gra.DrawArc(Pen, (PictureBox1.Width - Dia) / 2.0!, (PictureBox1.Height - Dia) / 2.0!, Dia, Dia, 0, 360)
Gra.DrawLine(Pen, 0, 0, PictureBox1.Width / 2.0!, PictureBox1.Height / 2.0!)
PictureBox1.Image = Bmp
end sub
private sub s_Pen()
Dim Gra As Graphics = PictureBox1.CreateGraphics()
Dim Pen As New Pen(Color.Magenta)
Dim Dia As Single = Math.Min(PictureBox1.Height, PictureBox1.Width) * 0.5!
Gra.DrawRectangle(Pen, (PictureBox1.Width - Dia) / 2.0!, (PictureBox1.Height - Dia) / 2.0!, Dia, Dia)
end sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询