当子程序第二次运行时,程序会冻结

时间:2014-05-23 01:25:01

标签: vb.net

我一直在做皮肤窃取器,但出于某种原因,当我重新点击按钮3时,它就会冻结。请注意,我添加了MsgBoxes以了解它冻结的位置(据我所知,它在AssembleSkin()处冻结)。我不知道它为什么会这样做,所以如果你知道为什么,请继续发表建议!感谢

 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    If TextBox1.Text = "" Then

    Else
        MsgBox("1")
        Dim request = DirectCast(WebRequest.Create _
      ("http://www.minecraft.net/skin/" + TextBox1.Text + ".png"), WebRequest)
        MsgBox("2")
        Try
            Dim response As WebResponse = DirectCast(request.GetResponse(), WebResponse)
            MsgBox("3")
            AssembleSkin(FindImage(TextBox1.Text), PictureBox1, PictureBox2, PictureBox5, PictureBox6, PictureBox3, PictureBox4)
            MsgBox("4")
            Dim Skin As Image = FindImage(TextBox1.Text)
            MsgBox("5")
            PictureBox7.Image = SizeImage(Skin, 300, 150)
        Catch ex As WebException

            Dim response As WebResponse = DirectCast(ex.Response, WebResponse)
            MsgBox("Skin does not exist", MsgBoxStyle.OkOnly, "Mynecraft V2")
        End Try
    End If
End Sub

 Private Function FindImage(ByVal Username As String) As Image
    Dim tClient As WebClient = New WebClient

    Dim tImage As Bitmap = Bitmap.FromStream(New MemoryStream(tClient.DownloadData("http://www.minecraft.net/skin/" + Username + ".png")))

    Return tImage
End Function

Public Function GetPicturePart(ByVal SourceImage As Bitmap, ByVal Region As Rectangle) As Bitmap
    Dim ImagePart As Bitmap = New Bitmap(Region.Width, Region.Height)
    Using G As Graphics = Graphics.FromImage(ImagePart)
        Dim TargetRect As Rectangle = New Rectangle(0, 0, Region.Width, Region.Height)
        Dim SourceRect As Rectangle = Region
        G.DrawImage(SourceImage, TargetRect, SourceRect, GraphicsUnit.Pixel)
    End Using
    Return ImagePart
End Function

Private Function SizeImage(ByVal img As Bitmap, ByVal width As Integer, ByVal height As Integer) As Bitmap
    Dim newBit As New Bitmap(width, height)
    Dim g As Graphics = Graphics.FromImage(newBit)
    g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
    g.PixelOffsetMode = Drawing2D.PixelOffsetMode.Half
    g.DrawImage(img, 0, 0, width, height)
    Return newBit
End Function

Private Sub AssembleSkin(ByVal Image As Image, ByVal Head As PictureBox, ByVal Body As PictureBox, ByVal LeftArm As PictureBox, ByVal RightArm As PictureBox, ByVal RightLeg As PictureBox, ByVal LeftLeg As PictureBox)
    Head.Image = SizeImage(GetPicturePart(Image, New Rectangle(New Point(8, 8), New Size(8, 8))), 64, 64)
    Body.Image = SizeImage(GetPicturePart(Image, New Rectangle(New Point(20, 20), New Size(8, 12))), 64, 96)
    LeftArm.Image = SizeImage(GetPicturePart(Image, New Rectangle(New Point(44, 20), New Size(4, 12))), 32, 96)
    RightArm.Image = SizeImage(GetPicturePart(Image, New Rectangle(New Point(44, 20), New Size(4, 12))), 32, 96)
    LeftLeg.Image = SizeImage(GetPicturePart(Image, New Rectangle(New Point(4, 20), New Size(4, 12))), 32, 96)
    RightLeg.Image = SizeImage(GetPicturePart(Image, New Rectangle(New Point(4, 20), New Size(4, 12))), 32, 96)
End Sub

1 个答案:

答案 0 :(得分:0)

  

ByVal Image As Image

; o我第一次看到这种表示法。

另外,在catch语句中检查ex.tostring堆栈跟踪。 (或使用system.diagnostics.stacktrace)/

同样,当findimage方法返回null并且传递给assembleskin方法时会发生什么。也很有可能成为问题。

在任何情况下,请检查异常代码/跟踪。