コード例 #1
0
ファイル: BitmapHeader.cs プロジェクト: erisonliang/BigBitmap
        public static BitmapHeader GetHeader(MemoryMappedViewAccessor Reader, out long MMPosition)
        {
            MMPosition = 0;

            var header = new BitmapHeader();

            header.Type = Encoding.ASCII.GetString(new byte[] { Reader.ReadByte(MMPosition) });
            MMPosition++;

            header.Type += Encoding.ASCII.GetString(new byte[] { Reader.ReadByte(MMPosition) });
            MMPosition++;

            header.FileSize = Reader.ReadInt32(MMPosition);
            MMPosition     += 4;

            header.Reserved = Reader.ReadInt32(MMPosition);
            MMPosition     += 4;

            header.Offset = Reader.ReadInt32(MMPosition);
            MMPosition   += 4;

            header.BitmapInfoHeaderSize = Reader.ReadInt32(MMPosition);
            MMPosition += 4;

            return(header);
        }
コード例 #2
0
ファイル: BigBitmap.cs プロジェクト: erisonliang/BigBitmap
        public static BigBitmap CreateBigBitmapFromBitmap(Stream Stream, string BigBitmapFilePath)
        {
            using (var Reader = new StreamReader(Stream))
            {
                using (var BigBitmapFile = new FileStream(BigBitmapFilePath, FileMode.Create, FileAccess.ReadWrite))
                {
                    byte[] data = new byte[40];

                    Stream.Read(data, 0, 18);
                    var header = BitmapHeader.GetHeader(data);

                    Stream.Read(data, 0, 36);
                    var infoHeader = BitmapInfoHeader.GetInfoHeader(data);

                    if (infoHeader.BitsPerPixel != 24)
                    {
                        throw new InvalidOperationException("Only 24bpp Bitmaps Supported");
                    }

                    var bigBitmap = new BigBitmap();

                    bigBitmap.FileHeader          = new BigBitmapHeader();
                    bigBitmap.FileHeader.FileSize = header.FileSize;

                    bigBitmap.FileInfoHeader = new BigBitmapInfoHeader();
                    bigBitmap.FileInfoHeader.BitmapHeight         = infoHeader.BitmapHeight;
                    bigBitmap.FileInfoHeader.BitmapWidth          = infoHeader.BitmapWidth;
                    bigBitmap.FileInfoHeader.BitsPerPixel         = infoHeader.BitsPerPixel;
                    bigBitmap.FileInfoHeader.ColorDataSize        = infoHeader.ColorDataSize;
                    bigBitmap.FileInfoHeader.ColorPlanes          = infoHeader.ColorPlanes;
                    bigBitmap.FileInfoHeader.Colors               = infoHeader.Colors;
                    bigBitmap.FileInfoHeader.CompressinoMethod    = infoHeader.CompressinoMethod;
                    bigBitmap.FileInfoHeader.HorizantalResolution = infoHeader.HorizantalResolution;
                    bigBitmap.FileInfoHeader.IColors              = infoHeader.IColors;
                    bigBitmap.FileInfoHeader.VerticalResolution   = infoHeader.VerticalResolution;

                    data = bigBitmap.FileHeader.CreateBigBitmapHeader();
                    BigBitmapFile.Write(data, 0, data.Length);
                    data = bigBitmap.FileInfoHeader.CreateInfoHeaderData(infoHeader.ColorDataSize);
                    BigBitmapFile.Write(data, 0, data.Length);

                    int bytesRead = -1;
                    while ((bytesRead = Stream.Read(data, 0, data.Length)) != 0)
                    {
                        BigBitmapFile.Write(data, 0, bytesRead);
                    }

                    BigBitmapFile.Flush();
                    BigBitmapFile.Close();

                    return(new BigBitmap(BigBitmapFilePath));
                }
            }
        }
コード例 #3
0
ファイル: BitmapHeader.cs プロジェクト: erisonliang/BigBitmap
        public static BitmapHeader GetHeader(byte[] data)
        {
            var header = new BitmapHeader();

            using (var s = new MemoryStream(data))
                using (var reader = new BinaryReader(s))
                {
                    header.Type = Encoding.ASCII.GetString(data, 0, 2);
                    s.Seek(2, SeekOrigin.Begin);
                    header.FileSize             = reader.ReadInt32();
                    header.Reserved             = reader.ReadInt32();
                    header.Offset               = reader.ReadInt32();
                    header.BitmapInfoHeaderSize = reader.ReadInt32();
                }
            return(header);
        }
