We help IT Professionals succeed at work.

Basic SetDIBitsToDevice question

olegsp asked
Medium Priority
Last Modified: 2013-12-03
I want to display a rectangle area from my bitmap on a screen. The area in bitmap is defined by its upper-left pixel coordinate (xBmp, yBmp), and size (wBmp, hBmp). The screen area is defined similarly with (xDest, yDest), and size (wDest, hDest). Since no bitmap stretching is done, wBmp=wDest, and hBmp=hDest. I am using the following code for display:
ret = SetDIBitsToDevice(
      hDC,                 // handle to DC
      xDest,               // x-coord of destination
      yDest,               // y-coord of destination
      wDest,           // source rectangle width
      hDest,          // source rectangle height
      xBmp,                // x-coord of source
      yBmp,                // y-coord of source
      0,         // first scan line in array
      hBmp,         // number of scan lines
      pix,     // array of DIB bits
      bInfo, // bitmap information
      DIB_RGB_COLORS          // RGB or palette indexes
and yet it shows my bitmap shifted vertically. What am I doing wrong?
Watch Question

ret = SetDIBitsToDevice(
     -hDest, // Make this negative

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Now it fails to display anything at all, and returns GDI error.

It worked with positive hDest. I looked thru a lot of online examples, but they all use the simplest scenario of xDest=yDest=xBmp=yBmp=0. In my case, these aro not zeros. In particular, in my general case, where should I start my scan line, and how many scan lines should I use?
Author of the Year 2009
Based on your variable naming conventions, you may be setting the wrong values into:
    wDest,           // source rectangle width
    hDest,          // source rectangle height

These need to match values for the DIB (which is the source, not the Dest).

If that is not correct, then please decribe more specifically what you mean when your say "shifted vertically".  Thanks.

-- Dan
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.