【发布时间】:2014-02-28 14:15:17
【问题描述】:
我搜索了有关字节数组的所有问题,但总是失败。我从来没有编写过 c#,我是这方面的新手。你能帮我如何从字节数组制作图像文件吗?
这是我的函数,它将字节存储在名为 imageData 的数组中
public void imageReady( byte[] imageData, int fWidth, int fHeight))
【问题讨论】:
我搜索了有关字节数组的所有问题,但总是失败。我从来没有编写过 c#,我是这方面的新手。你能帮我如何从字节数组制作图像文件吗?
这是我的函数,它将字节存储在名为 imageData 的数组中
public void imageReady( byte[] imageData, int fWidth, int fHeight))
【问题讨论】:
您需要将这些bytes 放入MemoryStream:
Bitmap bmp;
using (var ms = new MemoryStream(imageData))
{
bmp = new Bitmap(ms);
}
使用 Bitmap(Stream stream) 构造函数重载。
更新:请记住,根据文档和我一直在阅读的源代码,ArgumentException 将在这些条件下抛出:
stream does not contain image data or is null.
-or-
stream contains a PNG image file with a single dimension greater than 65,535 pixels.
【讨论】:
Bitmap(Stream):“您必须在位图的生命周期内保持流打开。”
MemoryStream,那么您不必担心会关闭它,因为MemoryStream 在被处理时实际上并没有做任何事情。 System.Drawing.ImageConverter.ConvertFrom 方法实际上利用了这一事实,因此做出这样的假设似乎是安全的。做,只需var bmp = new Bitmap(new MemoryStream(imageData)); 就足够了。
各位,谢谢你们的帮助。我认为所有这些答案都有效。但是我认为我的字节数组包含原始字节。这就是为什么所有这些解决方案都不适用于我的代码。
但是我找到了解决方案。也许这个解决方案可以帮助其他像我一样遇到问题的程序员。
static byte[] PadLines(byte[] bytes, int rows, int columns) {
int currentStride = columns; // 3
int newStride = columns; // 4
byte[] newBytes = new byte[newStride * rows];
for (int i = 0; i < rows; i++)
Buffer.BlockCopy(bytes, currentStride * i, newBytes, newStride * i, currentStride);
return newBytes;
}
int columns = imageWidth;
int rows = imageHeight;
int stride = columns;
byte[] newbytes = PadLines(imageData, rows, columns);
Bitmap im = new Bitmap(columns, rows, stride,
PixelFormat.Format8bppIndexed,
Marshal.UnsafeAddrOfPinnedArrayElement(newbytes, 0));
im.Save("C:\\Users\\musa\\Documents\\Hobby\\image21.bmp");
此解决方案适用于 8bit 256 bpp (Format8bppIndexed)。如果您的图片有其他格式,您应该更改PixelFormat。
现在颜色有问题。一旦我解决了这个问题,我将为其他用户编辑我的答案。
*PS = 我不确定步幅值,但对于 8 位,它应该等于列。
这个功能也适用于我。这个功能将 8 位灰度图像复制到 32 位布局中。
public void SaveBitmap(string fileName, int width, int height, byte[] imageData)
{
byte[] data = new byte[width * height * 4];
int o = 0;
for (int i = 0; i < width * height; i++)
{
byte value = imageData[i];
data[o++] = value;
data[o++] = value;
data[o++] = value;
data[o++] = 0;
}
unsafe
{
fixed (byte* ptr = data)
{
using (Bitmap image = new Bitmap(width, height, width * 4,
PixelFormat.Format32bppRgb, new IntPtr(ptr)))
{
image.Save(Path.ChangeExtension(fileName, ".jpg"));
}
}
}
}
【讨论】:
可以这么简单:
var ms = new MemoryStream(imageData);
System.Drawing.Image image = Image.FromStream(ms);
image.Save("c:\\image.jpg");
测试一下:
byte[] imageData;
// Create the byte array.
var originalImage = Image.FromFile(@"C:\original.jpg");
using (var ms = new MemoryStream())
{
originalImage.Save(ms, ImageFormat.Jpeg);
imageData = ms.ToArray();
}
// Convert back to image.
using (var ms = new MemoryStream(imageData))
{
Image image = Image.FromStream(ms);
image.Save(@"C:\newImage.jpg");
}
【讨论】:
static 访问器为猫剥皮的方法,但是是的,您需要将 MemoryStream 包裹在 using 中。
System.Windows.Controls.Image 是一个显示图像的控件。这里使用的是 System.Drawing.Image 类。
Image.FromStream 方法的字节数组不是有效图像。
此外,您可以简单地将byte array 转换为Bitmap。
var bmp = new Bitmap(new MemoryStream(imgByte));
您也可以直接从文件路径获取Bitmap。
Bitmap bmp = new Bitmap(Image.FromFile(filePath));
【讨论】: