字节数组末尾的大量0值

时间:2016-03-30 20:57:54

标签: c# libtiff.net

我正在使用BitMiracle的LibTiff.Net来读取位图图像并返回一个TIFF字节[],它作为Base64String嵌入到文件中。我注意到Base64字符串的结尾比我预期的要长很多,其尾端是大量的'A'字符。在调试时,我看到LibTiff返回给我的byte []最后有几千个值,这些值似乎不是图像本身的必要部分(据我所知)。

我在这里使用BitMiracle的示例代码转换:https://bitmiracle.github.io/libtiff.net/html/075f57db-d779-48f7-9fd7-4ca075a01599.htm

尽管如此,我还是看不出会在byte []末尾导致“垃圾”的原因。有什么想法吗?

编辑以添加代码 - GetTiffImageBytes()位于上面的链接中:

public void GenImage()
      using (System.Drawing.Image frontImage = System.Drawing.Image.FromStream(file))//;
            {
                file.Close();

                //Draw something
                b = new Bitmap(frontImage);
                Graphics graphics = Graphics.FromImage(b);
                graphics.DrawString(data1, (Font)GlobalDict.FontDict["font1"], Brushes.Black, 200, 490);
                graphics.DrawString(data2, (Font)GlobalDict.FontDict["font2"], Brushes.Black, 680, 400);

            }
            //Convert to TIF - requires BitMiracle.LibTiff.Classic
            byte[] tiffBytes = GetTiffImageBytes(b, false);

            return tiffBytes;
            }

以上称为:

  byte[] aFrontImage = MiscTools.GenImage(somestuff);

  fileXML.WriteLine("    <FrontImage>" + System.Convert.ToBase64String(aFrontImage, 0, aFrontImage.Length) + "</FrontImage>");

所有事情都说完了,它运行正常,我们的应用程序可以读取生成的图像。我只是想减小尺寸,因为其中一些文件可能有成千上万的图像。我有一些较旧的样本文件是手工创建的,其中一些Base64字符串是通过另一种大小相同的字符串的方法创建的,保存了我认为是垃圾的所有拖尾字节。

正如有人评论的那样,一个选项可能只是读取byte []并在转换之前从末尾删除所有0值,但我试图弄清楚为什么会发生这种情况。

谢谢!

2 个答案:

答案 0 :(得分:2)

问题很可能出现在linked source example

return ms.GetBuffer();

对于MemoryStream,这将返回整个底层数组,即使您尚未实际使用该数组。如果你写的足够填充它,这个缓冲区将被调整为更大的缓冲区,但是它不会扩展到只覆盖所需的大小,它每次都会增长到之前大小的两倍。另外,你有一个Length属性,它可以指示实际使用了多少这个数组。

这类似于List<T>的容量,每次填充当前容量时,容量也会增加一倍。 Count属性将指示您在列表中实际拥有的项目数。

修复很简单,用以下代码替换上面的代码:

return ms.ToArray();

这将创建一个新数组,其大小足以包含实际写入内存流的字节,并将缓冲区的内容(适合和计数的部分)复制到其中。

要验证缓冲区是否大于需要,您可以运行以下简单代码:

var ms = new MemoryStream();
Console.WriteLine("GetBuffer: " + ms.GetBuffer().Length);
Console.WriteLine("ToArray: " + ms.ToArray().Length);
ms.WriteByte(0);
Console.WriteLine("GetBuffer: " + ms.GetBuffer().Length);
Console.WriteLine("ToArray: " + ms.ToArray().Length);

这将输出:

GetBuffer: 0
ToArray: 0
GetBuffer: 256
ToArray: 1

正如您所看到的,当向其写入1个字节时,初始缓冲区大小增加到256个字节。在此之后,每次达到当前大小时它将加倍。

.NET Fiddle here

答案 1 :(得分:0)

现在,我只是在事后“修复”了这个问题并创建了一个我调用每个图像的方法:

        private static byte[] fixImageByteArray(byte[] inByte)  // Fix issue with garbage suffix data - reduces image byte[] size by roughly half.
    {
        int newByteBaseLength = inByte.Length - 1;  
        while (inByte[newByteBaseLength] == 0)
        {
            --newByteBaseLength;
        }

        float newByteModifiedLength = ((inByte.Length - newByteBaseLength) * 0.1f) + 0.5f;  // When using newByteBaseLength + 1, some TIFF Tag data was getting lost.  This seems to resolve the issue.

        int newByteModifiedLengthAsInt = (int)newByteModifiedLength;

        byte[] outByte = new byte[newByteBaseLength + newByteModifiedLengthAsInt];
        Array.Copy(inByte, outByte, newByteBaseLength + newByteModifiedLengthAsInt);

        return outByte;
    }

编辑:我修改了变量名称以使其更有意义。我发现旧方法(使用newByteBaseLength + 1)来调整数组的大小会对TIFF标签造成一些损害。通过使用效率稍低的方法,图像大小仍然显着减少,但标签保持不变。