如何将数组中的2个或更多字节项合并为一个字节元素

时间:2012-02-22 09:45:53

标签: c# reporting-services arraylist byte

一开始我想说,我是C#的初学者,所以请耐心等待 我有字节数组。一个项目代表pdf文件的页面。我现在需要将这个数组合并为一个。我希望这个新阵列能够显示两个页面。在这种情况下:
第1页(来自项目[0])
第2页(来自项目[1。])

好主意?我需要这个将pdf作为图像附加到报告服务而不使用数据库。

<小时/> Screen

修改

这是我的代码:

System.Collections.ArrayList items = new System.Collections.ArrayList();
System.IO.FileStream fs = new System.IO.FileStream("C://1.pdf", System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] pdf = new byte[fs.Length];
fs.Read(pdf, 0, (int)fs.Length);

PDFParser.Parse parser = new PDFParser.Parse();
System.Collections.Generic.List<System.Drawing.Image> images = parser.Split(pdf);

object dataByte = null;
for (int i = 0; i < images.Count; i++)
{
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    images[i].Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    items.Add(ms.ToArray());
}

和PDFParser.dll中的方法:

public class Parse
    {
        public List<Image> Split(byte[] document)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(document);
            Document pdfDoc = new Document(new BinaryReader(ms));
            Page page = null;
            List<Image> returnVal = new List<Image>();

            float resolution = 100;
            float scale = resolution / 72f;

            int bmpW = (int) (scale * pdfDoc.Pages[0].Width);
            int bmpH = (int) (scale * pdfDoc.Pages[0].Height);

            for (int i = 0; i < pdfDoc.Pages.Count; i++)
            {
                page = pdfDoc.Pages[i];
                using (Bitmap bitmap = new Bitmap(bmpW, bmpH))
                {
                    Graphics graphics = Graphics.FromImage(bitmap);
                    graphics.ScaleTransform(scale, scale);
                    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    page.Draw(graphics);
                    returnVal.Add((Image)bitmap.Clone());
                }
            }
            return returnVal;
        }
    }

我使用的第三方计划:
http://www.tallcomponents.com/pdfrasterizer3.aspx

3 个答案:

答案 0 :(得分:4)

我担心这不会起作用!您看到的字节数组是PDF对象的序列化形式,表示每个PDF页面。将这两者合并在一起将导致一个字节数组无法反序列化为有效的PDF文档对象。

要实现您的需求,请将每个字节数组反序列化为合适的PDF表示形式,而不是使用合适的API将文档合并在一起。

答案 1 :(得分:2)

+1给ColinE的回答。要实现您想要实现的效果,您需要使用一些第三方C#PDF库。您可以在this SO questionthere的答案中找到一些开源PDF库的链接。

<强>更新

至于你的代码 - 要合并两个图像,以便一个接一个地出现,你可以像这样使用Graphics类:

替换

object dataByte = null;
for (int i = 0; i < images.Count; i++)
{
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    images[i].Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    items.Add(ms.ToArray());
}

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

if (images.Count > 0)
{
    int totalHeight = 0;
    int maxWidth = 0;
    for (int i = 0; i< images.count; i++)
    {
        totalHeight += images[i].Height;
        if (images[i].Width > maxWidth)
        {
            maxWidth = images[i].Width;
        }
    } 
    Image mergedImage = new System.Drawing.Bitmap(maxWidth, totalHeight);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(mergedImage);
    int heightOffset = 0;
    for (int i = 0; i< images.Count; i++)
    {
        g.DrawImage(images[i],new Point(0, heightOffset));
        heightOffset += images[i].Height;
    }
    g.Dispose(); // Mandatory! Graphics is using unmanaged resources so it must be disposed explicitly.
    mergedImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
}

答案 2 :(得分:0)

如果其他人正在寻找类似的东西,你只想合并两个阵列而不关心尺寸和东西:

        private void LoadImage()
        {
            string src = string.empty;
            byte[] mergedImageData = new byte[0];

            mergedImageData = MergeTwoImageByteArrays(watermarkByteArray, backgroundImageByteArray);
            src = "data:image/png;base64," + Convert.ToBase64String(mergedImageData);
            MyImage.ImageUrl = src;
        }

        private byte[] MergeTwoImageByteArrays(byte[] imageBytes, byte[] imageBaseBytes)
        {
            byte[] mergedImageData = new byte[0];
            using (var msBase = new MemoryStream(imageBaseBytes))
            {
                System.Drawing.Image imgBase = System.Drawing.Image.FromStream(msBase);
                Graphics gBase = Graphics.FromImage(imgBase);
                using (var msInfo = new MemoryStream(imageBytes))
                {
                    System.Drawing.Image imgInfo = System.Drawing.Image.FromStream(msInfo);
                    Graphics gInfo = Graphics.FromImage(imgInfo);
                    gBase.DrawImage(imgInfo, new Point(0, 0));
                    //imgBase.Save(Server.MapPath("_____testImg.png"), ImageFormat.Png);
                    MemoryStream mergedImageStream = new MemoryStream();
                    imgBase.Save(mergedImageStream, ImageFormat.Png);
                    mergedImageData = mergedImageStream.ToArray();
                    mergedImageStream.Close();
                }
            }
            return mergedImageData;
        }