Posted on 2004-10-05
Medium Priority
Last Modified: 2013-12-26

Hey.  I'm using a big bitmap in my VB app, for various reasons I have used VB resource editor to load it into a RES file which is included in the project.  The bitmap called itself image number 101.

Now how do I use it?  It looks like LoadImage requires use of the MAKEINTRESOURCE macro to turn the number 101 into the proper resource identifier.  But VB won't recognize this macro.  How can I declare it?  Or how can I use LoadImage w/o it?

Question by:riceman0
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
LVL 10

Expert Comment

ID: 12226339
LoadResPicture(101, vbResBitmap) returns a reference to your bitmap.
LVL 10

Expert Comment

ID: 12226529
Sorry - I should have said
LoadResPicture(101, vbResBitmap) returns a reference to your bitmap as a stdPicture object

Therefore you should be able to work with this...
    hBMP = LoadResPicture(101, vbResBitmap).handle

Author Comment

ID: 12226869

Looks great, but doesn't seem to work.  The old way:

Dim hbmpBack as long
hdcBack = CreateCompatibleDC(hdcCompat)
Call SelectObject(hdcBack, hbmpBack)

works when I try to BitBlt from hdcBack to a picturebox later.  But the new way (using a resource instead of a file)

Dim hbmpBack as long
hbmpBack = LoadResPicture(101, vbResBitmap)
hdcBack = CreateCompatibleDC(hdcCompat)
Call SelectObject(hdcBack, hbmpBack)

seems to result in a black field for BitBlt-ing.  However the LoadResPicture call seems to work, hbmpBack returns a value in both cases.  The picture should be there, compiled into a RES file and appears in the project explorer under "Related documents."  Any ideas?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 10

Accepted Solution

fds_fatboy earned 500 total points
ID: 12227244
Hmm... That's a surprise. It works a treat for me. Here's the exact code I use if you want to compare and contrast because unfortunately I can't spare the time that this problem requires until I get out of work tonight.

Public Sub DayGraphic(X As Long, Y As Long, Graphic As HighlightGraphic, Color As OLE_COLOR)
    Dim pic As StdPicture
    Dim hBmp As Long
    Dim bmp As BITMAP
    Dim SrcDC As Long
    Dim HoldingDC As Long
    Dim HoldingBmp As Long
    Dim PrevHoldingBmp As Long
    Dim RGBColor As Long
    Dim rcPaint As RECT
    Dim LlnghBrush As Long

    Const LstrPROCEDURE As String = MstrMODULE & ".DayGraphic" ' The procedure name.
    On Error GoTo ErrorHandler

    Select Case Graphic
    Case [Crossed out date]
        If MlnghCrossDC = 0 Then
            MlnghCrossDC = CreateCompatibleDC(picDates.hdc)
            Set pic = LoadResPicture("CrossedOut", vbResBitmap)

            MlnghCrossBmp = pic.Handle
            MlnghPrevCrossBmp = SelectObject(MlnghCrossDC, MlnghCrossBmp)
        End If

        hBmp = MlnghCrossBmp
        SrcDC = MlnghCrossDC
    Case [Ringed date]
        If MlnghRingDC = 0 Then
            MlnghRingDC = CreateCompatibleDC(picDates.hdc)
            Set pic = LoadResPicture("ringedDate", vbResBitmap)

            MlnghRingBmp = pic.Handle
            MlnghPrevRingBmp = SelectObject(MlnghRingDC, MlnghRingBmp)

        End If

        hBmp = MlnghRingBmp
        SrcDC = MlnghRingDC
    End Select

    GetObject hBmp, Len(bmp), bmp

    rcPaint.Left = 0
    rcPaint.Top = 0
    rcPaint.Right = bmp.bmWidth
    rcPaint.Bottom = bmp.bmHeight

    HoldingDC = CreateCompatibleDC(MlnghBufferDC)
    HoldingBmp = CreateCompatibleBitmap(MlnghBufferDC, rcPaint.Right, rcPaint.Bottom)
    PrevHoldingBmp = SelectObject(HoldingDC, HoldingBmp)

    RGBColor = TranslateOLEColor(Color)

    If RGBColor <> vbWhite Then
        LlnghBrush = CreateSolidBrush(RGBColor)
        FillRect HoldingDC, rcPaint, LlnghBrush
        DeleteObject LlnghBrush
        BitBlt HoldingDC, 0, 0, rcPaint.Right, rcPaint.Bottom, SrcDC, 0, 0, vbSrcPaint
        ReleaseDC HoldingBmp, SrcDC
        TransparentBlt MlnghBufferDC, X, Y, rcPaint.Right, rcPaint.Bottom, HoldingDC, 0, 0, rcPaint.Right, rcPaint.Bottom, vbWhite
        ReleaseDC MlnghBufferBmp, HoldingDC
        BitBlt HoldingDC, 0, 0, rcPaint.Right, rcPaint.Bottom, SrcDC, 0, 0, vbNotSrcCopy
        ReleaseDC HoldingBmp, SrcDC
        TransparentBlt MlnghBufferDC, X, Y, rcPaint.Right, rcPaint.Bottom, HoldingDC, 0, 0, rcPaint.Right, rcPaint.Bottom, vbBlack
        ReleaseDC MlnghBufferBmp, HoldingDC
    End If

    SelectObject HoldingDC, PrevHoldingBmp
    DeleteObject HoldingBmp
    DeleteDC HoldingDC

    Exit Sub
'Error handler code [Generated 18/05/04 Using LCPEditToolbox Ver 1.2 build 90]
'This code block must be the last code in the procedure.
    'Clean up
    Set pic = Nothing

    With Err
        If .Source = App.Title Then
            .Source = App.Title & "::" & LstrPROCEDURE
        End If

        .Raise .Number, .Source, .Description, .HelpFile, .HelpContext
    End With

End Sub

Author Comment

ID: 12228485

Perfect.  Borrowing from your code, this fixed it:

        Dim pic As StdPicture
        Set pic = LoadResPicture(101, vbResBitmap)
        hbmpBack = pic.handle

Thanks a lot!
LVL 10

Expert Comment

ID: 12231904
Ahh -  .handle - I didn't spot that earlier - but as I said , I was a bit busy.
Glad it helped.

I also noticed in my code, I forgot to set the pic to nothing so it's probably leaking away - that code's been running for a year or so as well.

Author Comment

ID: 12232633
I'm suffering from leaks too.  When exactly do you need to set things to nothing?  Is the rule that you need one Nothing for every Set?
LVL 10

Expert Comment

ID: 12236192
>>Is the rule that you need one Nothing for every Set?

Yes - before it goes out of scope.

Some people will tell you that VB is meant to handle it but I have found it doesn't necessarily. Another nasty is when you have parent and child objects and the child has a reference to its parent. Standard practice is to set module level object references to nothing in the terminate event (Unload event in the case of a form) but in this case, neither object will ever terminate because of the reference held by the other. In this case the the parent must tell the child to remove the reference to itself before it attempts to terminate. Knowing when to to this can be tricky. A way round this is to use store objPtr to the parent as a Long and use it create a temporary local reference to the parent when you need it - removing it as soon as you have finished with it. But that is getting way off topic on this question.
LVL 10

Expert Comment

ID: 12236222
Also to avoid leaks, I should have said every SelectObject should be put back the way it was. Every object created with an API call should have a DeleteObject/DeleteDC, every blt should have a releaseDC etc.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question