实现如图所示效果:
实现代码如下所示:
注意,此处不做代码格式化处理...
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using TXTextControl;
namespace InsertImage
{
public partial class Form1 : Form
{
string FilePath1 = Application.StartupPath + "\\Image\\1.png";
string FilePath2 = Application.StartupPath + "\\Image\\2.png";
string FilePath3 = Application.StartupPath + "\\Image\\3.png";
string FilePath4 = Application.StartupPath + "\\Image\\4.png";
public Form1()
{
InitializeComponent();
}
private void 插入图片ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath1);
// 完成TX中使用的度量单位缇(Twip)与.NET使用的度量单位像素(Pixel)的转换
Graphics g = txContent.CreateGraphics();
int iTwipsPerPixel = (int)(1600 / g.DpiX);
// 创建TX中的图片对象
System.Drawing.Image thumbImage = img.GetThumbnailImage(150, 150, null, System.IntPtr.Zero);
TXTextControl.Image image = new TXTextControl.Image(thumbImage);
// 设置图片ID
image.ID = 1001;
// 将图片插入到TextFrame中
txContent.Images.Add(image, txContent.InputPosition.TextPosition);
}
private void AddImage()
{
System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath1);
System.Drawing.Image img2 = System.Drawing.Image.FromFile(FilePath2);
System.Drawing.Image img3 = System.Drawing.Image.FromFile(FilePath3);
System.Drawing.Image img4 = System.Drawing.Image.FromFile(FilePath4);
List<System.Drawing.Image> list = new List<System.Drawing.Image>();
list.Add(img);
list.Add(img2);
list.Add(img3);
list.Add(img4);
//AddOneImage(list);
AddOne2Image(list,200,200);
}
private void AddOne2Image(List<System.Drawing.Image> list, int width, int height)
{
System.Drawing.Image thumbImage = list[0].GetThumbnailImage(width, height, null, System.IntPtr.Zero);
TXTextControl.Image image = new TXTextControl.Image(thumbImage);
int txWidth = txContent.Width;
txContent.Images.Add(image, txContent.InputPosition.TextPosition);
this.txContent.Select(this.txContent.Text.Length, 0);
this.txContent.Selection.Text = " ";
}
public Table AddTable(int rows, int cols, int id)
{
this.txContent.Tables.Add(rows, cols, id);
Table table = this.txContent.Tables.GetItem(id);
return table;
}
private void AddOneImage(List<System.Drawing.Image> list)
{
for (int i = 0; i < list.Count; i++)
{
System.Drawing.Image thumbImage = list[i].GetThumbnailImage(200, 200, null, System.IntPtr.Zero);
TXTextControl.Image image = new TXTextControl.Image(thumbImage);
int width = txContent.Width;
int imageW = image.Size.Width;
txContent.Images.Add(image, txContent.InputPosition.TextPosition);
this.txContent.Select(this.txContent.Text.Length, 0);
this.txContent.Selection.Text = " ";
}
}
private void 插入ToolStripMenuItem_Click(object sender, EventArgs e)
{
AddImage();
}
}
}