iTextSharp - 从 PictureBox 控件中获取图像并将其插入到 PDF 文件中

iTextSharp - take image from PictureBox control and insert it into a PDF file

我想从图片框控件中获取图像并将其插入到我的 PDF 文档中。我可以很好地编写 PDF 文档,并找到了有关从文件或资源中插入图像的信息,但找不到与图片框一起使用的任何内容。

下面的代码可以从文件中运行,但是我如何从图片框中获取图像?还是我需要先将图像保存为临时文件?

iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath);
image.ScalePercent(24f);
doc.Add(image);


谢谢

您可以从 MemoryStream

获取图像
    MemoryStream ms = new MemoryStream(bytes);
    Image img = Image.GetInstance(ms);

或者你可以使用字节数组

    byte[] bytes = GetImageBytesSomehow();
    Image img = Image.GetInstance(bytes);

更新(布鲁诺洛瓦吉):

我在 MSDN 上发现了以下问题:Read image from picturebox and store it in byte array

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] buff = ms.GetBuffer();

现在您可以使用 buff 作为参数创建一个 Image 实例。