none
New alternative to find the .png

    Question

  • This is the developed alternative code I am using: compliments to Frank L Smith

    Option Strict On
    Option Explicit On
        Option Infer Off
    
        Imports System.IO
        Imports System.IO.Path
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object,
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim i As Integer = 1
    
            Dim imagePath As String =
                Combine("C:\Users\BusinessOffice1Room\Documents\",
                        i.ToString) & ".png"
    
            Dim fi As New FileInfo(imagePath)
    
            If fi.Exists Then
                imagePath = Combine("C:\Users\BusinessOffice1Room\Documents\",
                        i.ToString) & ".png"
                ' Now do the rest...  
                MsgBox("File extracted")
            Else
                MsgBox("File doesn't exist ")
            End If
    
            Stop
    
        End Sub
    End Class

    Ill be using the following code here but in doubt which parts are suppose to go in. 

    Private Class Sprite
            Public Name As String
            Public Pic As PictureBox
            Public ColRow As Point
            Public Steps As Integer = -1    '-1 = unlimited move
            Public Visible As Integer = 2   '0 = not visible  1= Frozen  2=Active   
            Public Range As Integer
            Public AP As Integer
            Public HP As Integer
        End Class

    This part of course because it outlines the characteristics of the sprites

    Then it is this part where there is a .backgroundimage (do I replace that with the code with works)

        Private Sprites As New List(Of Sprite)
    
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
            For i = 1 To 3
    
                Dim pic = New PictureBox()
                With pic
                    .Name = "Pic" & i.ToString
                    .Size = GridSize
                    .BackgroundImageLayout = ImageLayout.Stretch
                    .BackgroundImage = Image.FromFile("C:\Users\BusinessOffice1Room\Documents\" & i.ToString & ".png")
                    '.BackgroundImage = Image.FromFile("N:\Computer Science Coursework\Work In progression\BMP Files\Tanks\" & i.ToString & ".png")
                    Me.Controls.Add(pic)
                    AddHandler .MouseDown, AddressOf Pics_MouseDown
                    AddHandler .MouseMove, AddressOf pics_MouseMove
                    AddHandler .MouseUp, AddressOf pics_MouseUp
                End With
    
                Dim spt As New Sprite
                spt.Pic = pic
                spt.ColRow = New Point(i)
                Sprites.Add(spt)
            Next
    
            Sprites(1).Pic.Name = "Heavy Panzer"
            Sprites(2).Pic.Name = "Heavy Scorcher"
            Sprites(1).HP = 10
            Sprites(1).AP = 5
            Sprites(2).HP = 10
            Sprites(2).AP = 5
    
            Form1_Resize(1, Nothing)
        End Sub

    WRA

    Saturday, March 25, 2017 9:48 PM

Answers

  • Then it is this part where there is a .backgroundimage (do I replace that with the code with works)

    You know that those files exist because you created them at that location.  Checking for their existence is not important to your current task - you will get an error if they aren't found, and you can fix it at that time.  If you are going to make progress you need to concentrate on the primary task and not take off at a tangent. The code for matching your sprites to their images has been provided in that other thread.

    • Marked as answer by Waliur Rahman Sunday, March 26, 2017 11:53 AM
    Saturday, March 25, 2017 10:03 PM
  • This is the code I used from you Acamar

    DoubleBuffered = True Sprites.Add(New Sprite) Sprites.Add(New Sprite) Sprites.Add(New Sprite) Sprites(0).Name = "Light Battle" Sprites(0).HP = 10 Sprites(0).AP = 5 Sprites(1).Name = "Heavy Panzer" Sprites(1).HP = 10 Sprites(1).AP = 5 Sprites(2).Name = "Heavy Scorcher" Sprites(2).HP = 10 Sprites(2).AP = 5 For i = 0 To 2 Dim pic = New PictureBox() With pic .Name = "Pic" & i.ToString .Size = GridSize .BackgroundImageLayout = ImageLayout.Stretch Dim ImageFile As String = System.IO.Path.Combine("C:\Users\BusinessOffice1Room\Documents\", Sprites(i).Name & ".png") .BackgroundImage = Image.FromFile(ImageFile) Me.Controls.Add(pic) AddHandler .MouseDown, AddressOf Pics_MouseDown AddHandler .MouseMove, AddressOf Pics_MouseMove AddHandler .MouseUp, AddressOf Pics_MouseUp End With Sprites(i).pic = pic Sprites(i).ColRow = New Point(i) Next End Sub


    WRA


    Sunday, March 26, 2017 11:49 AM

All replies

  • Then it is this part where there is a .backgroundimage (do I replace that with the code with works)

    You know that those files exist because you created them at that location.  Checking for their existence is not important to your current task - you will get an error if they aren't found, and you can fix it at that time.  If you are going to make progress you need to concentrate on the primary task and not take off at a tangent. The code for matching your sprites to their images has been provided in that other thread.

    • Marked as answer by Waliur Rahman Sunday, March 26, 2017 11:53 AM
    Saturday, March 25, 2017 10:03 PM
  • @Acamar

    Morning/Afternoon you know what happen to Frank's comments? in the last thread I posted because they have all gone?


    WRA

    Sunday, March 26, 2017 11:26 AM
  • How could I actually check if the file directory correct because I can't be able to go into debug mode to step through code and validate it works otherwise I need to use a msgbox and if statement feature and just use a button to quickly test it. 

    This is because when I load the application it doesn't show any of my sprites at all. 

     Public Sub LoadAssets()
            For Each imageFile In IO.Directory.GetFiles(IO.Path.Combine(My.Application.Info.DirectoryPath, "VWXYZ"), "*.png")
                SpriteSheets(IO.Path.GetFileNameWithoutExtension(imageFile)) = Image.FromFile(imageFile)
            Next
        End Sub

    As some of your might seen in the past few threads it will show the given sprites and I'm trying to load them up by referencing to their names rather than calling them 1, 2, 3 etc which is just complicated and awful


    WRA

    Sunday, March 26, 2017 11:45 AM
  • @Acamar

    Morning/Afternoon you know what happen to Frank's comments? in the last thread I posted because they have all gone?


    WRA

    Explain what you mean? My comments about what/where?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, March 26, 2017 11:47 AM
  • This is the code I used from you Acamar

    DoubleBuffered = True Sprites.Add(New Sprite) Sprites.Add(New Sprite) Sprites.Add(New Sprite) Sprites(0).Name = "Light Battle" Sprites(0).HP = 10 Sprites(0).AP = 5 Sprites(1).Name = "Heavy Panzer" Sprites(1).HP = 10 Sprites(1).AP = 5 Sprites(2).Name = "Heavy Scorcher" Sprites(2).HP = 10 Sprites(2).AP = 5 For i = 0 To 2 Dim pic = New PictureBox() With pic .Name = "Pic" & i.ToString .Size = GridSize .BackgroundImageLayout = ImageLayout.Stretch Dim ImageFile As String = System.IO.Path.Combine("C:\Users\BusinessOffice1Room\Documents\", Sprites(i).Name & ".png") .BackgroundImage = Image.FromFile(ImageFile) Me.Controls.Add(pic) AddHandler .MouseDown, AddressOf Pics_MouseDown AddHandler .MouseMove, AddressOf Pics_MouseMove AddHandler .MouseUp, AddressOf Pics_MouseUp End With Sprites(i).pic = pic Sprites(i).ColRow = New Point(i) Next End Sub


    WRA


    Sunday, March 26, 2017 11:49 AM
  • Morning/Afternoon you know what happen to Frank's comments? in the last thread I posted because they have all gone?

    Those comments were related to confirming that the file exists before you try to load it.  They are not relevant to what you are trying to do.  To understand why, you need to ask yourself the question 'What should the code do if the file doesn't exist?'.  There is no sensible answer to that other than 'Tell the user that the application is installed incorrectly, and exit'. 

    When you eventually come to deal with this problem it will be by embedding the images within your program, so that you will not have to load the images from disk.  So any time you spend now on code that confirms that the files exist is simply wasted time, and you do not have enough of that to be wasting it.

    Use the code I provided to load the files by name.  Make sure they exist with those names before starting the program.  And concentrate on getting your game play to work.

    Sunday, March 26, 2017 11:52 AM
  • I fixed it Acamar. Never mind about the issues I have just posed it was relatively minor errors such as wrong file name etc. 

    Thanks Frank and Acamar for the help you don't know how much appreciated I am that I am getting help from many people here. 


    WRA

    Sunday, March 26, 2017 11:53 AM