centered Picture in Cell

Hi, My bitmap has the size 16x16, my column 24x20. I'd like to center/middle the picture. How can i center a Picture in a Gridcell ? hEgerth

1 Reply

CB Clay Burch Syncfusion Team August 13, 2002 09:08 AM UTC

If you are using the ImageGridCell class from the ExcelSelectionMarker sample to display the bitmap, then you can change the OnDraw override to draw a centered bitmap. Here is some sample code.
protected override void OnDraw(System.Drawing.Graphics g, System.Drawing.Rectangle clientRectangle, int rowIndex, int colIndex, Syncfusion.Windows.Forms.Grid.GridStyleInfo style)
{
	if (clientRectangle.IsEmpty)
		return;
		
	if(style.Tag is Bitmap)
	{
		Size bmSize = ((Bitmap) style.Tag).Size;
		Rectangle targetRect = new Rectangle(clientRectangle.X, clientRectangle.Y, bmSize.Width, bmSize.Height);

		if( bmSize.Width < clientRectangle.Width )
			targetRect.X += (clientRectangle.Width - bmSize.Width) / 2;
		if( bmSize.Height < clientRectangle.Height )
			targetRect.Y += (clientRectangle.Height - bmSize.Height) / 2;

		g.DrawImage((Bitmap) style.Tag, targetRect);
	}
}

Loader.
Up arrow icon