使用 .NET 提供的图像文件编解码器可以编码的图像大小是否有限制?
我正在尝试使用 .bmp、.jpg、.png 或 .tif 编码器对大小 > 4GB 的图像进行编码,但它根本无法正常工作(或无法正常工作,即写出不可读的文件)。
当我将图像大小降低到 < 2GB 时,它确实适用于 .jpg,但不适用于 .bmp、.tif 或 .png。
我的下一个尝试是尝试 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 内存区域,但这仅限于 < 2GB。
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();