Results 1 to 3 of 3

Thread: Please Help: Cant load MemoryDC properly in NT, XP, 2000

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Location
    New Jersey
    Posts
    1

    Unhappy Please Help: Cant load MemoryDC properly in NT, XP, 2000

    I am in search of a work around for loading an image from file into a memory DC. I really hope someone can help as I have been looking for a solution for months.

    It appears to me that when a single image is loaded into a memory DC, that NT, XP, and 2000 work correctly. In my case, I am loading many images into separate memory DC's, and the problem occurs consistently.

    The following code to load the memory DC's works wonderfully for Windows 95,98,ME. It also works sometimes under Windows NT,2000,XP when small single images are loaded. Unfortunately, when large and high quantity images are loaded under Windows NT,2000,XP, I get strange results. (including image trailing, unloaded dc's, etc)

    Here is the subroutine I use to load my Memory DC's (a modified version of Soren's):


    '=====================================================
    'LOAD A MEMORY DC WITH THE BITMAP
    '=====================================================
    Public Function GenerateDC(FileName As String) As Long
    On Error GoTo BugTrap

    Dim DC As Long
    Dim hBitmap As Long

    'Create a Device Context, compatible with the screen
    DC = CreateCompatibleDC(0)

    If DC < 1 Then
    GenerateDC = 0
    Exit Function
    End If

    'Load the image....BIG NOTE: This function is not supported under NT, there you can not
    'specify the LR_LOADFROMFILE flag
    hBitmap = LoadImage(0, FileName, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE Or LR_LOADFROMFILE Or LR_CREATEDIBSECTION)

    If hBitmap = 0 Then 'Failure in loading bitmap
    DeleteDC DC
    GenerateDC = 0
    Exit Function
    End If

    'Throw the Bitmap into the Device Context
    SelectObject DC, hBitmap

    'Return the device context
    GenerateDC = DC

    'Delete the bitmap handle object
    DeleteObject hBitmap

    '=====================================================
    Exit Function
    BugTrap:
    BugReport ("System/GenerateDC")
    '=====================================================
    End Function
    '=====================================================

    Thank you all in advance, and I hope someone can help me out! Please feel free to email me at [email protected] if you would like to discuss it further.

  2. #2
    Zaei
    Guest
    Dont use CreateCompatibleDC(0). Use CreateCompatibleDC(GetDC(GetDesktopWindow())).

    Z.

  3. #3
    Addicted Member Janus's Avatar
    Join Date
    Aug 2001
    Location
    California
    Posts
    221
    You can't place the results of GetDesktopWindow in the GetDC call, it's not safe :P store it in a variable so you can be guaranteed success releasing the DC after the CreateCompatibleDC call.
    "1 4m 4 1337 #4xz0r!'
    Janus

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