Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
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
Posted
Comments
BillWoodruff 28-Jan-14 6:23am    
What have you tried so far ? Please show your code.
lukeer 29-Jan-14 4:43am    
Code doesn't belong in a comment. It's much more readable if you move it to your question (use the Improve question link) and embrace it in preformat tags:<pre lang="c#">YourCodeHere();</pre>

Set a Panel object on the form. In the OnPaint event handler of the panel, you will draw the matrix as a tiling of squares of the desired colors. Use the Graphics::FillRectangle method, passing it a SolidBrush.

Alternatively, if you want the values to appear too, you can create arrays of Labels or Textboxes and set their background/foreground colors at run-time.
 
Share this answer
 
v3
Comments
a4ashiq 29-Jan-14 3:42am    
Hi YvesDaoust,
its for asp.net web forms. i forgot to mention that in my question. I already got the answer yesterday. But i think your suggestion will work. I'll try that also for sure. Thanks for the reply.
I got the answer from another website for the same question. Here is it

C#
<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();
%>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900