如何将Panel的内容复制到richTextBox中?

本文关键字:复制 richTextBox Panel | 更新日期: 2023-09-27 18:10:58

我有一个应用程序的用户面板。该面板允许用户输入他们的数字签名。我想从面板中取出绘图并将其复制到richTextBox的最后。

面板的当前代码如下:

public partial class Signature : Form
{
    SolidBrush color;
    float width;
    List<List<Point>> _lines;
    Boolean _mouseDown;
    public Signature()
    {
        InitializeComponent();
        _lines = new List<List<Point>>();
        color = new SolidBrush(Color.Black);
        _mouseDown = false;
    }
    private void clear_Click(object sender, EventArgs e)
    {
        _lines.Clear();
        sign.Invalidate();
    }
    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        _mouseDown = true;
        _lines.Add(new List<Point>());
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (_mouseDown)
        {
            _lines.Last().Add(e.Location);
            sign.Invalidate();
        }
    }
    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        foreach (var lineSet in _lines)
        {
            if (lineSet.Count > 1)
            {
                e.Graphics.DrawLines(new Pen(Color.Black, 4.0F), lineSet.ToArray());
            }
        }
    }
    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        _mouseDown = false;
    }
    private void use_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Signature successfully imported!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        Basic_Word_Processor.Instance.richTextBoxPrintCtrl1.SelectedText = "";
        this.Close();
    }
}

}

我怎么能把绘图从面板和插入到richTextBox的结束?

如何将Panel的内容复制到richTextBox中?

您可以先将签名绘制到Bitmap,然后将该位图通过Clipboard复制到RichTextBox。你可以试试这个,但我必须说它没有经过测试:

Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
Rectangle rect = new Rectangle(0, 0, panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, rect);
Clipboard.SetImage(bmp);
richTextBox1.Paste();

或者,您可以将Lines绘制到Bitmap