Пример #1
0
        public static vector[,] readFromBMPFile(string FilePath)
        {
            BMP24_FILE_HEADER fileHeader = new BMP24_FILE_HEADER();

            BMP24_INFO_HEADER infoHeader = new BMP24_INFO_HEADER();

            try
            {
                using (BinaryReader binReader = new BinaryReader(File.Open(FilePath, FileMode.Open)))
                {
                    fileHeader.magic = binReader.ReadUInt16();
                    fileHeader.fileSize = binReader.ReadUInt32();
                    fileHeader.reserved1 = binReader.ReadUInt16();
                    fileHeader.reserved2 = binReader.ReadUInt16();
                    fileHeader.arrayOffset = binReader.ReadUInt32();

                    infoHeader.infoHeaderSize = binReader.ReadUInt32();
                    infoHeader.arrayWidth = binReader.ReadUInt32();
                    infoHeader.arrayHeight = binReader.ReadUInt32();
                    infoHeader.colorPlanes = binReader.ReadUInt16();
                    infoHeader.bitsPerPixel = binReader.ReadUInt16();
                    infoHeader.compressionType = binReader.ReadUInt32();
                    infoHeader.arraySize = binReader.ReadUInt32();
                    infoHeader.xResolution = binReader.ReadUInt32();
                    infoHeader.yResolution = binReader.ReadUInt32();
                    infoHeader.colorsInPalette = binReader.ReadUInt32();
                    infoHeader.importantColors = binReader.ReadUInt32();

                    vector[,] array = new vector[infoHeader.arrayWidth, infoHeader.arrayHeight];

                    for (long row = infoHeader.arrayHeight - 1; row >= 0; row--)
                    {

                        for (long column = infoHeader.arrayWidth - 1; column >= 0; column--)
                        {

                        }

                        binReader.ReadBytes((int)(4 - (infoHeader.arrayWidth * 3) % 4) % 4));
                    }
                }
            }

            catch
            {

            }
        }
Пример #2
0
        /// <summary>
        /// write an array of RGB values into a Bitmap file
        /// </summary>
        /// <param name="FilePath">Path where the file should be created</param>
        /// <param name="image">RGB array to be stored</param>
        public static void writeToBMPFile(string FilePath, vector[,] image)
        {
            // Create a bitmap file header
            BMP24_FILE_HEADER fileHeader = new BMP24_FILE_HEADER();

            // Initialize with common values
            fileHeader.magic = 0x4D42;
            fileHeader.fileSize = 54 + ((uint)image.GetLength(0) * 3 + ((4 - ((uint)image.GetLength(0) * 3) % 4) % 4)) * (uint)image.GetLength(1);
            fileHeader.reserved1 = 0;
            fileHeader.reserved2 = 0;
            fileHeader.arrayOffset = 0x36;

            // Create a bitmap info header
            BMP24_INFO_HEADER infoHeader = new BMP24_INFO_HEADER();

            // Initialize with common values
            infoHeader.infoHeaderSize = 0x28;
            infoHeader.arrayWidth = (uint)image.GetLength(0);
            infoHeader.arrayHeight = (uint)image.GetLength(1);
            infoHeader.colorPlanes = 0x01;
            infoHeader.bitsPerPixel = 0x18;
            infoHeader.compressionType = 0;
            infoHeader.arraySize = (uint)(image.GetLength(0) * 3 + ((4 - ((uint)image.GetLength(0) * 3) % 4) % 4)) * (uint)image.GetLength(1);
            infoHeader.xResolution = 0x0B13;
            infoHeader.yResolution = 0x0B13;
            infoHeader.colorsInPalette = 0;
            infoHeader.importantColors = 0;

            // Write the data to the file
            try
            {
                using (BinaryWriter binWriter = new BinaryWriter(File.Open(FilePath, FileMode.CreateNew))) // Create new file
                {
                    binWriter.Write(fileHeader.magic);
                    binWriter.Write(fileHeader.fileSize);
                    binWriter.Write(fileHeader.reserved1);
                    binWriter.Write(fileHeader.reserved2);
                    binWriter.Write(fileHeader.arrayOffset);

                    binWriter.Write(infoHeader.infoHeaderSize);
                    binWriter.Write(infoHeader.arrayWidth);
                    binWriter.Write(infoHeader.arrayHeight);
                    binWriter.Write(infoHeader.colorPlanes);
                    binWriter.Write(infoHeader.bitsPerPixel);
                    binWriter.Write(infoHeader.compressionType);
                    binWriter.Write(infoHeader.arraySize);
                    binWriter.Write(infoHeader.xResolution);
                    binWriter.Write(infoHeader.yResolution);
                    binWriter.Write(infoHeader.colorsInPalette);
                    binWriter.Write(infoHeader.importantColors);

                    // Write the content of the array to the file in the right order
                    for (long row = infoHeader.arrayHeight - 1; row >= 0; row--)
                    {
                        for (long column = infoHeader.arrayWidth - 1; column >= 0; column--)
                        {
                            binWriter.Write((byte)image[column, row].z);
                            binWriter.Write((byte)image[column, row].y);
                            binWriter.Write((byte)image[column, row].x);
                        }
                        // Pad rows according to the file format
                        for (long padCount = (4 - (infoHeader.arrayWidth * 3) % 4) % 4; padCount > 0; padCount--)
                        {
                            binWriter.Write((byte)0);
                        }
                    }
                }
            }
            // Catch exception if file allready exists
            catch (IOException)
            {
                // Replace generic exception with more specific ones
                throw new FileAlreadyExistsException("There already exists a file with this name at the specified path");
            }

            return;
        }