加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 像素数据到图像

发布时间:2020-12-15 17:36:54 所属栏目:百科 来源:网络整理
导读:我有这种格式的列表像素 ArrayList list = new ArrayList();list.add(Red);list.add(Blue);list.add(Green); 我有很多像这样的价值观.我想将数组列表转换为图像..尝试了很多,但没有找到任何适当的材料. 编辑:这可能有所帮助: Listbyte pic = new Listbyte(
我有这种格式的列表像素
ArrayList list = new ArrayList();
list.add(Red);
list.add(Blue);
list.add(Green);

我有很多像这样的价值观.我想将数组列表转换为图像..尝试了很多,但没有找到任何适当的材料.

编辑:这可能有所帮助:

List<byte> pic = new List<byte>(); 
for (int j = 0; j < headdata.Count; j++)
{ 
    if(headdata.ElementAt(j).getHead() == i) 
    { 
        pic.Add((byte)headdata.ElementAt(j).getRed());
        pic.Add((byte)headdata.ElementAt(j).getGreen());
        pic.Add((byte)headdata.ElementAt(j).getBlue()); 
    }
}

解决方法

首先,正如我在评论中所说,最好使用List而不是ArrayList来避免装箱/拆箱.

然后,您需要bpp(每像素位数)和宽度和高度来创建图像.

如果你有所有这些值,那么你可以得到起始像素索引:

int i = ((y * Width) + x) * (depth / 8);

例如:

List<byte> bytes = new List<byte>(); // this list should be filled with values
    int width = 100;
    int height = 100;
    int bpp = 24;

    Bitmap bmp = new Bitmap(width,height);

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {   
            int i = ((y * width) + x) * (bpp / 8);
            if(bpp == 24) // in this case you have 3 color values (red,green,blue)
            {
                 // first byte will be red,because you are writing it as first value
                 byte r = bytes[i]; 
                 byte g = bytes[i + 1]; 
                 byte b = bytes[i + 2]; 
                 Color color = Color.FromArgb(r,g,b);
                 bmp.SetPixel(x,y,color); 
            }

        }
    }

您可以使用其他库来更快地创建位图和写入值. (SetPixel()非常慢.See)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读