PDFsharp MigraDoc将运行时生成的位图添加到表格单元格

时间:2017-12-06 11:07:21

标签: c# bitmap pdfsharp migradoc

我想使用PDFsharp和MigraDoc生成PDF。就目前而言,一切都很完美。

现在我想出了一个在运行时创建位图的想法,并将其添加到我的一个表格单元格中。

我已经读过可以从资源中添加位图,而不必将它们放在硬盘上。

请参阅:http://www.pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx

这是我尝试将其改编为我的小项目:

创建位图的代码:

Bitmap GreenDot = new Bitmap(32,32);
Graphics GreenDotGraphics = Graphics.FromImage(GreenDot);
GreenDotGraphics.FillEllipse(Brushes.Green,0,0,32,32);

//The next step will be converting the Bitmap to an byte[]

var byteGreenDot = ImageToByte(GreenDot);

//Now converting it to string as seen in the WikiPage

string stringGreenDot = Convert.ToBase64String(byteGreenDot);
string FinalGreenDot = "base64:"+ stringGreenDot;

//Now creating a table
.
.
.
cell = MyRow.Cell[1];
cell.AddImage(FinalGreenDot);
.
.
.

将位图转换为byte []

的代码
public static byte[] ImageToByte(System.Drawing.Image img)
{
    using(var ms = new MemoryStream())
    {
        img.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
        return ms.ToArray();
    }
}

当我运行代码时,我会收到一条警告:“警告:图像'base64:Qk02E [...] =='未找到。” (此帖子的base64字符串被截断)。

我想我没有正确地将其转换为byte []。

有人能让我走上正轨吗?

1 个答案:

答案 0 :(得分:-1)

请注意,您使用的是1.50版本中引入的功能。

使用1.32或更早版本时,会收到您提及的警告。

使用1.50 beta 2或更高版本时,您的代码按预期工作。