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

时间:2018-08-01 14:30:05

标签: c# pdf itext pdf-generation

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

下面的代码将从文件中工作,但是如何从图片框中拾取图像?还是我需要先将图像另存为临时文件?

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


谢谢

1 个答案:

答案 0 :(得分:2)

您可以从MemoryStream

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

或者您可以使用字节数组

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

更新(Bruno Lowagie)

我在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();

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