如何将矩阵表示为颜色而不是元素的图像 [英] How to represent a matrix as an image of colors instead of elements
问题描述
你好,
我需要将矩阵(2D数组)表示为颜色而不是数组元素。
可以说,我有一个8x6矩阵作为
1 1 1 1 2 2 >
1 1 1 1 2 2
1 1 1 1 2 2
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
这个
有三个子矩阵,4x4,3x2,2x5表示不同的数字
我需要制作一个矩阵大小的图像,这样每个子矩阵都应该涂上不同的颜色(除了以外的任何颜色)白色)。
任何人都可以帮我在c#.net中执行此操作吗?我不擅长图形。
提前感谢
Hello there,
I need to represent a matrix (2D array) as colors instead of array elements.
Lets say, I have a 8x6 matrix as
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
in this
there are three sub matrices as 4x4, 3x2, 2x5 represented with different numbers
I need to make an image with a rectangle of the matrix-size in such a way that each sub-matrix should be painted with different colors(any color other than white).
Can anyone help me to do this in c#.net? I am not good in graphics.
thanks in advance
推荐答案
设置一个面板
表单上的对象。在面板的OnPaint
事件处理程序中,您将矩阵绘制为所需颜色的方块平铺。使用Graphics :: FillRectangle
方法,传递SolidBrush
。
或者,如果您希望显示值,也可以创建Label
或文本框
es并在运行时设置它们的背景/前景色。
Set aPanel
object on the form. In theOnPaint
event handler of the panel, you will draw the matrix as a tiling of squares of the desired colors. Use theGraphics::FillRectangle
method, passing it aSolidBrush
.
Alternatively, if you want the values to appear too, you can create arrays ofLabel
s orTextbox
es and set their background/foreground colors at run-time.
我从另一个网站得到了同一个问题的答案。这是
I got the answer from another website for the same question. Here is it
<pre><%@ 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;
// 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));
}
}
// Do everything as like with the bitmap:
// show it up, e.g. myPictureBox.Image = result;
// save it to the file...
// Here I am showing it in the page
result.Save(Response.OutputStream, ImageFormat.Jpeg);
result.Dispose();
Response.End();
%>
这篇关于如何将矩阵表示为颜色而不是元素的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!