Articles in this section
Category / Section

How to set background image to fit proportionally in Cell in WinForms GridDataBoundGrid?

3 mins read

By default, the image will be placed in the cell based on its size in WinForms GridDataBoundGrid. 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.

C#

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;
    }
 
    try
    {
        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;
                 break;
                 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);
                 break;
                 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));
                   }
                   else
                   {
                        destRect.Width = System.Convert.ToInt32(Microsoft.VisualBasic.Conversion.Fix(destRect.Height * srcRatio));
                    }
                    break;
                    default:
                    break;
            }
 
                    if (! destRect.IsEmpty)
                    {
                             g.DrawImage(image, destRect, srcRect, gu);
                     }
 
                         g.Clip = saveRegion;
           }
        }
        catch
        {
        }
 
}
 

 

 

VB

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
            Return
        End If
 
      Try
            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))
                                       Else
                                           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
             Catch
   End Try
 
End Sub
 

 

Screenshot

C:\Users\mohanraj.gunasekaran\Pictures\syn.png

Sample

C# : FitImageincell_CS

VB : FitImageincell_VB


Conclusion

I hope you enjoyed learning about how to set background image to fit proportionally cell  in WinForms GridDataBoundGrid.

You can refer to our WinForms GridDataBound feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our  WinForms GridDataBound example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied