CodeGo.net>使用内存映射视图查看大的位图图像
作者:互联网
我有一个非常大的位图,正在尝试使用C#应用程序进行查看.
这里的主要问题是我无法将其直接加载到内存中,因此我尝试使用内存映射视图库按照Reference One、Reference Two、Reference Three、Reference Four和Reference Five的指南进行加载.
我到现在为止的内容如下:
根据需要读取部分位图(例如,读取前200行).
从我读取的行中创建另一个位图并显示它.
问题:
重建的位图图像部分丢失了颜色信息,并且上下颠倒显示.
示例:-[注意:我在这里使用小尺寸图像,并尝试显示其中的一部分以进行测试]
真实图像:-
输出应为(选择前200行并重建较小的位图并显示):
如您所见,重建的图像是无色且上下颠倒的.
现在的代码部分:
BMPMMF类,负责整个过程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
namespace BMPViewer
{
class BMPMMF
{
/// <summary>
/// It opens the image using memory mapped view and read the needed
/// parts, then call CreateBM to create a partially bitmap
/// </summary>
/// <param name="bmpFilename">Path to the physical bitmap</param>
/// <returns></returns>
public Bitmap readPartOfImage(string bmpFilename)
{
var headers = ReadHeaders(bmpFilename);
var mmf = MemoryMappedFile.CreateFromFile(bmpFilename, FileMode.Open);
int rowSize = headers.Item2.RowSize; // number of byes in a row
// Dictionary<ColorObject, int> rowColors = new Dictionary<ColorObject, int>();
int colorSize = Marshal.SizeOf(typeof(MyColor));
int width = rowSize / colorSize;//(headers.Item1.DataOffset+ rowSize) / colorSize;
int height = 200;
ColorObject cObj;
MyColor outObj;
ColorObject[][] rowColors = new ColorObject[height][];
// Read the view image and save row by row pixel
for (int j = 0; j < height; j++)
{
rowColors[j] = new ColorObject[width];
using (var view = mmf.CreateViewAccessor(headers.Item1.DataOffset + rowSize * j, rowSize, MemoryMappedFileAccess.Read))
{
for (long i = 0; i < rowSize; i += colorSize)
{
view.Read(i, out outObj);
cObj = new ColorObject(outObj);
rowColors[j][i / colorSize] = cObj;
}
}
}
return CreateBM( rowColors );
}
/// <summary>
/// Used to create a bitmap from provieded bytes
/// </summary>
/// <param name="rowColors">Contains bytes of bitmap</param>
/// <returns></returns>
private Bitmap CreateBM(ColorObject[][] rowColors )
{
int width = rowColors[0].Count();
int height = rowColors.Count();
//int width = rowColors.Values.Where(o => o == 0).Count();
Bitmap bitm = new Bitmap(width, height, PixelFormat.Format24bppRgb);
// new Bitmap(imgdat.GetUpperBound(1) + 1, imgdat.GetUpperBound(0) + 1, PixelFormat.Format24bppRgb);
BitmapData bitmapdat = bitm.LockBits(new Rectangle(0, 0, bitm.Width, bitm.Height), ImageLockMode.ReadWrite, bitm.PixelFormat);
int stride = bitmapdat.Stride;
byte[] bytes = new byte[stride * bitm.Height];
for (int r = 0; r < bitm.Height; r++)
{
for (int c = 0; c < bitm.Width; c++)
{
ColorObject color = rowColors[r][c];
bytes[(r * stride) + c * 3] = color.Blue;
bytes[(r * stride) + c * 3 + 1] = color.Green;
bytes[(r * stride) + c * 3 + 2] = color.Red;
}
}
System.IntPtr scan0 = bitmapdat.Scan0;
Marshal.Copy(bytes, 0, scan0, stride * bitm.Height);
bitm.UnlockBits(bitmapdat);
return bitm;
}
/// <summary>
/// Returns a tuple that contains necessary information about bitmap header
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private Tuple<BmpHeader, DibHeader> ReadHeaders(string filename)
{
var bmpHeader = new BmpHeader();
var dibHeader = new DibHeader();
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
using (var br = new BinaryReader(fs))
{
bmpHeader.MagicNumber = br.ReadInt16();
bmpHeader.Filesize = br.ReadInt32();
bmpHeader.Reserved1 = br.ReadInt16();
bmpHeader.Reserved2 = br.ReadInt16();
bmpHeader.DataOffset = br.ReadInt32();
dibHeader.HeaderSize = br.ReadInt32();
if (dibHeader.HeaderSize != 40)
{
throw new ApplicationException("Only Windows V3 format supported.");
}
dibHeader.Width = br.ReadInt32();
dibHeader.Height = br.ReadInt32();
dibHeader.ColorPlanes = br.ReadInt16();
dibHeader.Bpp = br.ReadInt16();
dibHeader.CompressionMethod = br.ReadInt32();
dibHeader.ImageDataSize = br.ReadInt32();
dibHeader.HorizontalResolution = br.ReadInt32();
dibHeader.VerticalResolution = br.ReadInt32();
dibHeader.NumberOfColors = br.ReadInt32();
dibHeader.NumberImportantColors = br.ReadInt32();
}
}
return Tuple.Create(bmpHeader, dibHeader);
}
}
public struct MyColor
{
public byte Red;
public byte Green;
public byte Blue;
//public byte Alpha;
}
public class ColorObject
{
public ColorObject(MyColor c)
{
this.Red = c.Red;
this.Green = c.Green;
this.Blue = c.Blue;
// this.Alpha = c.Alpha;
}
public byte Red;
public byte Green;
public byte Blue;
// public byte Alpha;
}
public class BmpHeader
{
public short MagicNumber { get; set; }
public int Filesize { get; set; }
public short Reserved1 { get; set; }
public short Reserved2 { get; set; }
public int DataOffset { get; set; }
}
public class DibHeader
{
public int HeaderSize { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public short ColorPlanes { get; set; }
public short Bpp { get; set; }
public int CompressionMethod { get; set; }
public int ImageDataSize { get; set; }
public int HorizontalResolution { get; set; }
public int VerticalResolution { get; set; }
public int NumberOfColors { get; set; }
public int NumberImportantColors { get; set; }
public int RowSize
{
get
{
return 4 * ((Bpp * Width) / 32);
}
}
}
}
这是使用方法:-
Bitmap bmpImage = bmp.readPartOfImage(filePath); // path to bitmap
pictBoxBMP.Image = bmpImage; // set the picture box image to the new Partially created bitmap
我寻求的解决方案:-
只需正确显示部分创建的位图即可,我猜想在位图重建或使用内存映射视图的位图读取中存在问题.
更新#1:应用@TaW solution后,我得到了显示的颜色,即使它们与原始颜色也有些许不同,但是可以接受.
解决方法:
我相信您做得不正确.从您的颜色结构/类来看,您使用24Bpp.
对于24Bpp,要添加的填充将为Width%4,因此在RowSize getter中进行更改
return 4 * ((Bpp * Width) / 32);
至
return (Bpp / 8 * Width) + Width % 4;
对于一般情况,您可以将步幅作为
Padding = Width % (4 * (4 - (Bpp / 8) );
根据定义,行的顺序是上下颠倒的(即自下而上),但是您是按自上而下创建的!所以在CreateBM更改
for (int r = 0; r < bitm.Height; r++)
至
for (int r = bitm.Height - 1 ; r > 0; r--)
至于颜色:先尝试修复这些问题,然后再报告结果,好吗?
标签:memory-mapped-files,image,bitmap,c 来源: https://codeday.me/bug/20191028/1954432.html