Results 1 to 5 of 5

Thread: CreateBitmap API

  1. #1

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Question CreateBitmap API

    How can I create a Bitmap from an Array of Bytes?

    I tried using createBitmap but I couldn't make it work... Could anyone give me an example of how it is used?


    VB Code:
    1. Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  2. #2
    Junior Member
    Join Date
    Oct 2002
    Posts
    27

    Some answers, not all...

    VB Code:
    1. Public Type BITMAP ' 14 Bytes
    2.     bmType As Long
    3.     bmWidth As Long
    4.     bmHeight As Long
    5.     bmWidthBytes As Long
    6.     bmPlanes As Integer
    7.     bmBitsPixel As Integer
    8.     bmBits As Long
    9. End Type
    10.  
    11. Public Declare Function GetObject Lib "gdi32" Alias "GetObjectA" _
    12.     (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long

    That is the code to get information about an image or picture, hObject is the handle to that object.

    The code you have offered would appear to create such an object using several of the values listed here.
    Namely width, height, planes, bitspixel (nBitCount) and lpBits.

    lpBits is a pointer to an array of the appropriate length, make sure you get this right!

    In VB you can pass this array by simply entering the first element of that array.

    I have gotten the Set/GetBitmapBits to work which works on fundamentally the same principle. Let me know if you get this to work properly, although I will try it when I have a little time!

    Oh yea, if it follows the same idea as other API calls, the return value will be a pointer to the object (handle)

  3. #3
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    280
    hi,

    although is now not updated the guide at allapi.net has an example that might help (actually the whole guide is invaluable...)

    it create a bitman from a array of 8 bytes (a hatched effect) (then greats a brush and fills a form with....)

    works for me..

    (the comments within the code acknowledge author.....)

    Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type
    Private Declare Function CreatePatternBrush Lib "gdi32" (ByVal hBitmap As Long) As Long
    Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
    Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Integer) As Long
    Dim bBytes(1 To 8) As Integer
    Private Sub Form_Paint()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    Dim R As RECT, mBrush As Long, hBitmap As Long
    For mBrush = 1 To 8 Step 2
    bBytes(mBrush) = 170 '170 = 10101010
    bBytes(mBrush + 1) = 85 '85 = 01010101
    Next
    'Create a memory bitmap
    hBitmap = CreateBitmap(8, 8, 1, 1, bBytes(1))
    'Create the pattern brush
    mBrush = CreatePatternBrush(hBitmap)
    SetRect R, 0, 0, Me.ScaleWidth, Me.ScaleHeight
    'Fill the form
    FillRect Me.hdc, R, mBrush
    'Clean up
    DeleteObject mBrush
    DeleteObject hBitmap
    End Sub

  4. #4
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,238

    Re: CreateBitmap API

    Sorry to bump such an old thread, but this is the only thread that already exists on these forums, that has any information about the CreateBitmap API function. And I figured it was better to bump an old thread, than to create a new thread about a topic that already has a thread.

    I'm having a problem with this. I've created my own screencap software, using this technique and using the first cell of a byte array as the pointer just like this example you gave. I create a Bitmap, a DC, select the bitmap into the DC, BitBlt the screen's DC to my own DC, and then I'm expecting the raw bytes of image data to be present in the byte array. But it's not. It's full of 0's as if there's no image data, but then I BitBlt it back out of this DC I made, out to my VB6 form's DC, and the screencap appears on my form. It appears to be storing the image data SOMEWHERE, but not in the array that it was SUPPOSED TO store it in.

    Has anybody else had this problem?

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: CreateBitmap API

    Feel you should start your own thread. There are several potential problems with the code provided. Information that wasn't provided and maybe should have, specifically Word alignment of bitmap rows. Additionally, we don't have your code that you used, obviously you are not using an 8x8 bitmap at 1 bit per pixel.

    But the main reason for your confusions, is that Blting to a DC does not update the byte array. You have to transfer the bitmap bits back into the byte array. In other words, the byte array is no longer referenced by the bitmap, after you call CreateBitmap API.

    I and others obviously won't mind responding to a thread of your own. Personally, I don't like continuing with a 'hijacked' thread simply because replies can trigger off emails to people that have subscribed to this one. Don't feel that is appropriate and/or fair to the original poster. Anyway, just my feeling about using others threads...
    Last edited by LaVolpe; Dec 9th, 2014 at 05:47 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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