开始使用Reports for WinForms > 打印和预览功能入门 > 制作一个简单的表格 > 创建一个三行三列的表格 > 向表格的特定单元格中添加两幅图片 |
本文展示了如何利用RenderImage 类向当前表格的特定单元格中添加两幅不同的图片。同时也展示了在单元格中如何使用ImageAlignHorzEnum来排列图片。注意,下面的范例使用了在“创建一个三行三列的表格”章节中创建的3x3尺寸的空表格,此外你还需要准备两幅GIF或JPEG格式的图片来完成本文中的各个步骤。完成如下步骤:
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Make a table. Dim table As C1.C1Preview.RenderTable = New C1.C1Preview.RenderTable(Me.C1PrintDocument1) table.Style.GridLines.All = New C1.C1Preview.LineDef(Color.DarkGray) Dim r As Integer = 3 Dim c As Integer = 3 Dim row As Integer Dim col As Integer For row = 0 To r - 1 Step +1 For col = 0 To c - 1 Step +1 Dim celltext As C1.C1Preview.RenderText = New C1.C1Preview.RenderText(Me.C1PrintDocument1) ' Add empty cells. celltext.Text = String.Format("", row, col) table.Cells(row, col).RenderObject = celltext Next Next ' Generate the document. Me.C1PrintDocument1.Body.Children.Add(table) Me.C1PrintDocument1.Generate() End Sub |
C#
C# |
拷贝代码
|
---|---|
private void Form1_Load(object sender, System.EventArgs e) { // Make a table. C1.C1Preview.RenderTable table = new C1.C1Preview.RenderTable(this.c1PrintDocument1); table.Style.GridLines.All = new C1.C1Preview.LineDef(Color.DarkGray); const int r = 3; const int c = 3; for (int row = 0; row < r; ++row) { for (int col = 0; col < c; ++col) { C1.C1Preview.RenderText celltext = new C1.C1Preview.RenderText(this.c1PrintDocument1); celltext.Text = string.Format("", row, col); // Add empty cells. table.Cells[row, col].RenderObject = celltext; } } // Generate the document. this.c1PrintDocument1.Body.Children.Add(table); this.c1PrintDocument1.Generate(); } |
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
' Fix the center cell's size. table.Rows(1).Height = New C1.C1Preview.Unit(5, C1.C1Preview.UnitTypeEnum.Cm) table.Cols(1).Width = New C1.C1Preview.Unit(8, C1.C1Preview.UnitTypeEnum.Cm) |
C#
C# |
拷贝代码
|
---|---|
// Fix the center cell's size. table.Rows[1].Height = new C1.C1Preview.Unit(5, C1.C1Preview.UnitTypeEnum.Cm); table.Cols[1].Width = new C1.C1Preview.Unit(8, C1.C1Preview.UnitTypeEnum.Cm); |
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
' Set up image alignment. table.CellStyle.ImageAlign.StretchHorz = False table.CellStyle.ImageAlign.StretchVert = False table.CellStyle.ImageAlign.AlignHorz = C1.C1Preview.ImageAlignHorzEnum.Center |
C#
C# |
拷贝代码
|
---|---|
// Set up image alignment. table.CellStyle.ImageAlign.StretchHorz = false; table.CellStyle.ImageAlign.StretchVert = false; table.CellStyle.ImageAlign.AlignHorz = C1.C1Preview.ImageAlignHorzEnum.Center; |
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Dim img1 As C1.C1Preview.RenderImage = New C1.C1Preview.RenderImage(Me.C1PrintDocument1) Dim img2 As C1.C1Preview.RenderImage = New C1.C1Preview.RenderImage(Me.C1PrintDocument1) |
C#
C# |
拷贝代码
|
---|---|
C1.C1Preview.RenderImage img1 = new C1.C1Preview.RenderImage(this.c1PrintDocument1); C1.C1Preview.RenderImage img2 = new C1.C1Preview.RenderImage(this.c1PrintDocument1); |
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
img1.Image = Me.PictureBox1.Image img2.Image = Me.PictureBox2.Image |
C#
C# |
拷贝代码
|
---|---|
img1.Image = this.pictureBox1.Image; img2.Image = this.pictureBox2.Image; |
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
table.Cells(1, 1).RenderObject = img1 table.Cells(1, 2).RenderObject = img2 |
C#
C# |
拷贝代码
|
---|---|
table.Cells[1, 1].RenderObject = img1; table.Cells[1, 2].RenderObject = img2; |
注意:表格中左上角单元格的行列号都是0 |
你的表格应该看起来你下面的表格相似