Results 1 to 6 of 6

Thread: Reading specific height and width from an image

  1. #1

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Lightbulb Reading specific height and width from an image

    My objective is to read an exact part of a .PNG image I have in my resources and store it in a variable.

    For example. this is the image I'm reading from.

    Name:  char1.png
Views: 336
Size:  11.9 KB

    There are 16 different positions there, and I want to read each one and store it as an image.

    Dim charWalkLeft, charWalkRight etc.

    So whenever an event of moving right or left happens I can just make the picturebox image = to that variable.

    If clarification is needed let me know.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Reading specific height and width from an image

    You will need to call DrawImage where the image parameter is your resource, the point parameter is [0, 0], the rectangle parameter is the location of the original image based on which way you're walking, and the graphics unit parameter is pixel.

    You can create an enumeration to include the cardinal directions and a dictionary to store the direction along with all the points your sub-images are located at. Assuming each sub-image is 32 pixels by 32 pixels take a look at this example:
    Code:
    Public Enum CardinalDirection
        East
        North
        South
        West
    End Enum
    
    Private Const SUB_IMAGE_SIZE As New Size(32, 32)
    
    Private images As New Dictionary(Of CardinalDirection, Point()) From { _
        {CardinalDirection.East, {New Point(0, 32), New Point(32, 32), New Point(64, 32), New Point(96, 32)}, _
        {CardinalDirection.North, {New Point(0, 96), New Point(32, 96), New Point(64, 96), New Point(96, 96)}, _
        {CardinalDirection.South, {New Point(0, 0), New Point(32, 0), New Point(64, 0), New Point(96, 0)}, _
        {CardinalDirection.West, {New Point(0, 64), New Point(32, 64), New Point(64, 64), New Point(96, 64)}}
    Then to do your animation you would loop through each point in the desired cardinal direction using DrawImage throughout the process.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Reading specific height and width from an image

    As dday said but instead of an emumeration you could create a dictionary(of Integer, Point()) and just use the numbers 1 to 16 for the dictionary keys

  4. #4

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: Reading specific height and width from an image

    Where do I implement my resource image? I am a bit confused where I put this code into.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Reading specific height and width from an image

    It should only be done once, so I would normally put it in the Form_Load event handler (or a sub called from the event handler).

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Reading specific height and width from an image

    I've posted this "experiment" before somewhere.
    As part of its initialization it splits a bitmap of bushes into individual images.

    In the Form1_Load sub is this code which copies out the 64x64 images, and adds them to a list of bitmaps
    Code:
        'Load some bitmaps for example objects to be scattered around the gaming area
        Dim spriteSheet As New Bitmap(My.Resources.bushes8_10_64_64)
        Dim cloneRect As New Rectangle(0, 0, 64, 64)
        spriteSheet.MakeTransparent()
        For r As Integer = 0 To 9
          For c As Integer = 0 To 7
            cloneRect.X = c * 64
            cloneRect.Y = r * 64
            TileObjectType.AddBmp(spriteSheet.Clone(cloneRect, spriteSheet.PixelFormat))
          Next
        Next
        spriteSheet.Dispose() 'dispose of the main spritesheet, the individual sprites are now a shared asset of TileObjectType structure
    It uses the Clone method to create a new bitmap from a portion of the large spritesheet bitmap.
    Attached Files Attached Files
    Last edited by passel; Jan 24th, 2016 at 08:03 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width