从Stream创建新的Bitmap时,MemoryStream为null

时间:2013-08-03 05:51:13

标签: c# graphics bitmap .net-4.5

我正在尝试从流中创建一个Bitmap,其中包含在流中保存的PDF文档,但我不断获得参数null异常。 MS不是空的,它定位在0.所以我不知道还能做什么。

我正在使用Windows窗体应用程序沙箱测试功能,但我无法将内存流保存到位图。

有人能指出我哪里出错了吗?

   private async void Form1_Load(object sender, EventArgs e)
{
    //4355,4373

    IElevation elev = await ElevationManager.GetElevationAsync(4355);

    PdfSharp.Pdf.PdfDocument pdfDoc =
      await (await AlumCloudPlans.Manager.GetLabelsAsync(elev)).GetPDF(new SheetInfo(/*settings for PDF, Img printing is different*/3, 10, 240, 95, 780, 1000));

    System.IO.Stream ms = new MemoryStream();

    pdfDoc.Save(ms, false);
    ms.Position = 0;

    Bitmap bm = new Bitmap(ms); <---------(Error right here, says argument null)

    this.AutoScroll = true;
    this.pictureBox1.Image = bm;
    this.pictureBox1.BackColor = Color.White;
    this.Size = new System.Drawing.Size(bm.Width, (bm.Height + 50) / 2);
    this.pictureBox1.Size = new System.Drawing.Size(bm.Width, bm.Height + 5);
}

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

您正在尝试打开包含PDF文档的流,Bitmap构造函数正在等待图像数据。 PDFSharp只能创建PDF文档,但不能将其渲染为图像。

要将PDF文档呈现为位图,您必须使用其他库,例如Ghostscript的lib-pdfmupdf-converter或.NET包装器。