Basic SetDIBitsToDevice question

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?
Who is Participating?
rajeev_devinConnect With a Mentor Commented:
ret = SetDIBitsToDevice(
     -hDest, // Make this negative
olegspAuthor Commented:
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?
DanRollinsConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.