为什么我在System.Drawing.dll中收到System.ArgumentException

时间:2015-05-08 21:59:39

标签: vb.net

有人可以帮帮我吗?我正在尝试绘制图像,但是我收到了这个错误:

  

System.Drawing.dll中出现未处理的“System.ArgumentException”类型异常“

有功能:

Dim x, y, xlatime, ylungime As Integer
Dim myImage As Image
Dim folder As String

Sub PictureBox1_Paint(sender1 As Object, er As PaintEventArgs)
    Dim r As New Rectangle(x, y, xlatime, ylungime)
    er.Graphics.DrawImage(myImage, r)
End Sub

Private Function deseneaza(ByVal poza As String, ByRef x_perm As Integer, ByRef y_perm As Integer, ByRef lungime As Integer, ByRef latime As Integer)
    myImage = Image.FromFile(poza)
    x = x_perm
    y = y_perm
    xlatime = latime
    ylungime = lungime
    Refresh()
    Return Nothing
End Function

 Private Sub joaca_buton_Click(sender As Object, e As EventArgs) Handles joaca_buton.Click
    Timer1.Stop()
    nor.Hide()
    nor_mic.Hide()
    logo.Hide()
    exit_buton.Hide()
    joaca_buton.Hide()
    continua_buton.Hide()
    optiuni_buton.Hide()
    BackgroundImage.Dispose()
    deseneaza(folder + "\incarcare1.png", 0, 0, 729, 1008)
End Sub

1 个答案:

答案 0 :(得分:1)

在点击按钮之前,将调用图片框的绘画事件。在这种情况下,MyImage将为Nothing,因此当您尝试绘制它时会导致异常。更改绘制事件以包括检查:

Sub PictureBox1_Paint(sender1 As Object, er As PaintEventArgs)
    If myImage IsNot Nothing Then    
        Dim r As New Rectangle(x, y, xlatime, ylungime)
        er.Graphics.DrawImage(myImage, r)
    End If
End Sub

要强制重绘,请将Picturebox1.Invalidate()添加到Sub joaca_buton_Click的末尾。

Private Sub joaca_buton_Click(sender As Object, e As EventArgs) Handles joaca_buton.Click
    'The rest of the sub here
    deseneaza(folder + "\incarcare1.png", 0, 0, 729, 1008)
    PictureBox1.Invalidate()
End Sub