我在这里问了一个与 24bpp 图像有关的类似问题,但我正在使用那里描述的技术并且有第二个问题,这次与 48bpp 相关......
我有一个byte[]
包含 16 位颜色深度的图像。所以红色有两个字节,绿色有两个字节,蓝色有两个字节,依次从图像的左上角到右下角。我正在将它加载到Bitmap
这样的状态(考虑到步幅等):
byte[] data = ReadRawData();
// Swap from rgb to bgr
for (int i = 5; i < data.Length; i+=6)
{
var r1 = data[i - 5];
var r2 = data[i - 4];
var b1 = data[i - 1];
var b2 = data[i];
data[i - 5] = b1;
data[i - 4] = b2;
data[i - 1] = r1;
data[i] = r2;
}
// Load into a bitmap (I know the width and height, and the format
using (var b = new Bitmap(157, 196, PixelFormat.Format48bppRgb))
{
var bmpData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, b.PixelFormat);
// Here we loop through each line and make sure it is padded to
// the stride length
var bytesPerLine = b.Width * 6;
for (int i = 0; i < b.Height; i++)
{
IntPtr offset = bmpData.Scan0 + i * bmpData.Stride;
Marshal.Copy(data, i * bytesPerLine, offset, bytesPerLine);
int padding = bmpData.Stride - bytesPerLine;
if (padding > 0)
{
var pad = new byte[padding];
Marshal.Copy(pad, 0, offset + bytesPerLine, padding);
}
}
b.UnlockBits(bmpData);
// Done so save
b.Save("c:\\out.tiff", ImageFormat.Tiff);
}
它会产生这个图像:
但是,图像不正确,它应该如下所示:
所以,我的问题是,好吧,我做错了什么?
更新
如果我在出现字节序问题的情况下切换了 r1,r2 和 b1,b2 (以及绿色的),但是它会像这样绘制图像:
(所以仍然不对 - 但这是否给我们提供了关于可能发生的事情的线索?)
我在 github 上放了一个文件,这是原始数据,因此您可以将其保存到磁盘中,然后这个小方法将打开它:
private static byte[] ReadRawData()
{
byte[] data;
using (var ms = new MemoryStream())
{
using (var f = File.OpenRead("c:\\data16.bin"))
{
byte[] buffer = new byte[2048];
int read;
while ((read = f.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
data = ms.ToArray();
}
return data;
}
如果我需要改用 WPF 库,那对我来说很好。
更新 2
所以,正如评论中所建议的,我想出了一些代码来生成一个我可以推理的字节数组。
因此,我所做的是输出一个长度为 196 * 200 * 6 的字节数组,这样我将有一个 196 像素宽 x 200 高的图像,每个像素有 6 个字节(大小很方便,因为它足够大看到这并不意味着我必须为 Stride 事情烦恼)。然后我决定将图片分成 4 个垂直条纹,每个条纹的字节数是:
- 0x00, 0x00,0x00, 0x00, 0xFF, 0xFF
- 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF
- 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
这应该意味着我可以看到颜色之间的差异吧?好吧,我实际上得到的是这个,所以我做错了什么?:
这是我生成上图的代码:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
namespace ImagePlayingApplication
{
class Program
{
static void Main(string[] args)
{
const int width = 196;
const int height = 200;
const int columnWidth = width / 4;
const int bytesPerPixel = 6;
var data = new byte[width * height * bytesPerPixel];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width * bytesPerPixel; x += bytesPerPixel)
{
int i = y * width * bytesPerPixel + x;
// Blue and Green components
// always 0x00 since we are just
// interested in red
data[i] = 0x00;
data[i + 1] = 0x00;
data[i + 2] = 0x00;
data[i + 3] = 0x00;
if (x < columnWidth * bytesPerPixel)
{
// Left most column, full red
data[i + 4] = 0xFF;
data[i + 5] = 0xFF;
}
else if (x < columnWidth * bytesPerPixel * 2)
{
// Next left, half red
data[i + 4] = 0x00;
data[i + 5] = 0xFF;
}
else if (x < columnWidth * bytesPerPixel * 3)
{
// Next left, other half red
data[i + 4] = 0xFF;
data[i + 5] = 0x00;
}
else
{
// Final column, no red
data[i + 4] = 0x00;
data[i + 5] = 0x00;
}
}
}
using (var b = new Bitmap(width,
height,
PixelFormat.Format48bppRgb))
{
var bmpData = b.LockBits(
new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite,
b.PixelFormat);
Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
b.UnlockBits(bmpData);
b.Save(@"c:\users\public\stripes2.tiff", ImageFormat.Tiff);
}
}
}
}
那么,有谁知道我如何将此处描述的字节数组转换为正确的位图?正如我所说,如果 WPF 中有任何帮助,那很好,或者我可能需要将其转换为,我不知道 64bppargb 格式或什么?