CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2011
    Posts
    22

    My DIBs are 2 Bytes Short

    Hi guys,

    I noticed that my DIBs, both saved and copied to the clipboard, are 2 bytes short.

    Code:
    DWORD dwSize=sizeof(BITMAPINFO)+height*span;
    HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwSize);
    LPBYTE mem=(LPBYTE)GlobalLock(hGlobal);
    ...
    // fill in BITMAPINFO
    BITMAPINFO* bmi=(BITMAPINFO*)mem;
    ...
    // copy bits
    LPBYTE bits=mem+sizeof(BITMAPINFO)+2;
    // if I don't add the +2 the colors are shifted
    ...
    Where are those 2 bytes coming from ?

    My filesize is always 2 bytes smaller than the same bitmap saved in Photoshop, and if I copy to clipboard the colors are shifted by two bytes (red is blue, green is red, blue is green.)

    Thanks

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: My DIBs are 2 Bytes Short

    See this http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx under remarks.

    The bitmap data has to be LONG or DWORD aligned (both 32 bits) so I guess that sizeof(BITMAPINFO) is not a multiple of 4 bytes.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: My DIBs are 2 Bytes Short

    In addition, " each scan line must be padded with zeros to end on a LONG data-type boundary", so height*span might be wrong if you are using a color table and indices into the color table.

    Of course, if you are using RGBQUADs, then height*span is automatically fine.

    Mike

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

Featured