栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从字节数组创建位图?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从字节数组创建位图?

伙计们感谢您的帮助。我认为所有这些答案都行得通。但是我认为我的字节数组包含原始字节。这就是为什么所有这些解决方案都不适用于我的代码的原因。

但是我找到了解决方案。也许这种解决方案可以帮助其他遇到像我这样的问题的编码人员。

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

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/421482.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号