繁体   English   中英

MemoryStream 无法写入其他 MemoryStream

[英]MemoryStream fails to write to other MemoryStream

从一个 memory stream 写入另一个时,我遇到了问题。 我正在使用 NuGet package 将 PDF 转换为 png。 我需要将图像保存为 base64 字符串。 当我读入 pdf 时,它正确地创建了 pdf object 并具有正确的预期页数。 After I save the pdf to the memory stream, that stream has a length (presumably correct, but trying to create test validation now). 在我将 stream 发送到它应该通过复制到另一个 stream 的位置后,另一个 stream 从来没有任何数据。 我尝试了以下两种方法,一种更复杂,另一种基于我在这里找到的线程。

我无法让我的 memory 流相互写入。

这是我的 class

class pdf
{
    string localPath = @"C:\_Temp\MyForm.pdf";

    public pdf()
    {
        var base64String = GenerateSampleFormBase64(localPath);

        using(StreamWriter sw = new StreamWriter(@"C:\_Temp\log.txt"))
        {
            sw.WriteLine(base64String);
            sw.Flush();
        }

    }

    private static string GenerateSampleFormBase64(string path)
    {
        PdfDocument pdf = new PdfDocument(path);

        MemoryStream msPdf = new MemoryStream();
        pdf.Save(msPdf);

        var x = ConvertPdfPageToPng(msPdf);
        return Convert.ToBase64String(x);

    }

    static byte[] ConvertPdfPageToPng(MemoryStream msPng64)
    {
        // msPng64 length is 473923

        string base64;
        using(MemoryStream msPng = new MemoryStream(100))
        {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while((bytesRead = msPng64.Read(buffer, 0, buffer.Length)) > 0)
            {
                msPng.Write(buffer, 0, bytesRead);
            }

            // msPng is always length 0
            base64 = Convert.ToBase64String(msPng.GetBuffer(), 0, (int)msPng.Length);
            byte[] raw = Convert.FromBase64String(base64);

            if(raw.Length > 0)
                return raw;
            else
                throw new Exception("Failed to write to memory stream.");
        }
    }

    // This also did not work
    public static string ConvertToBase64(MemoryStream stream)
    {
        byte[] bytes;
        using(var memoryStream = new MemoryStream(100))
        {
            stream.CopyTo(memoryStream);
            bytes = memoryStream.ToArray();
        }

        return Convert.ToBase64String(bytes);
    }
}

您使用 PdfDocument.Save() 方法,但它保存 PDF,而不是 PNG。 您需要改用 PdfPage.Save 。 此示例代码显示如何在 Base64 中为 PDF 文档的第一页生成 PNG 图像:

using (var pdf = new PdfDocument(path))
{
    PdfDrawOptions options = PdfDrawOptions.Create();
    options.BackgroundColor = new PdfRgbColor(255, 255, 255);
    options.Compression = ImageCompressionOptions.CreatePng();

    string base64 = ConvertPdfPageToBase64Png(pdf.Pages[0], options);
    File.WriteAllText("page0_base64.txt", base64);
}

private static string ConvertPdfPageToBase64Png(PdfPage page, PdfDrawOptions options)
{
    using (var stream = new MemoryStream())
    {
        page.Save(stream, options);
        return Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM