System.ArgumentException HResult = 0x80070057 消息 = 参数无效来源 = System.Drawing为什么?

问题描述

使用 c#,我从 PE 文件提取一个游标资源并删除了 x-y 热点并预先添加了 BITMAPFILEHEADER 但是当我尝试转换为位图以在图片框中显示为 pbResImage.Image = Image.FromStream(bitmapStream.BaseStream); 我收到一个参数异常。

我的代码适用于数十个游标,并且可以使用其他程序提取有问题的游标。

我相信最简单的方法是通过代码 (bitmapBytes) 和 传递给 Image.FromStream (bitmapStream) 的流。我试了又试都没有成功。

我请求您帮助检查输入并确定 bitmapStream 的实际无效内容。一旦我知道 .NET 发现了什么问题,解决方案可能就在手边。

这是我提取的 bitmapBytes 数组:

0000- 28 00 00 00 20 00 00 00 40 00 00 00 01 00 04 00
0010- 00 00 00 00 80 02 00 00 00 00 00 00 00 00 00 00
0020- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 00
0030- 00 80 00 00 00 80 80 00 80 00 00 00 80 00 80 00
0040- 80 80 00 00 80 80 80 00 C0 c0 c0 00 00 00 FF 00
0050- 00 FF 00 00 00 FF FF 00 FF 00 00 00 FF 00 FF 00
0060- FF FF 00 00 FF FF FF 00 00 00 00 00 00 00 00 00
0070- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0080- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0090- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00a0- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00b0- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00c0- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00d0- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 88
00e0- 88 80 00 00 00 00 00 00 00 00 00 00 00 00 7F FF
00f0- FF F8 00 00 00 00 00 00 00 00 00 00 00 00 7F FF
0100- FF F8 00 00 00 00 00 00 00 00 00 00 00 07 FF FF
0110- FF FF 80 00 00 00 00 00 00 00 00 00 00 7F FF FF
0120- FF FF 80 00 00 00 00 00 00 00 00 00 07 FF FF FF
0130- FF FF F8 00 00 00 00 00 00 00 00 00 07 FF FF FF
0140- FF FF F8 00 00 00 00 00 00 00 00 00 7F FF FF FF
0150- FF FF F8 00 00 00 00 00 00 00 00 00 7F FF FF FF
0160- FF FF FF 80 00 00 00 00 00 00 00 07 FF FF FF FF
0170- FF FF FF 80 00 00 00 00 00 00 00 07 FF 88 FF FF
0180- FF FF FF 80 00 00 00 00 00 00 00 7F F8 00 FF FF
0190- FF FF FF 80 00 00 00 00 00 00 00 7F 80 07 FF FF
01a0- FF FF 7F 80 00 00 00 00 00 00 07 F8 00 07 FF 7F
01b0- F7 F8 0F 80 00 00 00 00 00 00 07 70 00 07 F8 0F
01c0- 80 F8 0F 80 00 00 00 00 00 00 00 00 00 07 F8 0F
01d0- 80 F8 07 70 00 00 00 00 00 00 00 00 00 07 F8 0F
01e0- 80 F8 00 00 00 00 00 00 00 00 00 00 00 07 F8 0F
01f0- 80 77 00 00 00 00 00 00 00 00 00 00 00 07 F8 07
0200- 70 00 00 00 00 00 00 00 00 00 00 00 00 07 F8 00
0210- 00 00 00 00 00 00 00 00 00 00 00 00 00 07 F8 00
0220- 00 00 00 00 00 00 00 00 00 00 00 00 00 07 F8 00
0230- 00 00 00 00 00 00 00 00 00 00 00 00 00 07 F8 00
0340- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 77 00
0250- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0260- 00 00 00 00 00 00 00 00 FF FF FF FF FF FF FF FF
0270- FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
0280- FF FF FF FF FF F8 1F FF FF F0 0F FF FF F0 0F FF
0290- FF E0 03 FF FF c0 03 FF FF 80 01 FF FF 80 01 FF
02a0- FF 00 01 FF FF 00 00 FF FE 00 00 FF FE 00 00 FF
02b0- Fc 00 00 FF Fc 20 00 FF F8 60 00 FF F8 E0 00 FF
02c0- FF E0 01 FF FF E0 07 FF FF E0 0F FF FF E0 7F FF
02d0- FF E1 FF FF FF E1 FF FF FF E1 FF FF FF E1 FF FF
02e0- FF F3 FF FF FF FF FF FF

这是作为“无效”参数的位图流:

0000- 42 4D F6 02 00 00 00 00 00 00 76 00 00 00 28 00
0010- 00 00 20 00 00 00 40 00 00 00 01 00 04 00 00 00
0020- 00 00 80 02 00 00 00 00 00 00 00 00 00 00 00 00
0030- 00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 80
0040- 00 00 00 80 80 00 80 00 00 00 80 00 80 00 80 80
0050- 00 00 80 80 80 00 C0 C0 C0 00 00 00 FF 00 00 FF
0060- 00 00 00 FF FF 00 FF 00 00 00 FF 00 FF 00 FF FF
0070- 00 00 FF FF FF 00 00 00 00 00 00 00 00 00 00 00
0080- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0090- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00a0- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00b0- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00c0- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00d0- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00e0- 00 00 00 00 00 00 00 00 00 00 00 00 08 88 88 80
00f0- 00 00 00 00 00 00 00 00 00 00 00 00 7F FF FF F8
0100- 00 00 00 00 00 00 00 00 00 00 00 00 7F FF FF F8
0110- 00 00 00 00 00 00 00 00 00 00 00 07 FF FF FF FF
0120- 80 00 00 00 00 00 00 00 00 00 00 7F FF FF FF FF
0130- 80 00 00 00 00 00 00 00 00 00 07 FF FF FF FF FF
0140- F8 00 00 00 00 00 00 00 00 00 07 FF FF FF FF FF
0150- F8 00 00 00 00 00 00 00 00 00 7F FF FF FF FF FF
0160- F8 00 00 00 00 00 00 00 00 00 7F FF FF FF FF FF
0170- FF 80 00 00 00 00 00 00 00 07 FF FF FF FF FF FF
0180- FF 80 00 00 00 00 00 00 00 07 FF 88 FF FF FF FF
0190- FF 80 00 00 00 00 00 00 00 7F F8 00 FF FF FF FF
01a0- FF 80 00 00 00 00 00 00 00 7F 80 07 FF FF FF FF
01b0- 7F 80 00 00 00 00 00 00 07 F8 00 07 FF 7F F7 F8
01c0- 0F 80 00 00 00 00 00 00 07 70 00 07 F8 0F 80 F8
01d0- 0F 80 00 00 00 00 00 00 00 00 00 07 F8 0F 80 F8
01e0- 07 70 00 00 00 00 00 00 00 00 00 07 F8 0F 80 F8
01f0- 00 00 00 00 00 00 00 00 00 00 00 07 F8 0F 80 77
0200- 00 00 00 00 00 00 00 00 00 00 00 07 F8 07 70 00
0210- 00 00 00 00 00 00 00 00 00 00 00 07 F8 00 00 00
0220- 00 00 00 00 00 00 00 00 00 00 00 07 F8 00 00 00
0230- 00 00 00 00 00 00 00 00 00 00 00 07 F8 00 00 00
0340- 00 00 00 00 00 00 00 00 00 00 00 07 F8 00 00 00
0250- 00 00 00 00 00 00 00 00 00 00 00 00 77 00 00 00
0260- 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0270- 00 00 00 00 00 00 FF FF FF FF FF FF FF FF FF FF
0280- FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
0290- FF FF FF F8 1F FF FF F0 0F FF FF F0 0F FF FF E0
02a0- 03 FF FF C0 03 FF FF 80 01 FF FF 80 01 FF FF 00
02b0- 01 FF FF 00 00 FF FE 00 00 FF FE 00 00 FF FC 00
02c0- 00 FF FC 20 00 FF F8 60 00 FF F8 E0 00 FF FF E0
02d0- 01 FF FF E0 07 FF FF E0 0F FF FF E0 7F FF FF E1
02e0- FF FF FF E1 FF FF FF E1 FF FF FF E1 FF FF FF F3
02f0- FF FF FF FF FF FF

相关代码如下:

    public void displayPECursor(TreeNode node)
    {
        Byte[] nodeBytes = (byte[])node.Tag;
        Byte[] bitmapBytes = new byte[nodeBytes.Length - 4];
        // Remove hotspot-x and hotspot-y values from the cursor resource data
        Buffer.Blockcopy(nodeBytes,4,bitmapBytes,nodeBytes.Count() - 4);

                   /////////////////////////////////// temp for debug
                   using (Stream file = File.OpenWrite(@"G:\___ProblemFiles\bitmapBytes.dat"))
                   {
                       file.Write(bitmapBytes,bitmapBytes.Length);
                   }
                   ///////////////////////////////////

        (int sizeBITMAPINFOHEADER,int sizeColorTable) = GetBmHeaderInfo(bitmapBytes);


        int bitmapFileLen = bitmapBytes.Length + sizeBITMAPFILEHEADER;
        using (BinaryWriter bitmapStream = new BinaryWriter(new MemoryStream(bitmapFileLen)))
        {
            // Write the missing BITMAPFILEHEADER.
            const Int16 zero = 0;
            bitmapStream.Write(bmSignature);
            bitmapStream.Write(bitmapFileLen);
            bitmapStream.Write(zero);
            bitmapStream.Write(zero);
            bitmapStream.Write(sizeBITMAPFILEHEADER + sizeBITMAPINFOHEADER + sizeColorTable);
            bitmapStream.Seek(sizeBITMAPFILEHEADER,SeekOrigin.Begin);
            bitmapStream.Write(bitmapBytes,bitmapBytes.Length);

                      /////////////////////////////////// temp for debug
                      //Save file for examination with hex editor
                      SaveStreamToFile(bitmapStream,@"G:\___ProblemFiles\My125.bmp");
                      ///////////////////////////////////

            pbResImage.Image = Image.FromStream(bitmapStream.BaseStream);
        }


            /////////////////////////////////////////////////////////////////// temp for debug
            //Internal method to save file for examination with hex editor
        void SaveStreamToFile(BinaryWriter streamName,string destFilename)
            {
                //The bmp file has been built in memory Now save it to file
                  StreamReader reader = new StreamReader(streamName.BaseStream);
                int bufferLen = 4096;
                byte[] buffer = new byte[bufferLen];
                FileStream saveStream = new FileStream(destFilename,FileMode.Create,FileAccess.ReadWrite);
                streamName.Seek(0,SeekOrigin.Begin); // stream has been prevIoUsly written so reposition to the beginning
                while (true)
                {
                    int read = streamName.BaseStream.Read(buffer,bufferLen);

                    if (read > 0)
                    {
                        saveStream.Write(buffer,read);
                    }
                    else
                        break;
                }
                saveStream.Close();
            }
            //////////////////////////////////////////////////////////////////////////
        return;
    }           
 _____________________________________________________________________________________________

    public (int sizeBITMAPINFOHEADER,int sizeColorTable) GetBmHeaderInfo(byte[] bitmapBytes)
    {
        //IntPtr offsetSizeInfoHeader = 
        //     Marshal.Offsetof(typeof(NativeMethods.BITMAPINFOHEADER),"biSize");
        //IntPtr offsetBitCount = 
        //     Marshal.Offsetof(typeof(NativeMethods.BITMAPINFOHEADER),"biBitCount");
        //IntPtr offsetClrUsed = 
        //     Marshal.Offsetof(typeof(NativeMethods.BITMAPINFOHEADER),"biClrUsed");
        //IntPtr offsetCompression = 
        //     Marshal.Offsetof(typeof(NativeMethods.BITMAPINFOHEADER),"biCompression");
        //int sizeBITMAPFILEHEADER = 
        //     System.Runtime.InteropServices.Marshal.SizeOf(typeof(NativeMethods.BITMAPFILEHEADER));
        //int sizeRGBQUAD = 
        //     System.Runtime.InteropServices.Marshal.SizeOf(typeof(NativeMethods.RGBQUAD));
        //UInt16 bmSignature;
        // the above variables were refactored to be class level; left here for Now for clarity
        // for the Stackoverflow community.

        var headerSize = new byte[2];
        var bitCount = new byte[2];
        var compression = new byte[4];
        var clrUsed = new byte[4];
        Array.copy(bitmapBytes,(int)offsetSizeInfoHeader,headerSize,2);
        Array.copy(bitmapBytes,(int)offsetBitCount,bitCount,(int)offsetCompression,compression,4);
        Array.copy(bitmapBytes,(int)offsetClrUsed,clrUsed,4);
        int sizeBITMAPINFOHEADER = BitConverter.ToInt16(headerSize,0);
        int count = BitConverter.ToInt16(bitCount,0);
        int compress = BitConverter.ToInt32(compression,0);
        int used = BitConverter.ToInt32(clrUsed,0);
        if (used == 0) used = (int)Math.Pow(2,count);  // Assume the maximum if used = 0
        // Determine the size of the color table
        int sizeColorTable = 0;
        if (compress == (int)NativeMethods.biCompression.BI_BITFIELDS)
        {
            sizeColorTable = 12;
        }
        else if (compress == (int)NativeMethods.biCompression.BI_ALPHABITFIELDS)
        {
            sizeColorTable = 16;
        }
        else if (count < 16 && used > 0)
        {
            sizeColorTable = sizeRGBQUAD * used;
        }
        return (sizeBITMAPINFOHEADER,sizeColorTable);
    }              

解决方法

有一种特殊类型的 DIB 图像用于索引光标和图标,其在标题中设置了双倍高度,但实际上在其后面包含一个 1 位 AND 掩码,用于指示哪些像素是透明的。

>

如果你分析标题,你在那里的数据似乎是一个 4 位、32x64 DIB 图像,但它实际上是一个 4 位 32x32 图像的组合,然后是一个 1-位 32x32 掩码。

通过这样考虑,可以准确地检测到这一点;如果高度可被 2 整除,则取高度的一半,然后将给定像素格式下该半高图像的预期尺寸加上每像素 1 位的该半高图像的预期尺寸(请记住,步幅始终是 4 个字节的倍数)。如果这与您获得的图像数据的大小匹配,则您正在处理透明图标格式。

要检查的这个大小值是 biSizeImage 中的 BITMAPINFOHEADER 值,您当前的代码似乎根本没有读取该字段。它是一个 32 位整数,紧跟在压缩类型字段之后。

Boolean isIcon = false;
Int32 maskSize = 0;
Int32 stride = GetClassicStride(biWidth,biBitCount);
Int32 actualHeight = biHeight;
if (biHeight % 2 == 0)
{
    Int32 halfHeight = biHeight / 2;
    Int32 maskStride = GetClassicStride(biWidth,1);
    Int32 maskSizeCheck = maskStride * halfHeight;
    if (biSizeImage = stride * halfHeight + maskSizeCheck)
    {
        isIcon = true;
        actualHeight = halfHeight;
        maskSize = maskSizeCheck;
    }
}

经典步幅公式:

public static Int32 GetClassicStride(Int32 width,Int32 bitsLength)
{
    return (((((bitsLength * width) + 7) / 8) + 3) / 4) * 4;
}

生成的图像:

Resulting image

此格式仅适用于图标文件,不适用于经典位图,这就是为什么您的 bmp 文件头不起作用的原因。因此,要么查看特定的图标/光标文件格式,要么将其转换为 32 位图像并手动将蒙版应用到图像数据上(就像我在生成上面的图像时所做的那样)。

从技术上讲,通过具有 24bpp 基础图像和 8bpp alpha 蒙版,此格式也可用于 32 位图标。在这种情况下,标题中的高度也将加倍。但到目前为止我还没有真正遇到过这种格式。

关于 Windows ICO 的维基百科文章:

https://en.wikipedia.org/wiki/ICO_(file_format)

ico 格式的规范:

https://web.archive.org/web/20160531004250/https://msdn.microsoft.com/en-us/library/ms997538.aspx