且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何创建字节数组位图?

更新时间:2023-11-09 22:42:58

大家好感谢你的帮助。我想,这一切答案的作品。不过,我觉得我的字节数组包含原始字节。这就是为什么所有这些解决方案,为我的code没有工作。

不过,我找到了解决办法。也许这个解决方案可以帮助其他codeRS谁拥有像我一样的问题。

 字节的静态[] PadLines(字节[]字节,诠释行,诠释列){
   VAR currentStride =列; // 3
   VAR newStride =列; // 4
   VAR newBytes =新的字节[newStride *行]。
   对于(VAR I = 0; I<行;我++)
       Buffer.BlockCopy(字节,currentStride *我,newBytes,newStride *我,currentStride);
   返回newBytes;
 } VAR列= imageWidth;
 VAR行= imageHeight;
 VAR跨距=列;
 VAR newbytes = PadLines(为imageData,行,列); VAR IM =新位图(列,行,步幅,
          PixelFormat.Format8bppIndexed,
          Marshal.UnsafeAddrOfPinnedArrayElement(newbytes,0)); im.Save(C:\\\\ \\\\用户\\\\穆萨文件\\\\ \\\\爱好image21.bmp);

该解决方案适用于8bit的256 BPP(Format8bppIndexed)。如果您的图像有另一种格式,你应该改变的PixelFormat

和有颜色的问题现在。当我解决了这一我将编辑我的回答其他用户。

* PS =我不知道跨距值但对于8位应该等于列。

和也此功能为我..该函数将8位灰度图像转化为32位的布局。

 公共无效SaveBitmap(字符串文件名,诠释的宽度,高度INT,字节[]为imageData)
        {            VAR数据=新的字节[宽*高* 4]。            INT O = 0;            对于(VAR I = 0; I<宽*高;我++)
            {
                VAR值=为imageData [I]
                数据〔O ++] =价值;
                数据〔O ++] =价值;
                数据〔O ++] =价值;
                数据〔O ++] = 0;
            }            不安全
            {
                固定(字节* PTR =数据)
                {                    使用(位图图像​​=新位图(宽度,高度,宽度* 4,
                                PixelFormat.Format32bppRgb,新的IntPtr(PTR)))
                    {                        image.Save(Path.ChangeExtension(文件名,名为.jpg));
                    }
                }
            }
        }

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.

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) {
   var currentStride = columns; // 3
   var newStride = columns;  // 4
   var newBytes = new byte[newStride * rows];
   for (var i = 0; i < rows; i++)
       Buffer.BlockCopy(bytes, currentStride * i, newBytes, newStride * i, currentStride);
   return newBytes;
 }

 var columns = imageWidth;
 var rows = imageHeight;
 var stride = columns;
 var newbytes = PadLines(imageData, rows, columns);

 var im = new Bitmap(columns, rows, stride, 
          PixelFormat.Format8bppIndexed, 
          Marshal.UnsafeAddrOfPinnedArrayElement(newbytes, 0));

 im.Save("C:\\Users\\musa\\Documents\\Hobby\\image21.bmp");

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 = I am not sure about stride value but for 8bit it should be equal to columns.

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)
        {

            var data = new byte[width * height * 4];

            int o = 0;

            for (var i = 0; i < width * height; i++)
            {
                var 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"));
                    }
                }
            }
        }