TX Textcontrol 使用總結五——添加圖片


實現如圖所示效果:

實現代碼如下所示:

注意,此處不做代碼格式化處理...

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();
}
}
}

  

 

2.動態添加表格並為表格添加圖片

 private void 插入表格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txContent.Tables.Add(5,3,100);
        }

        private void 表格中插入圖片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath1);
            TXTextControl.Image txImage = new TXTextControl.Image(img);

            TXTextControl.Table table = txContent.Tables.GetItem(100);
            TableCell cell = table.Cells.GetItem(1, 1);//第一行第一列,需減1
            txContent.Images.Add(txImage, cell.Start-1);

            TableCell cell2 = table.Cells.GetItem(2, 1);//第二行第一列
            txContent.Images.Add(txImage, cell2.Start);//第二行第二列則為5——cell2.Start

            TableCell cell3 = table.Cells.GetItem(3, 1);//第三行第一列
            txContent.Images.Add(txImage, cell3.Start+1);
            //注意:cell.Start 表示當前單元格的位置,從第一行開始計算
            //例如,三行三列的表格,第一行第一個單元格為1,以此類推2、3.....;第二行第二列則為5
            //每行第一個單元格插入需:cell.Start-1;
            //每一行第二列單元格插入則為:cell.Start,以此類推第三列為cell.Star+1,......
        }

  

運行效果如下:

 

添加圖片、圖片下方添加文字描述如下所示:

 private void 插入圖片圖片下方添加備注ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txContent.Tables.Clear();
            this.txContent.Tables.Add(5, 3, 100);

            System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath1);
            TXTextControl.Image txImage = new TXTextControl.Image(img);
            TXTextControl.Table table = txContent.Tables.GetItem(100);
            TableCell cell = table.Cells.GetItem(1, 1);//第一行第一列,需減1
            cell.CellFormat.VerticalAlignment = VerticalAlignment.Center;
            cell.CellFormat.LeftTextDistance = 500;
            txContent.Images.Add(txImage, cell.Start - 1);

            TableCell remark1 = table.Cells.GetItem(2, 1);//第1行第2列
            remark1.Text = "添加備注1信息成功";
            remark1.CellFormat.VerticalAlignment = VerticalAlignment.Center;
            remark1.CellFormat.LeftTextDistance=500;

            TableCell cell2 = table.Cells.GetItem(1, 2);
            cell2.CellFormat.VerticalAlignment = VerticalAlignment.Center;
            cell2.CellFormat.LeftTextDistance = 500;
            txContent.Images.Add(txImage, cell2.Start-1);

            TableCell remark2 = table.Cells.GetItem(2, 2);//第2行第2列
            remark2.Text = "添加備注2信息成功";
            remark2.CellFormat.VerticalAlignment = VerticalAlignment.Center;
            remark2.CellFormat.LeftTextDistance = 500;

            //TableCell cell3 = table.Cells.GetItem(3, 1);//第三行第一列
            //txContent.Images.Add(txImage, cell3.Start + 1);
        }

運行效果如下:

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM