Esempio n. 1
0
        private async Task <Stream> DibToBmpStreamAsync(Stream stream, CancellationToken token)
        {
            int dibSize        = (int)stream.Length;
            int fileHeaderSize = Marshal.SizeOf <BITMAPFILEHEADER>();
            int bmpSize        = fileHeaderSize + dibSize;

            var buffer = new byte[bmpSize];

            await stream.ReadFixedAsync(buffer.AsMemory(fileHeaderSize, dibSize), token).ConfigureAwait(false);

            var infoHeader = MemoryMarshal.Read <BITMAPINFOHEADER>(buffer.AsSpan(fileHeaderSize));

            var fileHeader = new BITMAPFILEHEADER
            {
                bfType      = BITMAPFILEHEADER.BM,
                bfSize      = fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage,
                bfReserved1 = 0,
                bfReserved2 = 0,
                bfOffBits   = fileHeaderSize + infoHeader.biSize + infoHeader.biClrUsed * 4
            };

            MemoryMarshal.Write(buffer, ref fileHeader);

            return(new MemoryStream(buffer));
        }
Esempio n. 2
0
        private Stream DibToBmpStream(Stream stream)
        {
            int dibSize        = (int)stream.Length;
            int fileHeaderSize = Marshal.SizeOf <BITMAPFILEHEADER>();
            int bmpSize        = fileHeaderSize + dibSize;

            var buffer = new byte[bmpSize];

            stream.ReadFixed(buffer.AsSpan(fileHeaderSize, dibSize));

            var infoHeader = MemoryMarshal.Read <BITMAPINFOHEADER>(buffer.AsSpan(fileHeaderSize));

            var fileHeader = new BITMAPFILEHEADER
            {
                bfType      = BITMAPFILEHEADER.BM,
                bfSize      = fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage,
                bfReserved1 = 0,
                bfReserved2 = 0,
                bfOffBits   = fileHeaderSize + infoHeader.biSize + infoHeader.biClrUsed * 4
            };

            MemoryMarshal.Write(buffer, ref fileHeader);

            return(new MemoryStream(buffer));
        }