Basic SetDIBitsToDevice question

Posted on 2006-04-03
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?
Question by:olegsp
    LVL 12

    Accepted Solution

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

    Author Comment

    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?
    LVL 49

    Assisted Solution

    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

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now