ImageList to DIB

ImageList to DIB

--------------------------------------------------------------------------------

This article was contributed by Mike Wild.

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( 00);
    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, 0255
            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.

Date Posted: 5 May 1998

Last updated: 22 March 1999

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值