【发布时间】:2010-05-28 20:49:28
【问题描述】:
使用 .NET 提供的图像文件编解码器可以编码的图像大小是否有限制?
我正在尝试对大小大于 4GB 的图像进行编码,但它根本无法使用 .bmp、.jpg、.png 或 .tif 编码器进行编码(或无法正常工作,即写出不可读的文件)。
当我将图像大小降低到
我的下一次尝试是尝试 libtiff,因为我知道 tiff 文件适用于大图像。
什么是大图像的好文件格式?还是我只是达到了文件格式限制?
(所有这些都在 64 位操作系统 (WinXP 64) 上完成,配备 8 GB RAM 并使用 x64 架构编译。)
Random r = new Random((int)DateTime.Now.Ticks);
int width = 64000;
int height = 64000;
int stride = (width % 4) > 0 ? width + (width % 4) : width;
UIntPtr dataSize = new UIntPtr((ulong)stride * (ulong)height);
IntPtr p = Program.VirtualAlloc(IntPtr.Zero, dataSize, Program.AllocationType.COMMIT | Program.AllocationType.RESERVE, Program.MemoryProtection.READWRITE);
Bitmap bmp = new Bitmap(width, height, stride, PixelFormat.Format8bppIndexed, p);
BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
ColorPalette cp = bmp.Palette;
for (int i = 0; i < cp.Entries.Length; i++)
{
cp.Entries[i] = Color.FromArgb(i, i, i);
}
bmp.Palette = cp;
unsafe
{
for (int y = 0; y < bd.Height; y++)
{
byte* row = (byte*)bd.Scan0.ToPointer() + (y * bd.Stride);
for (int x = 0; x < bd.Width; x++)
{
*(row + x) = (byte)r.Next(256);
}
}
}
bmp.UnlockBits(bd);
bmp.Save(@"c:\test.jpg", ImageFormat.Jpeg);
bmp.Dispose();
Program.VirtualFree(p, UIntPtr.Zero, 0x8000);
我也尝试过使用固定 GC 内存区域,但这仅限于
Random r = new Random((int)DateTime.Now.Ticks);
int bytesPerPixel = 4;
int width = 4000;
int height = 4000;
int padding = 4 - ((width * bytesPerPixel) % 4);
padding = (padding == 4 ? 0 : padding);
int stride = (width * bytesPerPixel) + padding;
UInt32[] pixels = new UInt32[width * height];
GCHandle gchPixels = GCHandle.Alloc(pixels, GCHandleType.Pinned);
using (Bitmap bmp = new Bitmap(width, height, stride, PixelFormat.Format32bppPArgb, gchPixels.AddrOfPinnedObject()))
{
for (int y = 0; y < height; y++)
{
int row = (y * width);
for (int x = 0; x < width; x++)
{
pixels[row + x] = (uint)r.Next();
}
}
bmp.Save(@"c:\test.jpg", ImageFormat.Jpeg);
}
gchPixels.Free();
【问题讨论】:
-
您使用的是什么处理器架构?
-
@Rowland - 使用 x64 编译。所以64位。对于这么大的图像,我认为 32 位是不够的。我的机器有 8GB 的 RAM,也许这还不够,尽管它应该用于略高于 4GB 的图像。我有大约 7GB 的空闲空间,当编码器启动时,还有大约 1-2GB 的空闲空间。
-
@roygbiv 只是检查显而易见的:)
-
BMP 仅限于用 2 个带符号的 16 位整数表示图像尺寸,因此假设 32 BPP,最大的 BMP 不能是 4GB(图像数据为 32767*32767*4 字节)。