简体   繁体   English

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

[英]How to merge 2 or more byte items in array to one byte element

In start I want say, I am beginner in C# so be patient :) 首先,我想说,我是C#的初学者,所以要耐心:)
I have array of bytes. 我有字节数组。 One item represent page of pdf file. 一项代表pdf文件的页面。 I need now merge this arrays to one. 我现在需要将此数组合并为一个。 I hope this new array will show two pages one above other. 我希望这个新数组将在彼此上方显示两个页面。 In this case: 在这种情况下:
Page1 (from Items[0]) 第1页(来自项目[0])
Page2 (from Items[1.]) 第2页(来自项目[1.])

Its good idea? 它的好主意? I need this to attach pdf as images to reporting services wihtout using Database. 我需要使用数据库将pdf作为图像附加到报表服务中。


屏幕

EDIT: 编辑:

Here is my code: 这是我的代码:

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());
}

and method from PDFParser.dll: 和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;
        }
    }

3'rd party program what I used: 我使用的3'rd party程序:
http://www.tallcomponents.com/pdfrasterizer3.aspx http://www.tallcomponents.com/pdfrasterizer3.aspx

I'm afraid this just isn't going to work! 恐怕这是行不通的! The array of bytes that you see is a serialized form of the PDF object that represents each PDF page. 您看到的字节数组是代表每个PDF页面的PDF对象的序列化形式。 Merging these two together will result in an array of bytes which cannot be de-serialized into a valid PDF document object. 将这两个合并在一起将导致无法将字节序列反序列化为有效的PDF文档对象。

To achieve what you require, deserialize each byte array into a suitable PDF representation, than use a suitable API to merge the documents together. 为了实现所需的功能,请将每个字节数组反序列化为合适的PDF表示形式,然后使用合适的API将文档合并在一起。

+1 to ColinE's answer. +1以ColinE的答案。 To implement the effect you want to achieve you'll need to use some 3rd party C# PDF library. 要实现您想要实现的效果,您需要使用一些第三方C#PDF库。 You can find some links to open-source PDF libraries in answers to this SO question or there . 你可以找到一些链接在回答开源PDF库这太问题还是存在

UPDATE 更新

As for your code - to merge two images so one appears right after another you can use Graphics class like this: 至于您的代码-合并两个图像,以便一个接一个地出现,您可以使用Graphics类,如下所示:

Replace 更换

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());
}

with

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);
}

In case someone else is looking for something similar where you just want to merge the two arrays and not care about dimensions and stuff: 如果其他人正在寻找类似的东西,而您只想合并两个数组,而不关心维度和内容:

        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;
        }

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

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