4 Replies - 523 Views - Last Post: 26 January 2014 - 04:25 AM Rate Topic: -----

#1 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 13-August 11

Copy Section of Image Into Separate Image

Posted 22 January 2014 - 09:55 AM

Hey everyone. I'v been working on a 2d RPG for a while now but I have come to another bump in the road. I would appreciate a little help untangling my code. I have a timer that checks for input. What I want it to do is roughly this:
If up is pressed then
copy the area behind the sprite from fr_bm
Paste the section of fr_bm at the same position of wholeimg
draw sprite on top

I am trying to cover up the annoying trail that my sprite leaves.
here is what i have(which I will admit is faily more messed up than it was an hour ago)

  Public Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Int32) As UShort
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If GetAsyncKeyState(Convert.ToInt32(Keys.Down)) Then
            Dim z As Graphics = Graphics.FromImage(WholeImg) 'wholeimgand fr_bm are both bitmaps that hold an identical background
            Dim g As Graphics = Graphics.FromImage(fr_bm)

            Dim fr_rect As New Rectangle(x, y - 18, 22, 3)
            Dim to_rect As New Rectangle(x, y - 18, 22, 3)


           
            g.DrawImage(WholeImg, to_rect, fr_rect, _
        GraphicsUnit.Pixel)

            g.DrawRectangle(Pens.Red, fr_rect)'this is just to show me where it put the chunk, this line will be deleted in the end


            y = y + 3
            z.DrawImageUnscaled(character, x, y)



        End If

        Me.BackgroundImage = WholeImg
        Me.Refresh()


Thanks for the help. :smile2:/>/>

This post has been edited by xilith117: 22 January 2014 - 09:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Copy Section of Image Into Separate Image

#2 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 13-August 11

Re: Copy Section of Image Into Separate Image

Posted 24 January 2014 - 09:19 PM

Please? :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#3 IronRazer  Icon User is online

  • D.I.C Addict

Reputation: 250
  • View blog
  • Posts: 810
  • Joined: 01-February 13

Re: Copy Section of Image Into Separate Image

Posted 25 January 2014 - 01:08 PM

Hi,
You are taking the wrong approach to this. There is no need to draw the section of the background image back onto the background image along with the sprite and then reset the background image to the new image every time an Up, Down, Left, or Right key is pressed. You can and should use the forms paint event to just draw the sprite on the form and the background image will take care of itself when the form is re-painted. Below is a short example of what i mean.
Imports System.Runtime.InteropServices

Public Class Form1
    Dim WithEvents Tmr As New Timer With {.Interval = 100}
    Dim Sprite As New Bitmap("C:\TestFolder\Sprite.png")
    Dim SpriteLocation As New Point(100, 100)

    <DllImport("user32.dll", EntryPoint:="GetAsyncKeyState")> Public Shared Function GetAsyncKeyState(ByVal vKey As Integer) As Short
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.BackgroundImage = New Bitmap("C:\TestFolder\Background.png")
        Me.BackgroundImageLayout = ImageLayout.Stretch
        Me.DoubleBuffered = True
        Tmr.Start()
    End Sub

    Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
        If GetAsyncKeyState(Keys.Left) <> 0 Then
            SpriteLocation = New Point(SpriteLocation.X - 3, SpriteLocation.Y)
            Me.Refresh()
        End If
        If GetAsyncKeyState(Keys.Up) <> 0 Then
            SpriteLocation = New Point(SpriteLocation.X, SpriteLocation.Y - 3)
            Me.Refresh()
        End If
        If GetAsyncKeyState(Keys.Right) <> 0 Then
            SpriteLocation = New Point(SpriteLocation.X + 3, SpriteLocation.Y)
            Me.Refresh()
        End If
        If GetAsyncKeyState(Keys.Down) <> 0 Then
            SpriteLocation = New Point(SpriteLocation.X, SpriteLocation.Y + 3)
            Me.Refresh()
        End If
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawImage(Sprite, SpriteLocation)
    End Sub
End Class



Attached Image


You should also read this link written by Reed Kimble who is very good with writing games and VB.NET programming. It explains some Do`s and Do Not`s plus has a lot of other info that you may be interested in.
How to Create Video Games in VB.Net (Windows Forms)
Was This Post Helpful? 2
  • +
  • -

#4 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 13-August 11

Re: Copy Section of Image Into Separate Image

Posted 25 January 2014 - 07:36 PM

Thanks! :) this is exactly what I needed. :bananaman:
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer  Icon User is online

  • D.I.C Addict

Reputation: 250
  • View blog
  • Posts: 810
  • Joined: 01-February 13

Re: Copy Section of Image Into Separate Image

Posted 26 January 2014 - 04:25 AM

Your Welcome. I am glad it helped. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1