捕获 TableLayoutPanel 特定区域的图像

时间:2021-07-03 10:51:38

标签: c# winforms graphics tablelayoutpanel image-capture

我有一个使用 TableLayoutPanel 控件的项目。
我想捕获该 TableLayoutPanel 的特定区域的图像。
我有一个图像(已捕获)宽度和高度信息。我也有开始结束单元格索引信息,我成功捕获但捕获的区域是错误的。

enter image description here

这是我的代码:

private void getImage()
{
    int totalWidth = 250 // image width size;
    int totalHeight = 200 // image height size;

    int LeftBottomCellColumnIndex = 3
    int LeftBottomCellRowIndex = 4

    int LeftTopCellColumnIndex = 3
    int LeftBottomCellRowIndex = 2 // I also have all cells index info

    Bitmap bm = new Bitmap(totalWidth, totalHeight);
    tableLayoutPanel2.DrawToBitmap(bm, new Rectangle(0, 0, totalWidth, totalHeight)); // 0,0 problem is here. I do not know what I have to put there

    string path = @"C:\Users\prdn5\TestDrawToBitmap.bmp";
    FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
    bm.Save(fs, ImageFormat.Bmp);
    fs.Flush();
    fs.Close();
}

1 个答案:

答案 0 :(得分:2)

出于某种原因,TableLayoutPanel 不会通过属性或方法直接公开单元格边界。但当然它会计算这些值供内部使用,因为它需要绘制其单元格的边界。

它的 OnPaintBackground 方法对此负责。
当 Control 需要重新绘制自身时,会重新计算 Cells 边界,并且对于每个 Cell,Control 会引发 CellPaint 事件。它的 TableLayoutCellPaintEventArgs 参数返回当前单元格的边界和列/行坐标。
由于每次修改/调整 TableLayoutPanel 大小时都会引发此事件,因此我们可以使用 CellPaint 事件处理程序来存储这些引用。

在这里,我使用 Dictionary<TableLayoutPanelCellPosition, Rectangle> 来存储每个 Cell 的坐标并将其映射到其边界。

Layout 事件处理程序设置一个布尔字段,该字段会导致仅在必要时重建字典(因为 TableLayoutPanel 的布局已更改)。

▶ 字典键是 TableLayoutPanelCellPosition,因为它允许快速简单的相等比较。当然,如果需要,您可以使用不同类型的对象。

▶ 假设 TableLayoutPanel 名为 tlp1

▶ 不处理在运行时添加和/或删除列/行。如果需要,请在 Layout 事件处理程序中重新定义 Dictionary 而不是 Form Constructor。

public partial class SomeForm : Form
{
    Dictionary<TableLayoutPanelCellPosition, Rectangle> tlpCells = null;
    bool calcCells = false;

    public SomeForm() {
        InitializeComponent();

        // Set the DoubleBuffered property via reflection (if needed)
        var flags = BindingFlags.Instance | BindingFlags.NonPublic;
        tlp1.GetType().GetProperty("DoubleBuffered", flags).SetValue(tlp1, true);

        tlpCells = new Dictionary<TableLayoutPanelCellPosition, Rectangle>();
        for (int x = 0; x < tlp1.ColumnCount; x++) {
            for (int y = 0; y < tlp1.RowCount; y++) {
                tlpCells.Add(new TableLayoutPanelCellPosition(x, y), Rectangle.Empty);
            }
        }
    }

    private void tlp1_Layout(object sender, LayoutEventArgs e) => calcCells = true;

    private void tlp1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
    {
        if (calcCells) {
            var cellPos = new TableLayoutPanelCellPosition(e.Column, e.Row);
            tlpCells[cellPos] = e.CellBounds;

            if (cellPos.Column == tlp1.ColumnCount - 1 && 
                cellPos .Row == tlp1.RowCount - 1) calcCells = false;
        }
    }
}

要从当前鼠标位置获取单元格,请调用 GetSelectedCell(),传递相对于 TableLayoutPanel 的当前指针位置:

var cell = GetSelectedCell(tlp1.PointToClient(MousePosition));
// If called from the MouseMove, MouseDown etc handlers, use the MouseEventArgs instead
var cell = GetSelectedCell(e.Location);

// [...]

private TableLayoutPanelCellPosition GetSelectedCell(Point position) 
    => tlpCells.FirstOrDefault(c => c.Value.Contains(position)).Key;

现在,要从一系列单元格生成位图,您只需指定起始(左、上)和结束(右、下)单元格并将这些值引用传递给 TlpCellRangeToBitmap() 方法(它也可以用作扩展方法)。
如果 includeBorders 参数设置为 true,它将包括单元格的外部边框(请参阅视觉示例)。添加用于检查 TableLayoutPanel 是否实际具有边框的代码(BorderStyle 属性)。
GetCellRangeBounds() 返回包含 Cell 范围的 Rectangle:此方法是独立的,可用于其他目的。

比如设置一个PictureBox的Image属性为生成的Bitmap:

var cellStart = new TableLayoutPanelCellPosition(2, 2);
var cellEnd = new TableLayoutPanelCellPosition(3, 5);

somePictureBox.Image?.Dispose();
somePictureBox.Image = TlpCellRangeToBitmap(tlp1, cellStart, cellEnd, false);

// [...]

private Bitmap TlpCellRangeToBitmap(TableLayoutPanel tlp, TableLayoutPanelCellPosition cellStart, TableLayoutPanelCellPosition cellEnd, bool includeBorders)
{
    // The 3rd parameter includes or excludes the external borders
    var selRect = GetCellRangeBounds(cellStart, cellEnd, includeBorders);
    using (var image = new Bitmap(tlp.Width + 1, tlp.Height + 1)) {
        tlp.DrawToBitmap(image, new Rectangle(Point.Empty, tlp.Size));
        return image.Clone(selRect, PixelFormat.Format32bppArgb);
    }
}

private Rectangle GetCellRangeBounds(TableLayoutPanelCellPosition start, TableLayoutPanelCellPosition end, bool extBorders)
{
    var cellStart = tlpCells[start];
    var cellEnd = tlpCells[end];
    if (extBorders) {
        cellStart.Location = new Point(cellStart.X - 1, cellStart.Y - 1);
        cellEnd.Size = new Size(cellEnd.Width + 2, cellEnd.Height + 2);
    }
    return new Rectangle(cellStart.Location, new Size(cellEnd.Right - cellStart.X, cellEnd.Bottom - cellStart.Y));
}

这是它的工作原理:

TableLayoutPanel Cell Ranges to Bitmap

相关问题