Converts a image list to DIB

In my PrintTree sample I wanted to print the tree in color. This was easy for text, but not for the icons. I tried a lot to convert the imagelist to DIB, but what I really needed was a method, which gives back the DIB of one icon, not of the whole Imagelist. And here is the used method:
HANDLE CAdvancedTreeCtrl::ImageToDIB( CImageList* pImageList, int iImageNumber, CWnd* pWnd,
                                      BOOL bOverlay, HTREEITEM actualItem )
{
    // Local Variables
    CBitmap 	bitmap;
    CWindowDC	dc( pWnd );

    CDC 		memDC;
    CRect		rect;
    CPalette    	pal;
    IMAGEINFO   	imageInfo;

    if( FALSE == pImageList->GetImageInfo( iImageNumber, &imageInfo ) )
    {
        	// Getting of the Imageinfos failed
        	return NULL;
    }

    // Create compatible stuff and select Bitmap
    if( FALSE == memDC.CreateCompatibleDC( &dc ) )
    {
        	// Create failed
        	return NULL;
    }

    if( FALSE == bitmap.CreateCompatibleBitmap( &dc, 
                                            imageInfo.rcImage.bottom-imageInfo.rcImage.top, 
                                            imageInfo.rcImage.right-imageInfo.rcImage.left ) )
    {
        	// Create failed
        	return NULL;
    }

    CBitmap* pOldBitmap = memDC.SelectObject( &bitmap );
    if( NULL == pOldBitmap )
    {
        	// Select failed
        	return NULL;
    }

    // Local Variables for Draw
    CPoint point( 0, 0);
    UINT nStyle = ILD_NORMAL;

    // Is there an Overlay
    if( TRUE == bOverlay )
    {
        	// Set the wanted style
          nStyle = ILD_TRANSPARENT | ( TVIS_OVERLAYMASK & GetItemState( actualItem, TVIS_OVERLAYMASK ) );
    }

    // Draw Image to the compatible DC
    if( FALSE == pImageList->Draw( &memDC, iImageNumber, point, nStyle ) )
    {
        	// Drawing of the Image failed
        	return NULL;
    }

    // Create logical palette if device support a palette
    if( dc.GetDeviceCaps( RASTERCAPS ) & RC_PALETTE )
    {
		UINT        nSize   = sizeof(LOGPALETTE) + ( sizeof(PALETTEENTRY) * 256 );
		LOGPALETTE* pLP     = (LOGPALETTE*)new BYTE[nSize];
		pLP->palVersion     = 0x300;
		pLP->palNumEntries = (unsigned short)GetSystemPaletteEntries( dc, 0, 255, 
		pLP->palPalEntry );

		// Create the palette
		pal.CreatePalette( pLP );

		// Free memory
		delete[] pLP;
    }

    memDC.SelectObject( pOldBitmap );

    // Convert the bitmap to a DIB
    return DDBToDIB( bitmap, BI_RGB, &pal );
}


The DDBToDIB-Method is the one already published under "Converting DDB to DIB".

Bugs and Improvements: Please report all bugs and improvements to me, thanks and enjoy it.



Comments

  • Bitmap already as bitstring array, need help

    Posted by Legacy on 10/19/2000 12:00am

    Originally posted by: Andy Brookfield

    I have an array of "BYTES" where every BIT represents a dot
    
    in a bitmap. I need to create a B/W bitmap using this
    data. Any and all help would be greatly appreciated

    Much appreciated
    Andy

    Reply
  • How to convert a .doc file into a .tif ( image ) file

    Posted by Legacy on 03/16/2000 12:00am

    Originally posted by: Prasad


    I want to convert a .doc ( Document ) file
    into a .tif ( image file ) . Do you have any idea?

    Reply
  • Tried to email...

    Posted by Legacy on 11/02/1999 12:00am

    Originally posted by: Dark Mage

    Tried to email the author, but it failed.

    Wanted to be able to convert a long bitmap to a smaller CBitmap using StretchBlt() in use on a button, and was looking for help.

    *DM*

    Reply
  • adding the image to the tree item

    Posted by Legacy on 08/05/1999 12:00am

    Originally posted by: debuke

    we have setitemimage(), but that only pertains the image list. what's done with this image after it's converted to associate it to the tree item? how is this handle used?

    is the sourcecode for this example available to see how this is used?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • When it comes to desktops – physical or virtual – it's all about the applications. Cloud-hosted virtual desktops are growing fast because you get local data center-class security and 24x7 access with the complete personalization and flexibility of your own desktop. Organizations make five common mistakes when it comes to planning and implementing their application management strategy. This eBook tells you what they are and how to avoid them, and offers real-life case studies on customers who didn't let …

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds