Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

Bitmap: how fill with image before call to BitBlt()?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
noleander - 22 Jun 2005 23:49 GMT
Hey.  I've got a color photo in a simple unsigned charRGB array.  The photo
does not exist on disk.  I want to display the photo in my window.

The application I am working in (not my choice) demands that I draw into the
display by first creating a Bitmap, and then drawing into a DisplayContext
using BitBlt().  The pseudocode looks something like:

  // Create an empty bitmap:
  HDC hScreen = GetDC (0);
  pHandle = CreateCompatibleBitmap (hScreen, width, height);
  // Here is the problem:  How do I copy my photo into the bitmap
  // at this point??
  // .... TBS ....  magic here ....
  // Now write the bitmap into the display:
  HDC hdcMemory = CreateCompatibleDC ((HDC)pSystemContext);
  hOldObj = SelectObject (hdcMemory, pHandle);
  BitBlt ((HDC)pSystemContext,  ....(HDC)hdcMemory, ..., SRCCOPY);

My question is:  How do I transfer the pixels from my photo in memory
(unsigned char array)  into the Bitmap?      I understand that the bitmap
type - by design - is very opaque and I dont expect to have direct access to
the innards.   But surely there must be some function call that will take an
RGB array of pixels and put it into the bitmap?

Thanks in advance for any help,
 neal
Jonathan Wilson - 23 Jun 2005 01:56 GMT
> My question is:  How do I transfer the pixels from my photo in memory
> (unsigned char array)  into the Bitmap?      I understand that the bitmap
> type - by design - is very opaque and I dont expect to have direct access to
> the innards.   But surely there must be some function call that will take an
> RGB array of pixels and put it into the bitmap?
Try SetDIBits
Jonathan Wilson - 23 Jun 2005 01:57 GMT
> My question is:  How do I transfer the pixels from my photo in memory
> (unsigned char array)  into the Bitmap?      I understand that the bitmap
> type - by design - is very opaque and I dont expect to have direct access to
> the innards.   But surely there must be some function call that will take an
> RGB array of pixels and put it into the bitmap?
Or you could use CreateDIBSection to create the HBITMAP, that might work too.
noleander - 27 Jun 2005 16:36 GMT
I got this working, finally.

For the record, here is the code that does the job:  note I chose to use the
function SetDIBitsToDevice()  although StretchDIBits() will also work.

=========================

       // createa dummy image
       unsigned char img[3*100*100];

       for ( int i =0; i< (3*100*100); i++ ) img[i] = 0;

       for ( int r=0; r<100; r++ ) {
           for ( int c=0; c<100; c++ ) {

               img[ 3*(r *100 + c)   ] =  r*2;  // unsinged char
               img[ 3*(r*100 + c) + 1]  = 255;
               img[ 3*(r*100 + c) + 2] = 255;

           }
       }

       long width = 100;
       long height = 100;

       HWND hwnd;
       hwnd =  (HWND)(frame->getSystemWindow ());
       HDC hdc = GetDC (hwnd);

       // Allocate enough memory for the BITMAPINFOHEADER and 256 RGBQUAD
palette entries
       // NOTE: the pallet bytes are ONLY NEEDED for  color LUT images ...
not needed here
       LPBITMAPINFO lpbi = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)
+ (1 * sizeof(RGBQUAD))];

       lpbi->bmiColors[1].rgbBlue = 0;  // NOT NEEDED
       lpbi->bmiColors[1].rgbGreen = 0;
       lpbi->bmiColors[1].rgbRed = 0;

       // These are all the members of the bitmap header struct
       lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);  // bytes
       lpbi->bmiHeader.biWidth = 100;   // pixels units
       lpbi->bmiHeader.biHeight = -100;  // negative == top down;  pos =
origin LLeft
       lpbi->bmiHeader.biPlanes = 1;   // must be 1
       lpbi->bmiHeader.biBitCount = 24;  // can be 32 for 4-byte pixels
(Upper byte ignored)
       lpbi->bmiHeader.biCompression = BI_RGB;  // BI_RGB means uncompressed
       lpbi->bmiHeader.biSizeImage = 0;  // size of img in bytes;  0 okay
for BI_RGB
       lpbi->bmiHeader.biXPelsPerMeter = 0;
       lpbi->bmiHeader.biYPelsPerMeter = 0;
       lpbi->bmiHeader.biClrUsed = 0;  // LUTs only
       lpbi->bmiHeader.biClrImportant = 0;  // LUTs only

       // Draw the image into the CRT device
       ::SetDIBitsToDevice(
           hdc, // handle to DC
           0,0 , // x-y-coord of destination upper-left corner
           100, 100, // width-height of source rectangle
           0,0, // x-y-coord of source upper-left corner
           0,//uStartScan,// first scan line in array
           100, // number of scan lines ... usu same as height above
           img, // array of DIB bits
           lpbi, // bitmap information
           DIB_RGB_COLORS); // RGB vs. palette indexes ... RGB means raw
RGB pixels;
                  // alternative is a color LUT pallet.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.