您的位置:首页 > 其它

UltraGrid单元格放多个图片

2014-01-02 19:33 316 查看
private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e)
{
    // Get the images that we might want to show in the cell.
    // You could optimize this method by caching the Bitmaps as members of the class.
    using( Bitmap bmp1 = new Bitmap( @"..\..\squiggle_up.bmp" ) )
    using( Bitmap bmp2 = new Bitmap( @"..\..\arrow_up.bmp" ) )
    using( Bitmap bmp3 = new Bitmap( @"..\..\squiggle_down.bmp" ) )
    using( Bitmap bmp4 = new Bitmap( @"..\..\arrow_down.bmp" ) )
    {
        Bitmap[] bmps;
        // Using whatever conditions you need, populate the array of Bitmaps
        // with the appropriate images. For this simple demo, we determine
        // the images to use based on the customer ID field.
        int custID = (int)e.Row.Cells[ "custID" ].Value;
        if( custID < 1 )
            bmps = new Bitmap[]{};
        else if( custID == 1 )
            bmps = new Bitmap[]{ bmp1 };
        else if( custID == 2 )
            bmps = new Bitmap[]{ bmp1, bmp2 };
        else if( custID == 3 )
            bmps = new Bitmap[]{ bmp1, bmp2, bmp3 };
        else
            bmps = new Bitmap[]{ bmp1, bmp2, bmp3, bmp4 };
        // If we have already given this cell a Bitmap, dispose of it before reassigning the new Bitmap.
        if( e.ReInitialize )
            ((Bitmap)e.Row.Cells["images"].Value).Dispose();
        // Pass the array of Bitmaps into the helper method which will
        // combine all of the images into one. Then assign that new image
        // to the value of the cell.
        e.Row.Cells["images"].Value = CombineBitmaps( bmps );
    }
}
private Bitmap CombineBitmaps( Bitmap[] bitmaps )
{            
    if( bitmaps.Length == 0)
        return new Bitmap( 1, 1 );
    int width = 0;
    int height = 0;
    foreach( Bitmap bmp in bitmaps )
    {
        width += bmp.Width;
        if( height < bmp.Height )
            height = bmp.Height;
    }
    Bitmap bitmap = new Bitmap( width, height );    
    using( Graphics grfx = Graphics.FromImage( bitmap ) )
    {
        int x = 0;
        foreach( Bitmap bmp in bitmaps )
        {            
            grfx.DrawImage( bmp, x, 0, bmp.Width, height );
            x += bmp.Width;
        }
    }
    return bitmap;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: