【发布时间】:2010-10-03 11:32:07
【问题描述】:
我有 3 个位图图像,每个图像大约 28-30 MB。我的应用程序需要经常访问这些图像中的像素。
目前我正在创建所有 3 个文件的 BitmapData,锁定它们,然后读取 3 个不同字节 [] 中的所有像素。以下是一张图片的代码:
System.Drawing.Imaging.BitmapData bmpData = result.LockBits(new System.Drawing.Rectangle(0, 0, result.Width , result.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite,((System.Drawing.Bitmap)Properties.Resources.bmp1).PixelFormat);
int pixelBytes = System.Drawing.Image.GetPixelFormatSize(((System.Drawing.Bitmap)Properties.Resources.silk_box).PixelFormat) / 8;
System.IntPtr ptr = bmpData.Scan0;
int size = bmpData.Stride * result.Height;
byte[] pixels = new byte[size];
System.Runtime.InteropServices.Marshal.Copy(ptr, pixels, 0, size);
所有 3 个位图都添加到应用程序资源中。
解决问题......虽然上述方法工作正常。唯一的问题是性能。将位图转换为 byte[] 并访问特定像素大约需要 10-12 秒。
我猜是下面这行花费了很多时间:
System.Runtime.InteropServices.Marshal.Copy(ptr, pixels, 0, size);
为此,我正在考虑将所有 3 个图像的 byte[] 添加到应用程序资源中,以便可以直接访问它,并且不需要将图像转换为 byte[]。 那么性能应该会提高。
请问上面的方法会成功吗?
我想知道如何将图像转换为字节[],然后将它们添加到应用程序资源中?
这个问题还有其他解决办法吗?
谢谢
【问题讨论】: