如何从字节数组创建位图? [英] How to create bitmap from byte array?

查看:25
本文介绍了如何从字节数组创建位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了所有关于字节数组的问题,但总是失败.我从未编码过 c# 我是这方面的新手.你能帮我如何从字节数组制作图像文件吗?

I searched all question about byte array but i always failed. I have never coded c# i am new in this side. Could you help me how to make image file from byte array.

这是我的函数,它将字节存储在名为 imageData

Here is my function which stores byte in array named imageData

public void imageReady( byte[] imageData, int fWidth, int fHeight))

推荐答案

各位朋友,感谢您的帮助.我认为所有这些答案都有效.但是我认为我的字节数组包含原始字节.这就是为什么所有这些解决方案都不适用于我的代码的原因.

Guys thank you for your help. I think all of this answers works. However i think my byte array contains raw bytes. That's why all of those solutions didnt work for my code.

但是我找到了解决方案.也许这个解决方案可以帮助其他和我一样有问题的程序员.

However i found a solution. Maybe this solution helps other coders who have problem like mine.

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");

此解决方案适用于 8 位 256 bpp (Format8bppIndexed).如果您的图片有其他格式,您应该更改 PixelFormat .

This solutions works for 8bit 256 bpp (Format8bppIndexed). If your image has another format you should change PixelFormat .

现在颜色有问题.一旦我解决了这个问题,我就会为其他用户编辑我的答案.

And there is a problem with colors right now. As soon as i solved this one i will edit my answer for other users.

*PS = 我不确定步幅值,但对于 8 位,它应该等于列.

*PS = I am not sure about stride value but for 8bit it should be equal to columns.

还有这个功能对我有用..这个功能将 8 位灰度图像复制到 32 位布局中.

And also this function Works for me.. This function copies 8 bit greyscale image into a 32bit layout.

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"));
                    }
                }
            }
        }

这篇关于如何从字节数组创建位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