C# 如何在c语言中将矩阵显示为颜色而不是元素#

C# 如何在c语言中将矩阵显示为颜色而不是元素#,c#,.net,matrix,colors,representation,C#,.net,Matrix,Colors,Representation,我需要将矩阵(2D数组)表示为颜色,而不是数组元素 比方说,我有一个8x6矩阵作为 1 1 2 2 1 1 2 2 1 1 2 2 1100 330 330 0 0 0 0 0 0 0 0 0 0 在这个 有三个子矩阵,分别为4x4、3x2和2x5,用不同的数字表示 我需要用矩阵大小的矩形制作一个图像,这样每个子矩阵都应该用不同的颜色(除白色以外的任何颜色)绘制 有人能帮我在c#net中完成这项工作吗?我不擅长图形。 提前感谢好吧,只需创建一个图像(如位图)并绘制、保存等: 公共静态位图矩阵as

我需要将矩阵(2D数组)表示为颜色,而不是数组元素

比方说,我有一个8x6矩阵作为

1 1 2 2

1 1 2 2

1 1 2 2

1100

330

330

0 0 0 0 0

0 0 0 0 0

在这个

有三个子矩阵,分别为4x4、3x2和2x5,用不同的数字表示

我需要用矩阵大小的矩形制作一个图像,这样每个子矩阵都应该用不同的颜色(除白色以外的任何颜色)绘制

有人能帮我在c#net中完成这项工作吗?我不擅长图形。 提前感谢

好吧,只需创建一个图像(如位图)并绘制、保存等:

公共静态位图矩阵asbitmap(int[,]数据){
if(Object.ReferenceEquals(null,data))
返回null;
//可能的笔刷(自己填充)
画笔[]画笔=新画笔[]{

brushs.Red,//Asp.net示例代码由Dmitry Bychenko的答案生成

<%@ Page Language="C#" ContentType="image/jpeg" %>

<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%
    Response.Clear();

int[,] A = new [,] {
{1, 1, 1, 1, 2, 2},
{1, 1, 1, 1, 2, 2},
{1, 1, 1, 1, 2, 2},
{1, 1, 1, 1, 0, 0},

{3, 3, 3, 3, 3, 0},
{3, 3, 3, 3, 3, 0},

{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};

int[,] data = A; 
// Do everything as like with the bitmap:
//  show it up, e.g. myPictureBox.Image = result;
//  save it to the file...

// Possible brushes (fill yourself)
Brush[] brushes = new Brush[] {
Brushes.Red,     // <- color for 0
Brushes.Green,   // <- color for 1
Brushes.Blue,    // <- color for 2
Brushes.Yellow,  // <- ...
Brushes.Cyan
};

// Let resulting bitmap be about 200x200
int step_x = 200 / (data.GetUpperBound(1) - data.GetLowerBound(1));
int step_y = 200 / (data.GetUpperBound(0) - data.GetLowerBound(0));

Bitmap result = new Bitmap((data.GetUpperBound(1) - data.GetLowerBound(1) + 1) * step_x,
                         (data.GetUpperBound(0) - data.GetLowerBound(0) + 1) * step_y);

using (Graphics gc = Graphics.FromImage(result)) {
for (int i = data.GetLowerBound(0); i <= data.GetUpperBound(0); ++i)
  for (int j = data.GetLowerBound(1); j <= data.GetUpperBound(1); ++j) {
    int v = data[i, j];
    gc.FillRectangle(brushes[v % brushes.Length], new Rectangle(j * step_x, i * step_y,     step_x, step_y));
  }
}

result.Save(Response.OutputStream, ImageFormat.Jpeg);
result.Dispose();
Response.End();
%>


WinForms?WPF?其他什么?…使用WinForms,您可以使用
面板
并使用。只需迭代矩阵并根据数字绘制每个“正方形”。每个正方形应为面板大小的1/6乘1/8。为每个索引使用预定义的颜色数组。尝试此操作,然后在遇到问题时返回,并从技术上显示您的代码,si既然问题是创建一个映像(映像实例?),那么目标平台(WinForms、WPF等)在如何生成所需的位图方面不起任何作用,比如说,位图对不起,我很晚才回复,我打算绘制asp.net web窗体。因为我的要求是asp.net,所以我为asp重写了。我将作为另一个答案发布,以便其他人都能从中受益。
<%@ Page Language="C#" ContentType="image/jpeg" %>

<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%
    Response.Clear();

int[,] A = new [,] {
{1, 1, 1, 1, 2, 2},
{1, 1, 1, 1, 2, 2},
{1, 1, 1, 1, 2, 2},
{1, 1, 1, 1, 0, 0},

{3, 3, 3, 3, 3, 0},
{3, 3, 3, 3, 3, 0},

{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};

int[,] data = A; 
// Do everything as like with the bitmap:
//  show it up, e.g. myPictureBox.Image = result;
//  save it to the file...

// Possible brushes (fill yourself)
Brush[] brushes = new Brush[] {
Brushes.Red,     // <- color for 0
Brushes.Green,   // <- color for 1
Brushes.Blue,    // <- color for 2
Brushes.Yellow,  // <- ...
Brushes.Cyan
};

// Let resulting bitmap be about 200x200
int step_x = 200 / (data.GetUpperBound(1) - data.GetLowerBound(1));
int step_y = 200 / (data.GetUpperBound(0) - data.GetLowerBound(0));

Bitmap result = new Bitmap((data.GetUpperBound(1) - data.GetLowerBound(1) + 1) * step_x,
                         (data.GetUpperBound(0) - data.GetLowerBound(0) + 1) * step_y);

using (Graphics gc = Graphics.FromImage(result)) {
for (int i = data.GetLowerBound(0); i <= data.GetUpperBound(0); ++i)
  for (int j = data.GetLowerBound(1); j <= data.GetUpperBound(1); ++j) {
    int v = data[i, j];
    gc.FillRectangle(brushes[v % brushes.Length], new Rectangle(j * step_x, i * step_y,     step_x, step_y));
  }
}

result.Save(Response.OutputStream, ImageFormat.Jpeg);
result.Dispose();
Response.End();
%>