コード例 #4
0
ファイル: BigBitmap.cs プロジェクト: erisonliang/BigBitmap
        public Bitmap ConvertToBitmap()
        {
            if (FileHeader.FileSize > int.MaxValue - 70)
            {
                throw new InvalidOperationException("File Size is too big, Try using \"Chunk\" method to get smaller Picture Size");
            }

            if (FileInfoHeader.BitmapHeight > int.MaxValue || FileInfoHeader.BitmapWidth > int.MaxValue || FileInfoHeader.ColorDataSize > int.MaxValue)
            {
                throw new InvalidOperationException("Color Data (Height or Width) is(are) too high, Cant fit one (or more) in \"Int\" DataType");
            }

            Bitmap map = null;

            using (var ms = new MemoryStream())
            {
                var header = new BitmapHeader();
                header.FileSize = (int)FileHeader.FileSize - 16;

                var headerInfo = new BitmapInfoHeader();
                headerInfo.BitmapHeight = (int)FileInfoHeader.BitmapHeight;
                headerInfo.BitmapWidth  = (int)FileInfoHeader.BitmapWidth;

                headerInfo.BitsPerPixel         = FileInfoHeader.BitsPerPixel;
                headerInfo.ColorPlanes          = FileInfoHeader.ColorPlanes;
                headerInfo.HorizantalResolution = FileInfoHeader.HorizantalResolution;
                headerInfo.VerticalResolution   = FileInfoHeader.VerticalResolution;

                var data = header.CreateBitmapHeader();
                ms.Write(data, 0, data.Length);

                data = headerInfo.CreateInfoHeaderData((int)FileInfoHeader.ColorDataSize);
                ms.Write(data, 0, data.Length);
                data = null;

                for (int i = 0; i < header.FileSize - 54; i++) // 54 = 70 - 16
                {
                    SeektoPage(i + 70);
                    ms.WriteByte(Reader.ReadByte(((i + 70) % MemoryPageSize)));
                }
                map = new Bitmap(ms);
            }

            return(map);
        }
コード例 #5
0
ファイル: BigBitmap.cs プロジェクト: erisonliang/BigBitmap
        public void ConvertAndSaveToBitmap(string Path)
        {
            using (var file = new FileStream(Path, FileMode.Create, FileAccess.ReadWrite))
            {
                if (FileHeader.FileSize > int.MaxValue - 70)
                {
                    throw new InvalidOperationException("File Size is too big, Try using \"Chunk\" method to get smaller Picture Size");
                }

                if (FileInfoHeader.BitmapHeight > int.MaxValue || FileInfoHeader.BitmapWidth > int.MaxValue || FileInfoHeader.ColorDataSize > int.MaxValue)
                {
                    throw new InvalidOperationException("Color Data (Height or Width) is(are) too high, Cant fit one field (or more) in \"Int\" DataType");
                }

                var header = new BitmapHeader();
                header.FileSize = (int)FileHeader.FileSize - 16; // 70-54=16 is difference between our new "bigbitmap" header and original bitmapheader

                var headerInfo = new BitmapInfoHeader();
                headerInfo.BitmapHeight = (int)FileInfoHeader.BitmapHeight;
                headerInfo.BitmapWidth  = (int)FileInfoHeader.BitmapWidth;

                headerInfo.BitsPerPixel         = FileInfoHeader.BitsPerPixel;
                headerInfo.ColorPlanes          = FileInfoHeader.ColorPlanes;
                headerInfo.HorizantalResolution = FileInfoHeader.HorizantalResolution;
                headerInfo.VerticalResolution   = FileInfoHeader.VerticalResolution;

                var data = header.CreateBitmapHeader();
                file.Write(data, 0, data.Length);
                data = headerInfo.CreateInfoHeaderData((int)FileInfoHeader.ColorDataSize);
                file.Write(data, 0, data.Length);

                for (int i = 0; i < header.FileSize - 54; i++) // 54 = 70 - 16
                {
                    SeektoPage(i + 70);
                    file.WriteByte(Reader.ReadByte(((i + 70) % MemoryPageSize)));
                }

                file.Flush();
                file.Close();
            }
        }
コード例 #6
0
ファイル: BigBitmap.cs プロジェクト: erisonliang/BigBitmap
        public Bitmap GetChunk(long width, long height, long x, long y)
        {
            if (width * height * 3 >= int.MaxValue)
            {
                throw new InvalidOperationException("Chunk Size is too big");
            }

            if (width + x > FileInfoHeader.BitmapWidth)
            {
                width = FileInfoHeader.BitmapWidth - x;
            }
            if (height + y > FileInfoHeader.BitmapHeight)
            {
                height = FileInfoHeader.BitmapHeight;
            }

            var newRowSize = (int)Math.Floor(((double)FileInfoHeader.BitsPerPixel * width + 31) / 32) * 4;
            var newPadding = (newRowSize - width * 3);
            var Data2Read  = (int)(newRowSize - newPadding);

            IEnumerable <byte> Chunk = null;

            byte[] rawData        = new byte[Data2Read];
            long   startByteIndex = -1;
            int    BytesRead      = -1;

            for (long i = FileInfoHeader.BitmapHeight - (y + height); i < (FileInfoHeader.BitmapHeight - y); i++)
            {
                startByteIndex = 70 + (x * 3) + (i * RowSize) - 1;

                SeektoPage(startByteIndex);

                BytesRead = Reader.ReadArray <byte>(startByteIndex % MemoryPageSize,
                                                    rawData, 0, Data2Read);

                if (newPadding != 0)
                {
                    rawData = rawData.Concat(new byte[newPadding]).ToArray();
                }


                if (Chunk == null)
                {
                    if (BytesRead != Data2Read)
                    {
                        Chunk   = rawData.SubArray(0, BytesRead);
                        Offset += BytesRead;

                        if (Offset + VirtualSize >= MmfInfo.Length)
                        {
                            VirtualSize = MmfInfo.Length - Offset;
                        }
                        else
                        {
                            VirtualSize = MemoryPagesForVirutalMemoryBuffer * MemoryPageSize;
                        }

                        Reader.Dispose();
                        Reader = Mmf.CreateViewAccessor(Offset, VirtualSize);

                        BytesRead += Reader.ReadArray <byte>(Offset % MemoryPageSize, rawData, BytesRead, rawData.Length - BytesRead);
                        if (BytesRead != RowSize)
                        {
                            Chunk = Chunk.Concat(rawData.SubArray(0, BytesRead));
                        }
                        else
                        {
                            Chunk = Chunk.Concat(rawData);
                        }
                    }
                    else
                    {
                        Chunk = rawData;
                    }
                }

                else
                if (BytesRead != Data2Read)
                {
                    Chunk   = Chunk.Concat(rawData.SubArray(0, BytesRead));
                    Offset += BytesRead;

                    if (Offset + VirtualSize >= MmfInfo.Length)
                    {
                        VirtualSize = MmfInfo.Length - Offset;
                    }
                    else
                    {
                        VirtualSize = MemoryPagesForVirutalMemoryBuffer * MemoryPageSize;
                    }

                    Reader.Dispose();
                    Reader = Mmf.CreateViewAccessor(Offset, VirtualSize);

                    BytesRead += Reader.ReadArray <byte>(Offset % MemoryPageSize, rawData, BytesRead, rawData.Length - BytesRead);
                    if (BytesRead != RowSize)
                    {
                        Chunk = Chunk.Concat(rawData.SubArray(0, BytesRead));
                    }
                    else
                    {
                        Chunk = Chunk.Concat(rawData);
                    }
                }
                else
                {
                    Chunk = Chunk.Concat(rawData);
                }
            }

            var fileHeader = new BitmapHeader()
            {
                FileSize = Chunk.Count() + 54
            };
            var fileInfoHeader = new BitmapInfoHeader()
            {
                BitmapHeight         = (int)height,
                BitmapWidth          = (int)width,
                BitsPerPixel         = FileInfoHeader.BitsPerPixel,
                ColorPlanes          = FileInfoHeader.ColorPlanes,
                HorizantalResolution = FileInfoHeader.HorizantalResolution,
                VerticalResolution   = FileInfoHeader.VerticalResolution
            };

            var HeaderData = fileHeader.CreateBitmapHeader();

            HeaderData = HeaderData.Concat(fileInfoHeader.CreateInfoHeaderData(Chunk.Count())).ToArray();
            HeaderData = HeaderData.Concat(Chunk).ToArray();


            using (var ms = new MemoryStream(HeaderData))
            { CurrentChunk = new Bitmap(ms); }

            return(CurrentChunk);
        }