Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c从二维数组中绘制.bmp#_C#_Image_Bmp - Fatal编程技术网

C# 使用c从二维数组中绘制.bmp#

C# 使用c从二维数组中绘制.bmp#,c#,image,bmp,C#,Image,Bmp,我正在尝试从二维布尔数组中绘制bmp图像文件。目标如下:我需要为每个值绘制一个小正方形,颜色取决于布尔值,如果为true,它以给定的颜色绘制,如果为false,它以白色绘制。 这个想法是基于矩阵创建一个迷宫 我在网上找到的大多数解决方案都是使用MemoryStream的一维字节数组,但我并没有用自己选择的大小绘制一个完整的正方形 我的主要问题是如何使用c绘制bmp或图像# 提前感谢您的任何建议我不确定您的输出设计将是什么,但这可能会让您开始使用GDI int boardHeight=120; i

我正在尝试从二维布尔数组中绘制bmp图像文件。目标如下:我需要为每个值绘制一个小正方形,颜色取决于布尔值,如果为true,它以给定的颜色绘制,如果为false,它以白色绘制。 这个想法是基于矩阵创建一个迷宫

我在网上找到的大多数解决方案都是使用MemoryStream的一维字节数组,但我并没有用自己选择的大小绘制一个完整的正方形

我的主要问题是如何使用c绘制bmp或图像#


提前感谢您的任何建议

我不确定您的输出设计将是什么,但这可能会让您开始使用GDI

int boardHeight=120;
int boardWidth=120;
int squareHeight=12;
int squareWidth=12;
Bitmap bmp = new Bitmap(boardWidth,boardHeight);
using(Graphics g = Graphics.FromImage(bmp))
using(SolidBrush trueBrush = new SolidBrush(Color.Blue)) //Change this color as needed
{
    bool squareValue = true; // or false depending on your array
    Brush b = squareValue?trueBrush:Brushes.White;
    g.FillRectangle(b,0,0,squareWidth,squareHeight);
}

您需要根据对输出图像的要求来扩展此功能,并在数组中进行迭代,但由于您指出您的主要问题是开始在.Net中绘图,因此希望此示例为您提供必要的基础知识。

以下是一个使用二维数组并保存生成位图的解决方案。你要么从文本文件中读取迷宫,要么像我一样手工输入。您可以使用
squareWidth
squareHeight
变量调整平铺的大小。使用一维数组也可以,但如果你只是在学习这些东西,可能就没有那么直观了

bool[,] maze = new bool[2,2];
maze[0, 0] = true;
maze[0, 1] = false;
maze[1, 0] = false;
maze[1, 1] = true;
const int squareWidth = 25;
const int squareHeight = 25;
using (Bitmap bmp = new Bitmap((maze.GetUpperBound(0) + 1) * squareWidth, (maze.GetUpperBound(1) + 1) * squareHeight))
{
    using (Graphics gfx = Graphics.FromImage(bmp))
    {
        gfx.Clear(Color.Black);
        for (int y = 0; y <= maze.GetUpperBound(1); y++)
        {
            for (int x = 0; x <= maze.GetUpperBound(0); x++)
            {
                if (maze[x, y])
                    gfx.FillRectangle(Brushes.White, new Rectangle(x * squareWidth, y * squareHeight, squareWidth, squareHeight));
                else
                    gfx.FillRectangle(Brushes.Black, new Rectangle(x * squareWidth, y * squareHeight, squareWidth, squareHeight));
            }
        }
    }
    bmp.Save(@"c:\maze.bmp");
}
bool[,]maze=newbool[2,2];
迷宫[0,0]=真;
迷宫[0,1]=假;
迷宫[1,0]=假;
迷宫[1,1]=真;
常数int squareWidth=25;
const int squareHeight=25;
使用(位图bmp=新位图((maze.GetUpperBound(0)+1)*squareWidth,(maze.GetUpperBound(1)+1)*squareHeight))
{
使用(Graphics gfx=Graphics.FromImage(bmp))
{
gfx.透明(颜色:黑色);

对于(int y=0;y您的平台是什么?silverlight?wpf?winforms?asp.net?(等),解决方案可能取决于此信息。