StretchDIBits() - weird behaviour..

This is a discussion on StretchDIBits() - weird behaviour.. within the Windows Programming forums, part of the Platform Specific Boards category; I'm using aforementioned gdi function to display some data into window's client area (part of memory where i actually write ...

  1. #1
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71

    Question StretchDIBits() - weird behaviour..

    I'm using aforementioned gdi function to display some data into window's client area (part of memory where i actually write some data like lines etc..), i do not use stretching or such thinks, just plain copy from data in form of bitmap.
    I use it A LOT, it handles all drawing to client area, and i encountered problem: sometimes it does neither fail nor draw anything: input params are proper, but it returns 0 as number of lines drawn. Weird is that it happens seemingly without any triggering effect, if i force a window to repaint, it sometimes works again, sometimes not; when i resize window, it works again... I have no clue why might it fail and so far found no evidence of errors in my own code (it actually WORKS almost all the time without errors or problems and/even after such a draw error).
    I still hope i'll find error within the code, but it is unlikely, so i started to 'blame' the function itself. May it be kind of 'overload'?
    I tried it only on WinXP (Pro) systems so far.

    Any ideas?
    Last edited by lobo; 09-24-2003 at 07:07 AM.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    not much to work with here. Is the memory DC being recreated every time? Just when you resize? I wonder if your DC being recreated on the resize is what's causing it to work again. As if the DC got hosed. Difficult to say without seeing the code I guess.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71
    Code:
    case WM_PAINT:
    //shall we draw?
        ld = (P_LOCAL_DATA) GetWindowLong(hwnd, 0);
    
        hdc = BeginPaint(hwnd, &ps);
    
        if (lcs->v[ld->id].view__viewport.viewport__data != NULL)
        {
            bmi.bmiHeader.biWidth = lcs->v[ld->id].view__viewport.viewport__size_x;
            bmi.bmiHeader.biHeight = lcs->v[ld->id].view__viewport.viewport__size_y;
    
            long lines;
    
            if (GDI_ERROR == (lines = SetDIBitsToDevice(hdc, 0, 0, lcs->v [ld->id].view__viewport.viewport__size_x,
              lcs->v[ld->id].view__viewport.viewport__size_y, 0, 0,
              0, lcs->v[ld->id].view__viewport.viewport__size_y,
              (P_VOID) lcs->v[ld->id].view__viewport.viewport__data,  &bmi, DIB_RGB_COLORS)))
         }
    
         EndPaint(hwnd, &ps);
         break;
    Code looks like this.
    I tried SetDIBitsToDevice(), but to the same result. Those structs point to window-specific data (dimensions, memory area to be displayed..) and contain valid data, that is sure. Bmi struct is static and fully initialised in WM_CREATE, here is only local dimension specification.

    Most important question (to me) is 'Can used function (setdibitstodevice or stretchdibits) return 0 drawn lines (as it does) even if all params are ok and it should draw +-hundreds of them???'

    Small note: resizing of window does sometimes cause data to appear (eg. cause successfull draw) but it works other way as well - sometimes if i resize window with displayed data, new draw does not display anything...
    Last edited by lobo; 09-24-2003 at 07:58 AM.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ok, I think I know what it is. The paint DC has an HREGION selected into it. This means that you will only be able to see the part that was Invalidated. If you want to force a repaint of the entire window, you need to InvalidateRect() with NULL passed in for the rect.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71
    i'm lazy, so i used InvalidateRect(, NULL, TRUE) anytime i needed redraw (eg. anytime i changed data in memory to be displayed), so that is not the problem.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    try ignoring the paintdc and using GetDC on the window to get a different one to draw on. At least that way we can eliminate the paintDC as being the problem.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71
    I did that to no success. However, i run into some unexpected behaviour caused by using GetDC() and InvalidateRect(.., NULL, TRUE) in one handler, so it *might* give me a clue to any problematic stuff there. I'll check it later evening when i have time and let know results.

    Still, it would be useful to know why does used draw function (Stretch...) draw 0 lines when it is supposed to draw more acc. to its params and still report no errors... that would make things clearer, i bet .

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Sorry. I can't do much without being able to debug the project myself.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71
    What do you think i've been doing last three days ?

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    um... you debugging it doesn't help me find your problem I'm afraid.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #11
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    GDI_ERROR (-1) is not returned by SetDIBitsToDevice() if it fails.

    It returns 0.

    StretchDIBits() returns GDI_ERROR.

    Use GetLastError() if SetDIBitsToDevice() returns zero and output to a messagebox your params.

    How are you filling the rest of the bitmap header? Is the header struct a local and non static?

    Do you check that you are actually asking it to draw some lines ie not drawing 0 lines?

    How do you know this index, ld->id, is valid? (just a suggestion)

    Try using the rect returned in the paintstruct for the update area.
    It may not be your app that calls for the paint (and so sets the update area to the whole client area)

    Do you have any non static local variables that are used by this function?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  12. #12
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71

    Lightbulb

    Thank you guys for suggestions :).. i did not find exact cause of the problem yet, but i managed to isolate part of preprocessing that seems to trigger possibility of error.. I'm not absolutely sure about it, but when i disabled it, i did not run into the problem so far. I should have no problems finding that error now, as it is only a few hundred lines of code to check :o).

    to Novacain specially:

    >GDI_ERROR (-1) is not returned by SetDIBitsToDevice() if it fails.
    >It returns 0

    My overlooking, thanks for correcting :).. i use StretchDIBits(), but error value from SetDIBitsToDevice() will be useful, too.
    The rest of things you suggested cannot play any role there, they are either being checked or are valid due to internal logic (like number of lines or indices).

    edit: i tried GetLastError(). Returned 0 :-|.
    Last edited by lobo; 09-25-2003 at 05:30 PM.

  13. #13

  14. #14
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Holy thread necrophillia batman!
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 10:23 PM
  2. Replies: 16
    Last Post: 04-20-2008, 01:15 PM
  3. VC++ 2005 Express: Weird Behaviour
    By psychopath in forum Tech Board
    Replies: 2
    Last Post: 06-21-2006, 07:47 PM
  4. Weird Characters With GetDlgItemText
    By execute in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2006, 04:53 PM
  5. weird error
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 07-17-2005, 07:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21