gpt4 book ai didi

VB.NET画图方法?

转载 作者:行者123 更新时间:2023-12-02 09:00:19 27 4
gpt4 key购买 nike

这是在表单上绘制图像的最佳方式吗?我的大部分经验是在 Java 方面,而在 VB.net 方面的经验很少。 Java中有可以调用的repaint()吗?有一个Me.refresh()。当您需要向表单添加内容或向表单绘制内容时,这就是您想要调用的吗?

类(class):

    Public Class Form1

Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' Create image.
Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")

' Create Point for upper-left corner of image.
Dim ulCorner As New Point(50, 50)

' Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner)

Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")

' Create Point for upper-left corner of image.
Dim ulCorner2 As New Point(150, 150)

' Draw image to screen.
e.Graphics.DrawImage(newImage2, ulCorner2)

End Sub

End Class

我在这里使用表单上的一个带有单击事件的按钮尝试了此代码

Public Class Form1
Dim i As Integer

Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

i = 0

' Create image.
Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")

' Create Point for upper-left corner of image.
Dim ulCorner As New Point(50, 50)

' Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner)

Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")

' Create Point for upper-left corner of image.
Dim ulCorner2 As New Point(150, 150)

' Draw image to screen.
e.Graphics.DrawImage(newImage2, ulCorner2)

If i = 5 Then
' Create image.
Dim newImage3 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")

' Create Point for upper-left corner of image.
Dim ulCorner3 As New Point(250, 250)

' Draw image to screen.
e.Graphics.DrawImage(newImage3, ulCorner3)
End If

End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
i = 5
Me.Refresh()
End Sub
End Class

刚刚在这里尝试了这段代码:

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
i = 5
Me.Invalidate()
End Sub

刚刚测试的新类:

Public Class Form2
Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Dim newImage3 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")

Private bgImage As Bitmap
Private srcImages As Image()

Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)

Dim img As Bitmap = Me.bgImage

If (img Is Nothing) Then
Me.bgImage = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
img = Me.bgImage
End If

If (Not Me.srcImages Is Nothing) Then
End If

MyBase.OnPaint(e)

End Sub

Private Sub _Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.srcImages = New Image(3 - 1) {}
Me.srcImages(0) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Me.srcImages(1) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Me.srcImages(2) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
End Sub

Private Sub _Disposed(sender As Object, e As EventArgs) Handles MyBase.Disposed
If (Not Me.srcImages Is Nothing) Then
For Each img As Image In Me.srcImages
If (Not img Is Nothing) Then
img.Dispose()
End If
Next
Me.srcImages = Nothing
End If
If (Not Me.bgImage Is Nothing) Then
Me.bgImage.Dispose()
Me.bgImage = Nothing
End If
End Sub

Private Sub _Resize(sender As Object, e As EventArgs) Handles MyBase.SizeChanged, MyBase.Resize
If (Not Me.bgImage Is Nothing) Then
Me.bgImage.Dispose()
Me.bgImage = Nothing
End If
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.Invalidate()
End Sub
End Class

最佳答案

第一个片段没有显示需要任何帮助的证据。 Paint 事件处理程序中的任何内容都不依赖于类的状态,因此没有理由强制重新绘制。默认绘画已经足够好了。

第二个片段是通过人工 i 变量实现的。在这种情况下,您总是会调用 Me.Invalidate(),这会将表单的整个客户区域标记为需要重新绘制。当不需要执行任何更重要的操作时,UI 最终会收到 Paint 事件。使用 Me.Refresh() 也可以工作,但过于严厉,而且很少需要它。我想不出一个好的例子。

注意如何Invalidate()有一些重载,您可以仅标记窗口的一部分以需要重新绘制。这是一项使绘画效率更高的优化。

正如所写,您的程序在使用一段时间后很可能会崩溃。您必须调用所加载图像的 Dispose() 方法。看一下 VB.NET Using 语句。您需要预先加载图像,这样在绘画时就不会发生这种情况,也不会重复发生。强烈建议遵循 Winforms 编程教程或书籍,以避免此类陷阱。

关于VB.NET画图方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20638943/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com