【问题标题】:Converting 2D array to bitmap image. C#将二维数组转换为位图图像。 C#
【发布时间】:2013-09-06 12:32:09
【问题描述】:

我正在开展一个项目,以使用位图图像分步显示 2D 世界生成过程。 数组数据是这样存储的:

Main.tile[i, j].type = x;

x 为整数值 >= 0。

基本上每次程序循环使用for循环都会改变i和j,在循环结束的循环过程中满足一定条件后运行下面的语句。 因此,可能的顺序是:

Main.tile[4, 67].type = 1;
Main.tile[4, 68].type = 1;
Main.tile[4, 69].type = 0;

等等。

我尝试了几种在数组更改/更新后直接修改位图图像的方法(使用 Bitmap.SetPixel),但这对于 21k、8k 像素分辨率位图来说似乎很慢。

我正在寻找一种在整个循环过程结束时(不是在每个单独的循环之后,而是在步骤之间)消化整个数组的方法,并相应地放置彩色点(取决于数组的值) i, j(好像是一个坐标系)。

有没有比 SetPixel 更快的替代方法,或者有更简单的方法可以将数组保存到位图/图像文件中?

【问题讨论】:

标签: c# arrays bitmap


【解决方案1】:

将您的数组更改为一维数组并在一维数组上应用所有操作,如果您愿意, 显示图像将其更改回二维。

如何将整个数组从 2D 更改为 1D:

byte[,] imageData = new byte[1, 2]
{ 
    {  1,  2 }
    {  3,  4 }
 };

var mergedData = new byte[ImageData.Length];

// Output { 1, 2, 3, 4 }
Buffer.BlockCopy(imageData, 0, mergedData, 0, imageData.Length);

从二维到一维:

// depending on whether you read from left to right or top to bottom.
index = x + (y * width)
index = y + (x * height)

从一维到二维:

x = index % width
y = index / width or

x = index / height
y = index % height

希望这能解决您的问题!

【讨论】:

    猜你喜欢
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 2014-11-21
    • 1970-01-01
    相关资源
    最近更新 更多