为什么画点有时会消失? [英] Why is a painted dot disappears sometime?
本文介绍了为什么画点有时会消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在图像上画一个点……
I try to paint a dot on an image …
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Image = Image.FromFile("C:\Users\SHEMMY-7X64\Pictures\postage.jpg")
Using p As New System.Drawing.Pen(Color.Yellow, 4)
Using g As Graphics = PictureBox1.CreateGraphics()
g.DrawEllipse(p, 15, 5, 10, 10)
End Using
End Using
End Sub
图像是绘制的,但不是点.将代码拆分为 2 个步骤时:1.加载图片
The image is painted but not the dot. When separating the code to 2 steps: 1. Load the image
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Image = Image.FromFile("C:\Users\SHEMMY-7X64\Pictures\postage.jpg")
End Sub
2.油漆
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using p As New System.Drawing.Pen(Color.Yellow, 4)
Using g As Graphics = PictureBox1.CreateGraphics()
g.DrawEllipse(p, 15, 5, 10, 10)
End Using
End Using
End Sub
这次是画了点.我在另一个网站上发布了这个问题,并被告知这是一个时间问题.
This time the dot was painted. I posted this question at an another site and was told it's a question of timing.
好吧,这是时间问题,但如何解决呢?
Ok it's a question of timing, but how to solve it?
推荐答案
您需要绘制包括图像在内的所有内容.你可以:
You need to draw everything including the image. You could:
创建一个 Bitmap
类型的类级变量并将其命名为 Bmp:
Create a class level variable of Bitmap
type and name it say, Bmp:
Private Bmp as Bitmap
加载新图片:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'dispose the old image if any:
bmp?.Dispose()
'and assing the new one:
bmp = New Bitmap(Image.FromFile("C:\Users\SHEMMY-7X64\Pictures\postage.jpg"))
'and call:
PictureBox1.Invalidate()
End Sub
现在的绘画程序:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If bmp IsNot Nothing Then
Dim srcRect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim desRect As New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Height)
Dim G As Graphics = e.Graphics
G.SmoothingMode = SmoothingMode.AntiAlias
G.Clear(BackColor) 'or: G.Clear(Parent.Backcolor) if you want.
G.DrawImage(bmp, desRect, srcRect, GraphicsUnit.Pixel)
Using pn As New Pen(Color.Yellow, 4)
G.DrawEllipse(pn, 15, 5, 10, 10)
End Using
End If
End Sub
最后别忘了清理:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
bmp?.Dispose()
End Sub
祝你好运.
这篇关于为什么画点有时会消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文