How to Preserve the Aspect Ratio of the Image in WinForms Grid?
Preserve the aspect ratio of the image in PdfGrid
The aspect ratio of an image can be preserved in PdfGrid by using ImagePosition property which must be set to “fit” in the PdfGrid cell.
Assemblies Required
1)Syncfusion.Compression.Base.dll
2)Syncfusion.Pdf.Base.dll
The below code snippet illustrates how to preserve the aspect ratio of the image in PDF Grid.
Code Snippet:
//Creates a new PDF document. PdfDocument doc = new PdfDocument(); //Adds a page. PdfPage page = doc.Pages.Add(); //Creates a PdfGrid. PdfGrid pdfGrid = new PdfGrid(); //Creates a DataTable. DataTable dataTable = new DataTable(); //Adds columns to the DataTable. dataTable.Columns.Add("ID"); dataTable.Columns.Add("Name"); //Adds rows to the DataTable. dataTable.Rows.Add(new object[] { " ", "Clay" }); dataTable.Rows.Add(new object[] { "E02", "Thomas" }); //Assigns data source. pdfGrid.DataSource = dataTable; //Gets the Image PdfBitmap pBmp = new PdfBitmap(@"..\..\Data\logo.png"); pdfGrid.Rows[0].Cells[0].Style.BackgroundImage = pBmp; //Fit the image inorder to preserve the aspect ratio. pdfGrid.Rows[0].Cells[0].ImagePosition = PdfGridImagePosition.Fit; // pdfGrid.Rows[0].Cells[0].ImagePosition = PdfGridImagePosition.Center; // pdfGrid.Rows[0].Cells[0].ImagePosition = PdfGridImagePosition.Stretch; // pdfGrid.Rows[0].Cells[0].ImagePosition = PdfGridImagePosition.Tile //Draws grid to the page of PDF document. pdfGrid.Draw(page, new PointF(10, 10)); //Saves the document and dispose it doc.Save("Output.pdf"); doc.Close(true);
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to Preserve the Aspect Ratio of the Image in WinForms Grid.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples 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 forums or feedback portal. We are always happy to assist you!