Results 1 to 23 of 23

Thread: VB 6 - BitBlt

  1. #1

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Talking VB 6 - BitBlt

    Hey All i noticed there was no tutorial on this forum for BiBlt so here one I made (please note this was origionally made for another forum, but i began to dislike using it so i came here )

    Comments and improvements are welcome


    So you want to learn BitBlt but dont know where to start? Well hopefully this tutorial will get you on the road to becoming a BitBlt master

    So what is BitBlt?

    Well BitBlt is one of many windows API's

    Getting Started

    So lets jump right in and get some coding done

    Ok first things first open up a new VB project and we are going to add a module, name it 'mdlBitblt' and name the form 'frmbitblt' In the module we are going to place the BitBlt API

    so in the module enter the following...



    VB Code:
    1. Public Declare Function BitBlt Lib"gdi32" _
    2.  (ByVal hDestDC As Long, ByVal X As Long,_
    3.  ByVal Y As Long, ByVal nWidth As Long, _
    4.  ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc _
    5.  As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long



    Ok dont be put off bye the way it looks, it looks ALOT harder than it actually is!

    Next Stage

    So we have the API in place now all we need to do is use it

    As you will see I have attached 3 pictures, Hero, Hero Mask, and Land
    You will need to draw up 3 picture boxes on your form and call them the following

    Hero
    HeroMask
    Land

    Set the 'autoredraw' property to True, and the 'visible' property to false, it is also a good idea to set the 'scalemode' to 3 – Pixels (also set the forms 'scalemode' to pixels)

    Ok now into each picture box load its respective image

    Next draw 3 command buttons and add the following names and captions –

    Name = cmddrawland caption = Draw Land
    Name = cmddrawmask caption = Draw Mask
    Name = cmddrawhero caption = Draw Hero

    Were all set up now all we need is the code

    The Land Code

    we will start by drawing the land....


    VB Code:
    1. Private Sub cmdDrawLand_click()
    2.  drawLand ' goes to the ‘drawland’ sub
    3.  end sub



    In the module…

    VB Code:
    1. Public Sub drawLand()
    2.  BitBlt frmBitBlt.hDC, 50, 50, 300, 300, frmBitBlt.Land.hDC, 0, 0, vbSrcCopy
    3.  End Sub



    Ok so lets go through that code….

    BitBlt - is saying we want to use the BitBlt Api,
    frmBitBlt.hDC - is where we are going to BitBlt to,
    50 is the X position on the surface your are BitBlt'ing on
    50 is the Y position on the surface your are BitBlt'ing on
    300 is the width
    300 is the height
    frmbitblt.land.hDC this is what we are actually going to Copy from
    0 and 0 - this is the area of the picture u wish to ‘cut out’ for now don’t worry about this
    VBSrcCopy - this is the type of bitblt for the background we will just copy it!

    You may have noticed this .HDC well heres a definition...
    This is an upgrade from previous versions of Visual Basic which allows you to interface with the windows api functions. You will find it on certain controls and all forms as the property .hdc or even hWnd.

    The term hdc is the handle to the device context of that window or control.

    The Hero Mask Code
    And Now we will draw our Hero Mask....

    But what is a mask? Well a mask in terms of BitBlt is basicly a copy of the actuall image, in our case all black, which we put on the background first to stop the background colours leaking through and spoiling our image


    VB Code:
    1. Private Sub cmdDrawmask_click()
    2.  drawMask ' this will go to the ‘drawMask’ sub in the module
    3.  End Sub



    In the module….

    VB:
    VB Code:
    1. Public Sub drawMask()
    2.  BitBlt frmBitBlt.hDC, 50, 50, 30, 30, frmBitBlt.HeroMask.hDC, 0, 0, vbSrcAnd
    3.  End Sub



    The only differance in the one above is vbSrcAnd which is what we use for masking

    The Hero Code

    And now we will draw our hero on top of the mask....


    VB Code:
    1. Private Sub cmdDrawHero_click()
    2.  drawHero ' this will go to the ‘drawhero’ sub in the module
    3.  End Sub


    In the module….


    VB Code:
    1. Public Sub drawhero()
    2.  BitBlt frmBitBlt.hDC, 50, 50, 30, 30, frmBitBlt.Hero.hDC, 0, 0, vbSrcPaint
    3.  End Sub




    Again this time we have vbSrcPaint which is what we use for painting on top of the mask!

    Please make a note to the fact that the hero mask X and Y position must be exactly the same as the Hero’s X and Y

    And that’s it I hope you have now begin to understand how powerfull BitBlt can be

    I have also made an example in the Zip file below



    ‘Good Luck’

    PINO-

    Please note the image of the 'hero' is not made by me, i found while searching, if you are the owner and do not wish me to use it then please say and i will remove it
    Last edited by Pino; Jun 16th, 2005 at 07:14 AM.

  2. #2

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787
    Forgot the attatchements
    Attached Files Attached Files

  3. #3

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787
    Ok hey again, i'm a little stuck with what to do, so if anyone would like to see and advance on this tuturiol or maybe you have something you need to be explained please reply, i cant think as of yet a tuturiol to write

    c-ya soon

    LIAM

  4. #4
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123
    why dont u try to explain how to do transitions like the ones powerpoint has. nothing fancy but an imagebox or a picturebox will do. thanx you.

  5. #5
    Member
    Join Date
    Jul 2004
    Posts
    36
    I have a question here.

    I read your explanation and downloaded your tutorial codes and ran it. I got a problem here.

    I had a little piece of software running, after mask or land or hero have been drawn on the form, I moved the software around and whenever it covered the picture area, the picture was erased by the software's interface.

    Or when I minimize the form and brougt it up again, image on the form disappeared

    Have an idea why?

  6. #6
    New Member
    Join Date
    Jan 2005
    Posts
    3

    Cool Re: VB 6 - BitBlt

    Ok... I am a student who just learned this semester (about 4 months ago and up until now) how to program in VB. I don't know much about bitblt and neither does anybody that I know, but I did get it to work on a project that I am working on and have been working on for like... 3 months. However, there are some slight defects...

    My project includes an image of a ship at the bottom of the screen, that a player can move back and fourth. The player also may shoot up at enemy ships hovering between it and the top of the screen. The enemies shoot back. Everything is made out of images (not pictureboxes).

    When I tried to use bitblt to make my ship be something other than a box, I was unsuccessful at first. I kept trying and eventually got that to work, and the ship was no longer flickering occasionally. Then the enemies and everything else started flickering like crazy !

    I tried solving my problem by using bitblt on the enemies as well ... That only resulted in either no enemies at all and just my ship, only one enemy, or nothing at all ... I mean like... What's up with that?? I have been looking around for a while for a solution, and have not found any help. If anybody knows how to fix this then PLEASE respond to this


    HELP!

  7. #7
    Member
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    41

    Re: VB 6 - BitBlt

    Quote Originally Posted by Elysees
    I have a question here.

    I read your explanation and downloaded your tutorial codes and ran it. I got a problem here.

    I had a little piece of software running, after mask or land or hero have been drawn on the form, I moved the software around and whenever it covered the picture area, the picture was erased by the software's interface.

    Or when I minimize the form and brougt it up again, image on the form disappeared

    Have an idea why?
    Check some of the "AutoDraw" boolean's of the picture boxes you use

  8. #8

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: VB 6 - BitBlt

    Yes sutor redraw must be set to true but, it may not draw correclty with the code I gace you if you change it to true... let me know.

  9. #9
    Junior Member MorbiousNIN's Avatar
    Join Date
    Jun 2005
    Location
    Burlington ontario
    Posts
    21

    Re: VB 6 - BitBlt

    I cant follow the code, since it is in one bit line insted of what ir would normaly look like ^^
    Don't bite the hand that feeds

  10. #10

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: VB 6 - BitBlt

    Quote Originally Posted by MorbiousNIN
    I cant follow the code, since it is in one bit line insted of what ir would normaly look like ^^
    Whats up?
    I cant understand you post?

    Pino

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: VB 6 - BitBlt

    The codes in the 1st post are all coming out on one line.

  12. #12
    Junior Member
    Join Date
    Nov 2005
    Posts
    16

    Re: VB 6 - BitBlt

    Hi! I got like 80 pictures in my animaton. I can't use like 80 picture boxes o_O
    Help...

  13. #13
    Member Shadow503's Avatar
    Join Date
    Apr 2006
    Posts
    37

    Re: VB 6 - BitBlt

    Quote Originally Posted by Tonberry
    Hi! I got like 80 pictures in my animaton. I can't use like 80 picture boxes o_O
    Help...
    For the main images create a picture box. Adjust the properties to fit your needs and then set the index property to 0.

    For my example the name will be picMain. I'm also assuming all the frames for the pictures will be located in the same directory your project is in, and that they will be named 1.gif, 2.gif, 3.gif , ect.

    Then you can use the load statement to create 80 more picture boxes.
    Dim i As Integer

    VB Code:
    1. For i = 1 To 80
    2. Load picMain(i)
    3. picMain.picture = LoadPicture(app.path & "\" i & ".gif")
    4. Next i

    Then you can do the same thing for the mask. I haven't tested this code but by all means it should work.

  14. #14
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: VB 6 - BitBlt

    Quote Originally Posted by Elysees
    I have a question here.
    Or when I minimize the form and brougt it up again, image on the form disappeared
    Set the destination object to AutoRedraw = True.
    After a bitblt call set the destination objects picture to it's image.

    Private Sub Form_Load()
    frmBitBlt.AutoRedraw = True
    End Sub

    Public Sub cmdDrawLand_click()
    drawLand
    frmBitBlt.Picture = frmBitBlt.Image
    End Sub

    hth,
    Ed

  15. #15
    New Member
    Join Date
    Jan 2008
    Posts
    2

    Re: VB 6 - BitBlt

    If I can make you guys a suggestion - setting the destination's autoredraw property to true can become a plague to your resources and your refresh rate will fall if you are doing animations.

    Experiment with getting all bitblt'ing done in the 'form_paint' procedure with autoredraw=false

    To help a little more - don't bitblt (or others like stretchblt, all related ops including setpixel) to the output hDC, this is ugly with flickering and other nonsense once you get a frame rate happening. Make a buffer, if you don't know how to use API calls to make a bitmap buffer which doesn't rely on a form or other container then just load a form at the size you want to make your output display, load the form and .hide it.

    Get the hidden form's hDC, this is your buffer - draw everything into the buffer then try an edit of the following code in your form's "form_paint" routine:

    Code:
    Private Sub form_paint()
        BitBlt Viewer.DC, FormXoFf, FormYoFf, Buffer.w, Buffer.h, Buffer.DC, 0, 0, SRCCOPY
    End Sub
    Secondly - double buffer it so that no ugliness may reach the output pane under any circumstance, this means build the frame/snapshot into buffer1, copy buffer1 to buffer2 everytime the frame is completed and then any/every time that the 'form_paint' routine is called it will be drawing a complete frame onto the output page instead of potentially being called from time to time with a half drawn buffer (more flickering effect if so).

    ** by memory (while since I bothered with this stuff), it is necessary to call 'form_paint' in the code directly after copying the completed buffer1 to buffer2, no need to sense focus or anything else because (while autoredraw=false) 'form_paint' is called not only on focus and resize, but also whenever other forms are dragged, resized or in any way moved in such a way as to reveal a part of 'our' form.

    Experiment with it, try to trust me that autoredraw on the output pane is ugly and slow, egdemeal's idea of setting the picture to it's image is ok if you are not trying to do a lot of clever graphics at a decentish/high frame rate - pretty sure it's overhead on the CPU that could be better spent.


    Just in case you are wondering what makes me think I understand these things, please go and look at my old crap that I wrote before learning that 'form_paint' is the best place to 'paint the form' (LOL!), I did alright and the three frame/rate animation games I am hosting at www.robsfreespace.com/free_basic_games all use bitblt and all are written by me.

    *** I read somewhere that the 'form_paint' procedure runs in the best way possible for outputting graphics, all to do with timing and interrupts - I thought it was bulldust but experiments made it clear that I could get a better frame rate on worse CPUs by using it in certain ways - experiment.

    Regards,
    robsoles.
    Last edited by robsoles; Jan 6th, 2008 at 12:11 AM.

  16. #16
    New Member
    Join Date
    Jan 2008
    Posts
    2

    Re: VB 6 - BitBlt

    Hey Tonberry,

    I've got a funny feeling you don't want to chew up all the resources that 80 pictureboxes would, an alternative is to assemble the pictures onto a single bitmap in an organised way and then pick them out with mathematics, grab mazeracer off me - http://www.robsfreespace.com/free_basic_games/mazeracer - and take a look at the single bitmap (..\gamebits.bmp) which has all the graphics I use in the game, including the background(s).

    - edit: Oops, should mention that the masks for the ships are all generated from the bitmap in the first second or so of being executed.
    Last edited by robsoles; Jan 6th, 2008 at 12:47 AM.

  17. #17
    New Member
    Join Date
    Mar 2008
    Posts
    3

    Re: VB 6 - BitBlt - looking for something to do?

    hi, u mentioned that you're a bit stuck on what to do next...

    i'm in need of a tutorial explaining how to make tile-based maps using a text file... n e idea how to do it, or can you nudge me in the right direction?

    thanks,
    shwhjw

  18. #18
    Member
    Join Date
    Jun 2010
    Posts
    40

    Re: VB 6 - BitBlt

    Ok, but.......
    To get to the result the example produces, it's a lot more simple to just put an Imagebox with the island and over it a Imagebox (transparent gif) with the hero. A LOT more simple.

    So what can you really do with BitBlt that you can't by just working with transparent gif?

  19. #19

    Thread Starter
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: VB 6 - BitBlt

    Quote Originally Posted by IHappenToBe View Post
    Ok, but.......
    To get to the result the example produces, it's a lot more simple to just put an Imagebox with the island and over it a Imagebox (transparent gif) with the hero. A LOT more simple.

    So what can you really do with BitBlt that you can't by just working with transparent gif?
    Have you tried using that for a full game? Lots of bullets on the screen lots of sprites? Using the method you just described your app would come crashing down. So speed is the main thing.

    You also have a lot more control on what part of the sprite you show. Essentially if your writing a game BitBlt should be the minimum and DirectX the ideal.

    This thread is old though!

  20. #20
    Member
    Join Date
    Jun 2010
    Posts
    40

    Re: VB 6 - BitBlt

    Thank you, Pino.
    Oh, now I get it: BitBlt is like GET - PUT in QuickBasic!
    And it's 100% flickering-proof, right?

    I know the thread is very old, but I haven't found any step-by-step tutorial on BitBlt (What it is, what for, why one should use it, why one shouldn't use it, etc).

    Thank you.

  21. #21
    Member
    Join Date
    Jun 2010
    Posts
    40

    Re: VB 6 - BitBlt

    As you will see I have attached 3 pictures, Hero, Hero Mask, and Land
    Sorry for intruding again, I'd hate to be an pain in the *** in my very 2nd BitBlt day, since you're on it at least since 2003.

    I just realized something it may be intresting:
    there is no need for the mask to be black & white. The mask perfectly can be the same coloured Hero but with white background.

    What i'm saying this for? 'Cause it's much more simple just to paint the background. (i.e, you do the mask with just 1 click, even with MsPaint).

    Now I'm off.

  22. #22
    Member
    Join Date
    May 2010
    Posts
    40

    Re: VB 6 - BitBlt

    considering that we have a bitblted image using a picture box or even a form, how can we print it?

  23. #23
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: VB 6 - BitBlt

    Please do not post questions in CodeBank threads unless they are about the actual code that is shown - for other questions on the topic you should create your own thread, which you already had in this case: how to print the bitblt image

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