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); }
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(); } }
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); }