We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to set background image to fit proportionally in Cell?

Platform: WinForms |
Control: GridDataBoundGrid(Classic)
Tags: image, image-cell

By default, the image will be placed in the cell based on its size. But it will not be change based on the cell size (while resizing the column or row). So that the custom cell renderer and cell model has to be created for achieving this. For more information about creating the custom cell renderer and cell model, please refer this : custom cellrender and cellmodel

In the below customization, the rectangle of the image is drawn based on the client rectangle of the cell (size and height).


The below codes are used to draw the image in cell using the Byte Array using OnDraw method.


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)
        byte[] pict = style.CellValue as byte();
        if (pict != null)
            int PictOffSet = ((GridImageCellModel)this.Model).PictureBufferOffset;
            Image image = null; //Image.FromStream(buffer, true);
            using (MemoryStream mStream = new MemoryStream(pict))
                image = System.Drawing.Image.FromStream(mStream);
            GridImageCellDrawOption cellDrawOption = ((GridImageCellModel)this.Model).CellDrawOption;
            System.Drawing.GraphicsUnit gu = System.Drawing.GraphicsUnit.Point;
            RectangleF srcRect = Image.GetBounds(ref gu);
            Rectangle destRect = Rectangle.Empty;
            Region saveRegion = g.Clip;
            switch (cellDrawOption)
                 case GridImageCellDrawOption.FitToCell:
                          destRect = clientRectangle;
                 case GridImageCellDrawOption.NoResize:
                         destRect = new Rectangle(clientRectangle.X, clientRectangle.Y, System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Fix(srcRect.Width)), System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Fix(srcRect.Height)));
                          g.Clip = new Region(clientRectangle);
                 case GridImageCellDrawOption.FitProportionally:
                          float srcRatio = (float)((int)System.Math.Floor(srcRect.Width / srcRect.Height));
                          float tarRatio = System.Convert.ToSingle(clientRectangle.Width) / clientRectangle.Height;
                          destRect = clientRectangle;
                  if (tarRatio < srcRatio)
                      destRect.Height = System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Fix(destRect.Width * srcRatio));
                        destRect.Width = System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Fix(destRect.Height * srcRatio));
                    if (! destRect.IsEmpty)
                             g.DrawImage(image, destRect, srcRect, gu);
                         g.Clip = saveRegion;




Protected Overrides Sub OnDraw(ByVal g As System.Drawing.Graphics, ByVal clientRectangle As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal colIndex As Integer, ByVal style As Syncfusion.Windows.Forms.Grid.GridStyleInfo)
        If clientRectangle.IsEmpty Then
        End If
            Dim pict() As Byte = TryCast(style.CellValue, Byte())
             If pict IsNot Nothing Then
                 Dim PictOffSet As Integer = (CType(Me.Model, GridImageCellModel)).PictureBufferOffset
                 Dim image As Image = Nothing 'Image.FromStream(buffer, true);
                  Using mStream As New MemoryStream(pict)
                      image = Image.FromStream(mStream)
                  End Using
                  Dim cellDrawOption As GridImageCellDrawOption = (CType(Me.Model, GridImageCellModel)).CellDrawOption
                   Dim gu As System.Drawing.GraphicsUnit = System.Drawing.GraphicsUnit.Point
                    Dim srcRect As RectangleF = image.GetBounds(gu)
                    Dim destRect As Rectangle = Rectangle.Empty
                    Dim saveRegion As Region = g.Clip
                    Select Case cellDrawOption
                            Case GridImageCellDrawOption.FitToCell
                                     destRect = clientRectangle
                            Case GridImageCellDrawOption.NoResize
                                     destRect = New Rectangle(clientRectangle.X, clientRectangle.Y, CInt(Fix(srcRect.Width)), CInt(Fix(srcRect.Height)))
                                     g.Clip = New Region(clientRectangle)
                             Case GridImageCellDrawOption.FitProportionally
                                      Dim srcRatio As Single = srcRect.Width \ srcRect.Height
                                      Dim tarRatio As Single = CSng(clientRectangle.Width) / clientRectangle.Height
                                      destRect = clientRectangle
                                       If tarRatio < srcRatio Then
                                           destRect.Height = CInt(Fix(destRect.Width * srcRatio))
                                           destRect.Width = CInt(Fix(destRect.Height * srcRatio))
                                        End If
                                Case Else
                        End Select
                        If Not destRect.IsEmpty Then
                            g.DrawImage(image, destRect, srcRect, gu)
                        End If
                        g.Clip = saveRegion
             End If
   End Try
End Sub





C# : FitImageincell_CS

VB : FitImageincell_VB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile