コード例 #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected java.awt.image.System.Drawing.Bitmap readBmp(java.io.File imageFile) throws java.io.IOException
        protected internal virtual System.Drawing.Bitmap readBmp(File imageFile)
        {
            System.IO.Stream @is = new BufferedInputStream(new System.IO.FileStream(imageFile, System.IO.FileMode.Open, System.IO.FileAccess.Read));

            // Reading file header
            int magic    = readLittleEndianShort(@is);
            int fileSize = readLittleEndianInt(@is);

            @is.skip(4);
            int dataOffset = readLittleEndianInt(@is);

            // Reading DIB header
            int dibHeaderLength = readLittleEndianInt(@is);
            int imageWidth      = readLittleEndianInt(@is);
            int imageHeight     = readLittleEndianInt(@is);
            int numberPlanes    = readLittleEndianShort(@is);
            int bitsPerPixel    = readLittleEndianShort(@is);

            // Skip rest of DIB header until data start
            @is.skip(dataOffset - 14 - 16);

            System.Drawing.Bitmap img = null;
            if (magic == (('M' << 8) | 'B') && dibHeaderLength >= 16 && fileSize >= dataOffset && numberPlanes == 1 && bitsPerPixel == 32)
            {
                img = new System.Drawing.Bitmap(imageWidth, imageHeight, System.Drawing.Bitmap.TYPE_INT_ARGB);
                for (int y = imageHeight - 1; y >= 0; y--)
                {
                    for (int x = 0; x < imageWidth; x++)
                    {
                        int argb = readLittleEndianInt(@is);
                        img.setRGB(x, y, argb);
                    }
                }
            }

            @is.Close();

            return(img);
        }