How to scan all pixel in square C#? - c#

I get height and width of an element on the website.
Example: Element easelCanvas with height: 1000px and width: 600px.
Note: I using Excel to easy imagine.
It must scan like this:
I do with code:
int width = 600, height = 1000;
for (int i = 0; i < width; i += 60)
{
for (int j = 0; j < height; j += 50)
{
}
}
First rows it scan from at position(0, 0) to final and go like:

I have creates one sample canvas it might be helpful to you ..
var canvas=document.getElementById("myCanvas");
var ctx=canvas.getContext("2d");
var index = 0;
for (var i = 0; i < canvas.height; i += 60)
{
for (var j = 0; j < canvas.width; j += 50)
{
ctx.strokeText("index"+index,j,i+15);
index++;
}
}
JsBin Link

Related

Problem with world generation in unity c#

I want to create a world generation using two noise maps (altitude noise and moisture noise). I create maps of these noises and set them in the inspector to values between 0 and 1.
I want to get result like this:
If Elevation < 1000
{
If Moisture < 50: Desert
Else Forest
}
And it seems that I did it as it should, but for some reason the generation does not work correctly:
Here is my code:
for (int x=0; x < tilemap.Width; x++)
{
for (int y = 0; y < tilemap.Height; y++)
{
// Get height at this position
var height = noiseMap[y * tilemap.Width + x];
var moisureHeight = moisureMap[y * tilemap.Width + x];
// Loop over our configured tile types
for (int i = 0; i < TileTypes.Length; i++)
{
var TileType = TileTypes[i];
for (var j = 0; j < TileType.MoisureValues.Length; j++)
{
// If the height is smaller or equal then use this tiletype
if (height <= TileType.Height)
{
if (moisureHeight <=TileType.MoisureValues[j].MoisureHeight)
{
tilemap.SetTile(x, y, (int)TileType.MoisureValues[j].GroundTile);
break;
}
}
}
}
}
}

Get all pixel data from Bitmap object

I want to fill up a double[400] array with pixel data as seen in the below code.
I have been searching for the answer and I still haven't found one. My attempt is:
Bitmap bm = (Bitmap) Image.FromFile(fileName);
numberVisual.Image = bm;
long overallIteration = 0;
for (long i = 0; i <= 20/*bm.Width*/; ++i)
{
for (long h = 0; h <= 20/*bm.Height*/; ++h)
{
input[/*(i * h)*/overallIteration] =
bm.GetPixel((int)h, (int)i) != Color.White ? 0 : 1;
++overallIteration;
}
}
It gives off an exception on
bm.GetPixel System.ArgumentOutOfRangeException: Parameter must be
positive and < Width.
All the bitmaps I am using are 20x20px.
Please check the height and the width of the image you are loading, if your image is 20x20, you have to iterate from 0 to 19 and not from 0 to 20
Right code
for (long i = 0; i < 20; ++i)
{
for (long h = 0; h < 20; ++h)
{
Wrong code
for (long i = 0; I <= 20; ++i)
{
for (long h = 0; h <= 20; ++h)
{

Grid Algorithm, similar to WPF Grid

Edit
Upon further reading and experimenting my reformulated questions :
How do I measure a Star Column Contents, specially when the contents of the grid can span multiple Cols/Rows?
If I have a Control that is inside Col 0, and it spans 2 cols.
and Col 0 is a Star Col, what size would the column actually take?
Or am I missing something?
EDIT:
Since it seems I didnt explain myself well enough, what I actually need to do is find how to make each of the contents mesure themselfs and report back to the actual Col/Row. My issues is mostly, what happens when a control spans multiple rows, I might be missing something on the grid actually works.
I would like to implement a GRID similar to WPF Grid, but I am having trouble understanding how the algoritm to Mesure the Grid actually works, currently I have something like this :
protected override Size MesureOverride(Size s)
{
int colCount = Columns.Count;
int rowCount = Rows.Count;
bool emptyRows = rowCount == 0;
bool emptyCol = colCount == 0;
if (emptyRows)
rowCount = 1;
if (emptyCol)
colCount = 1;
rows = new GridSegment[rowCount];
cols = new GridSegment[colCount];
if (float.IsNaN(s.Width))
{
//We are trying to mesure our size top
//Find what our contents width is
}
if (float.IsNaN(s.Height))
{
//find what our contents height is
}
CreateCells(s, emptyRows, rows, cols, emptyCol);
float gridWidth = 0;
float gridHeight = 0;
foreach (var col in cols)
{
gridWidth += col.DesiredSize;
}
foreach (var row in rows)
{
gridHeight += row.DesiredSize;
}
float mesureHeight = 0f;
float mesureWidth = 0f;
foreach (var v in this.Childreen)
{
var layoutInfo = v.Layout<GridLayoutInfo>();
var rowspan = layoutInfo.RowSpan;
var colspan = layoutInfo.ColumSpan;
float width = 0;
float height = 0;
float x = 0;
float y = 0;
if (rowspan < 1)
rowspan = 1;
if (colspan < 1)
colspan = 0;
for (int i = 0; i < layoutInfo.Row + rowspan && i < rowCount; i++)
{
if (i > 0 && i <= layoutInfo.Row)
y += rows[i - 1].DesiredSize;
if (i >= layoutInfo.Row)
height += rows[i].DesiredSize;
}
for (int i = 0; i < layoutInfo.Column + colspan && i < colCount; i++)
{
if (i > 0 && i <= layoutInfo.Column)
x += cols[i - 1].DesiredSize;
if (i >= layoutInfo.Column)
width += cols[i].DesiredSize;
}
v.Mesure(new Size(width,height));
//Calculate the grid size
}
return new Size(gridWidth, gridHeight);
}
private void CreateCells(Size s, bool emptyRows, GridSegment[] rows, GridSegment[] cols, bool emptyCol)
{
float avaiableWith = s.Width;
float avaiableHeight = s.Height;
if (!float.IsNaN(Width))
avaiableWith = Width;
if (!float.IsNaN(Height))
avaiableHeight = Height;
float totalRowFixed = 0;
float totalColFixed = 0;
float maxStarRow = 0;
float maxStarCol = 0;
if (emptyRows)
{
rows[0] = new GridSegment(0, 0, float.PositiveInfinity, MesureType.Start);
rows[0].Stars = 1;
maxStarRow = 1;
}
else
for (int index = 0; index < Rows.Count; index++)
{
var v = Rows[index];
rows[index] = new GridSegment(0, v.Min, v.Max, v.Type);
var current = rows[index];
if (current.Type == MesureType.Fixed)
{
//our desired size in units
current.DesiredSize = Mathf.Clamp(v.Value, current.Min, current.Max);
totalRowFixed += current.DesiredSize;
}
else if (current.Type == MesureType.Start)
{
current.Stars = v.Value;
maxStarRow += current.Stars;
}
}
if (emptyCol)
{
cols[0] = new GridSegment(1, 0, float.PositiveInfinity, MesureType.Start);
cols[0].Stars = 1;
maxStarCol = 1;
}
else
for (int index = 0; index < Columns.Count; index++)
{
var v = Columns[index];
cols[index] = new GridSegment(0, v.Min, v.Max, v.Type);
var current = cols[index];
if (current.Type == MesureType.Fixed)
{
//our desired size in units
current.DesiredSize = Mathf.Clamp(v.Value, current.Min, current.Max);
totalColFixed += current.DesiredSize;
}
else if (current.Type == MesureType.Start)
{
current.Stars = v.Value;
maxStarCol += current.Stars;
}
}
//TODO::ADD THE AUTO SIZED DEFs
//Calculate the sizings for all * columns
{
var starColumns = cols.Where(x => x.Type == MesureType.Start);
var stepStarColl = (avaiableWith - totalColFixed) / maxStarCol;
foreach (var gridSegment in starColumns)
{
gridSegment.DesiredSize = stepStarColl * gridSegment.Stars;
}
var starRows = rows.Where(x => x.Type == MesureType.Start);
var stepStarRows = (avaiableHeight - totalRowFixed) / maxStarRow;
foreach (var gridSegment in starRows)
{
gridSegment.DesiredSize = stepStarRows * gridSegment.Stars;
}
}
}
protected override Size ArrangeOverride(Size s)
{
int colCount = Columns.Count;
int rowCount = Rows.Count;
bool emptyRows = rowCount == 0;
bool emptyCol = colCount == 0;
if (emptyRows)
rowCount = 1;
if (emptyCol)
colCount = 1;
rows = new GridSegment[rowCount];
cols = new GridSegment[colCount];
CreateCells(s, emptyRows, rows, cols, emptyCol);
////Loop through all controls
foreach (var v in this.Childreen)
{
var layoutInfo = v.Layout<GridLayoutInfo>();
var rowspan = layoutInfo.RowSpan;
var colspan = layoutInfo.ColumSpan;
float width = 0;
float height = 0;
float x = 0;
float y = 0;
if (rowspan < 1)
rowspan = 1;
if (colspan < 1)
colspan = 0;
for (int i = 0; i < layoutInfo.Row + rowspan && i < rowCount; i++)
{
if (i > 0 && i <= layoutInfo.Row)
y += rows[i - 1].DesiredSize;
if ( i>= layoutInfo.Row )
height += rows[i].DesiredSize;
}
for (int i = 0; i < layoutInfo.Column + colspan && i < colCount; i++)
{
if (i > 0 && i <= layoutInfo.Column)
x += cols[i - 1].DesiredSize;
if (i >= layoutInfo.Column)
width += cols[i].DesiredSize;
}
v.Arrange(new Rect(x, y, width, height));
}
float gridWidth = 0;
float gridHeight = 0;
foreach (var col in cols)
{
gridWidth += col.DesiredSize;
}
foreach (var row in rows)
{
gridHeight += row.DesiredSize;
}
return new Size(gridWidth, gridHeight);
}
This is from my custom library, but I tried to keep it as close to WPF syntax as possible.
This code works almost as I want, but fails once the grid is inside a ScrollingContainer, since the size passed to the measure is Infinity, and it makes the grid take on a infinity large size.
Does anyone have an idea on how to solve this, I am not even using Auto Cols/Rows, since I thought it would simplify the Alg alot - I dwelled into the source of WPF, but I have major hard time trying to understand how it works.

Rotating matrices

Objectives
Imagine that, we have matrix like
a11 a12 a13
a21 a22 a23
a31 a32 a33
What I want to do is, from textbox value rotate this matrix so that, for example if I write 2 and press rotate, program must keep both diagonal values of matrix (in this case a11, a22, a33, a13, a31) and rotate 2 times clockwise other values. So result must be like
a11 a32 a13
a23 a22 a21
a31 a12 a33
It must work for all N x N size matrices, and as you see every 4 rotation takes matrix into default state.
What I've done
So idea is like that, I have 2 forms. First takes size of matrix (1 value, for example if it's 5, it generates 5x5 matrix). When I press OK it generates second forms textbox matrix like that
Form 1 code
private void button1_Click(object sender, EventArgs e)
{
int matrixSize;
matrixSize = int.Parse(textBox1.Text);
Form2 form2 = new Form2(matrixSize);
form2.Width = matrixSize * 50 + 100;
form2.Height = matrixSize *60 + 200;
form2.Show();
//this.Hide();
}
Form 2 code generates textbox matrix from given value and puts random values into this fields
public Form2(int matrSize)
{
int counter = 0;
InitializeComponent();
TextBox[] MatrixNodes = new TextBox[matrSize*matrSize];
Random r = new Random();
for (int i = 1; i <= matrSize; i++)
{
for (int j = 1; j <= matrSize; j++)
{
var tb = new TextBox();
int num = r.Next(1, 1000);
MatrixNodes[counter] = tb;
tb.Name = string.Format("Node_{0}{1}", i, j);
Debug.Write(string.Format("Node_{0}{1}", i, j));
tb.Text = num.ToString();
tb.Location = new Point(j * 50, i * 50);
tb.Width = 30;
tb.Visible = true;
this.splitContainer1.Panel2.Controls.Add(tb);
counter++;
}
}
}
Form 2 has 1 textbox for controlling rotation (others are generated on the fly, programmatically). What I want to do is, when I enter rotation count and press Enter on this textbox, I want to rotate textbox matrix as I explained above. Can't figure out how to do it.
Copy both diagonals to separate arrays, then rotate your matrix and replace diagonals. Below code shows each step:
class Program
{
static void Main(string[] args)
{
int matrixSize = 3;
string[,] matrix = new string[matrixSize,matrixSize];
//create square matrix
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
matrix[x, y] = "a" + (x + 1).ToString() + (y + 1).ToString();
}
}
Console.WriteLine(Environment.NewLine + "Base square matrix");
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(matrix[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
//copy diagonals
string[] leftDiagonal = new string[matrixSize];
string[] rightDiagonal = new string[matrixSize];
for (int x = 0; x < matrixSize; x++)
{
leftDiagonal[x] = matrix[x, x];
rightDiagonal[x] = matrix[matrixSize - 1 - x, x];
}
Console.WriteLine(Environment.NewLine + "Diagonals");
for (int x = 0; x < matrixSize; ++x)
{
Console.Write(leftDiagonal[x] + " " + rightDiagonal[x] + Environment.NewLine);
}
Console.ReadKey();
//rotate matrix
string[,] rotatedMatrix = new string[matrixSize, matrixSize];
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
rotatedMatrix[x, y] = matrix[matrixSize - y - 1, x];
}
}
Console.WriteLine(Environment.NewLine + "Rotated");
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(rotatedMatrix[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
//rotate matrix again
string[,] rotatedMatrixAgain = new string[matrixSize, matrixSize];
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
rotatedMatrixAgain[x, y] = rotatedMatrix[matrixSize - y - 1, x];
}
}
Console.WriteLine(Environment.NewLine + "Rotated again");
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(rotatedMatrixAgain[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
//replace diagonals
for (int x = 0; x < matrixSize; x++)
{
rotatedMatrixAgain[x, x] = leftDiagonal[x];
rotatedMatrixAgain[matrixSize - 1 - x, x] = rightDiagonal[x];
}
Console.WriteLine(Environment.NewLine + "Completed" + Environment.NewLine);
for (int x = 0; x < matrixSize; x++)
{
for (int y = 0; y < matrixSize; y++)
{
Console.Write(rotatedMatrixAgain[x, y] + " ");
}
Console.Write(Environment.NewLine);
}
Console.ReadKey();
}
}
I don't know C#, so I can only give a suggestion in pseudocode:
Input: An N by N matrix in
Output: The input matrix rotated as described in the OP out
for i = 1 to N
for j = 1 to N
if N - j != i and i != j // Do not change values on either diagonal
out[j][N-i] = in[i][j]
else
out[i][j] = in[i][j]
Disclaimer: This algorithm is untested. I suggest you use a debugger to check that it works as you want.
This seems like quite an unorthodox UI presentation, but you're not too far off in terms of being able to achieve your functionality. Instead of a linear array, a rectangular array will make your job much easier. The actual rotation could be implemented with a for loop repeating a single rotation (which would be implemented as in the case 1 code), but I've decided to combine them into the four possible cases. This actually allows you to enter a negative number for number of rotations. Which reminds me, you really should do more error checking. At least protect against int.Parse throwing an exception both places it's used (with a try catch block or by switching to int.TryParse) and make sure it returns a meaningful number (greater than 0, possibly set a reasonable maximum other than int.MaxValue) for matrixSize in button1_Click.
namespace RotatingMatrices
{
public class Form2 : Form
{
// note these class fields
private TextBox[,] matrixNodes;
private int matrixSize;
public Form2(int matrSize)
{
InitializeComponent();
// note these inits
matrixSize = matrSize;
matrixNodes = new TextBox[matrixSize, matrixSize];
Random r = new Random();
// note the new loop limits
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
var tb = new TextBox();
int num = r.Next(1, 1000);
// note the change in indexing
matrixNodes[i,j] = tb;
tb.Name = string.Format("Node_{0}_{1}", i, j);
Debug.Write(string.Format("Node_{0}_{1}", i, j));
tb.Text = num.ToString();
tb.Location = new Point(j * 50, i * 50);
tb.Width = 30;
tb.Visible = true;
this.splitContainer1.Panel2.Controls.Add(tb);
}
}
}
private void buttonRotate_Click(object sender, EventArgs e)
{
string[,] matrix = new string[matrixSize, matrixSize];
int rotations = (4 + int.Parse(textBoxRotations.Text)) % 4; // note the addition of and mod by 4
switch(rotations)
{
case 1: // rotate clockwise
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrix[j, matrixSize - i - 1] = matrixNodes[i, j].Text;
}
}
break;
case 2: // rotate 180 degrees
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrix[i, j] = matrixNodes[matrixSize - i - 1, matrixSize - j - 1].Text;
}
}
break;
case 3: // rotate counter-clockwise
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrix[i, j] = matrixNodes[j, matrixSize - i - 1].Text;
}
}
break;
default: // do nothing
return;
}
// restore diagonals
for(int i = 0; i < matrixSize; i++)
{
matrix[i, i] = matrixNodes[i, i].Text;
matrix[i, matrixSize - i - 1] = matrixNodes[i, matrixSize - i - 1].Text;
}
// write strings back to text boxes
for (int i = 0; i < matrixSize; i++)
{
for (int j = 0; j < matrixSize; j++)
{
matrixNodes[i, j].Text = matrix[i, j];
}
}
}
}
}
I decided to tackle the issue using a listView instead of a text box, which makes the logic easier for me. Using this method, I was able to think of the matrix as successive boxes. I start on the outside and move in toward the middle, changing the size of my box each time.
Also, rather than using two forms, I use one. At the top I have a textbox where the user enters the size they want the array to be, and a button labeled "Fill" (button2). And at the bottom I have a textbox where the user enters the degree of rotation. When they click "Rotate," it kicks off a process of adding values to linked lists, combining and shifting the list, and then writing back out to the matrix. I'm sure I made it more convoluted than it has to be, but it was a great learning exercise.
After looking over jerry's code above, I think I'm going to look into rectangular arrays. :)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Recycle
{
public partial class Form1 : Form
{
public int size;
public LinkedList<string> topRight = new LinkedList<string>();
public LinkedList<string> bottomLeft = new LinkedList<string>();
public LinkedList<string> myMatrix = new LinkedList<string>();
public LinkedList<string> shiftMatrix = new LinkedList<string>();
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
listView1.Clear();
size = int.Parse(textBox2.Text);
int c = 0;
int q = 0;
int w = 0;
string[] content = new string[size];
Random rnd = new Random();
for (c = 0; c < size; c++)
{
listView1.Columns.Add("", 25);
}
for (q = 0; q < size; q++)
{
for (w = 0; w < size; w++)
{
content[w] = rnd.Next(9,100).ToString();
}
ListViewItem lvi = new ListViewItem(content);
listView1.Items.Add(lvi);
}
}
public bool iseven(int size)
{
if (size % 2 == 0)
{
return true;
}
else
{
return false;
}
}
public void button1_Click(object sender, EventArgs e)
{
if (listView1.Items.Count < 3)
{
MessageBox.Show("Matrix cannot be rotated.");
return;
}
bool even = false;
int shift = int.Parse(textBox1.Text); //amount to shift by
int box = listView1.Items.Count - 1; //size of box
int half = Convert.ToInt32(listView1.Items.Count / 2);
int corner = 0; //inside corner of box
if (shift > listView1.Items.Count)
{
shift = shift % ((listView1.Items.Count - 2) * 4);
}
do
{
eachPass(shift, box, corner);
++corner;
--box;
} while (box >= half + 1);
}
public void eachPass(int shift, int box, int corner)
{
int x;
int i;
//Read each non-diagonal value into one of two lists
for (x = corner + 1; x < box; x++)
{
topRight.AddLast(listView1.Items[corner].SubItems[x].Text);
}
x = box;
for (i = corner + 1; i < box; i++)
{
topRight.AddLast(listView1.Items[i].SubItems[x].Text);
}
for (x = box - 1; x > corner; x--)
{
bottomLeft.AddLast(listView1.Items[box].SubItems[x].Text);
}
x = corner;
for (i = box - 1; i > corner; i--)
{
bottomLeft.AddLast(listView1.Items[i].SubItems[x].Text);
}
string myTest = "";
//join the two lists, so they can be shifted
foreach (string tR in topRight)
{
myMatrix.AddLast(tR);
}
foreach (string bL in bottomLeft)
{
myMatrix.AddLast(bL);
}
int sh;
//shift the list using another list
for (sh = shift; sh < myMatrix.Count; sh++)
{
shiftMatrix.AddLast(myMatrix.ElementAt(sh));
}
for (sh = 0; sh < shift; sh++)
{
shiftMatrix.AddLast(myMatrix.ElementAt(sh));
}
//we need the sizes of the current lists
int trCnt = topRight.Count;
int blCnt = bottomLeft.Count;
//clear them for reuse
topRight.Clear();
bottomLeft.Clear();
int s;
//put the shifted values back
for (s = 0; s < trCnt; s++)
{
topRight.AddLast(shiftMatrix.ElementAt(s));
}
for (s = blCnt; s < shiftMatrix.Count; s++)
{
bottomLeft.AddLast(shiftMatrix.ElementAt(s));
}
int tRn = 0;
int bLn = 0;
//write each non-diagonal value from one of two lists
for (x = corner + 1; x < box; x++)
{
listView1.Items[corner].SubItems[x].Text = topRight.ElementAt(tRn);
++tRn;
}
x = box;
for (i = corner + 1; i < box; i++)
{
listView1.Items[i].SubItems[x].Text = topRight.ElementAt(tRn);
++tRn;
}
for (x = box - 1; x > corner; x--)
{
listView1.Items[box].SubItems[x].Text = bottomLeft.ElementAt(bLn);
++bLn;
}
x = corner;
for (i = box - 1; i > corner; i--)
{
listView1.Items[i].SubItems[x].Text = bottomLeft.ElementAt(bLn);
++bLn;
}
myMatrix.Clear();
shiftMatrix.Clear();
topRight.Clear();
bottomLeft.Clear();
}
}
}

How to add a matrix of OvalShapes to the window?

I want to draw 16*64 matrix, each one containing a:
Microsoft.VisualBasic.PowerPacks.OvalShape.
I used this:
List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape =
new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 64; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = 20;
ovl.Height = 20;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovalShape.Add(ovl);
}
}
How can I show it in the Window?
Creating a separate container for each shape is not required. Also you can skip the additional container list for the shapes. So you could use this very compact code.
var ovalShapes = new Microsoft.VisualBasic.PowerPacks.ShapeContainer()
{
Dock = DockStyle.Fill,
Margin = new Padding(0),
Padding = new Padding(0),
};
for (int i = 0; i < 16; i++)
for (int j = 0; j < 64; j++)
ovalShapes.Shapes.Add(
new Microsoft.VisualBasic.PowerPacks.OvalShape()
{
Width = 20,
Height = 20,
FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid,
FillColor = Color.Green,
Location = new Point(20 * i, 20 * j),
});
this.Controls.Add(ovalShapes);
From MSDN:
An OvalShape control cannot be displayed directly on a form or
container control; it must be contained in a ShapeContainer object.
After you initialize an OvalShape, you will have to set its Parent
property either to an existing ShapeContainer or to a new instance of
ShapeContainer.
Try to set Location and add your controls to the Form:
List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape = new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 64; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = 20;
ovl.Height = 20;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovl.Location = new Point(ovl.Width*i, ovl.Height*j);
ovalShape.Add(ovl);
}
}
foreach(OvalShape os in ovalShape)
{
Microsoft.VisualBasic.PowerPacks.ShapeContainer shapeContainer = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
Control c = new Control();
shapeContainer.Parent = c;
os.Parent = shapeContainer;
myForm.Controls.Add(c);
}
First simplify
int totalCount = 1024; //16*64
const int shapeWidth =20;
const int shapeHeight = 20;
for (int j = 0; j < totalCount; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = shapeWidth;
ovl.Height = shapeHeight;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovalShape.Add(ovl);
}
After pickup your drawing area and decide how much shapes per row you would like to have.
So some hypothetical code could look like this:
int shapesPerRowCount = 5;
int yPos = 0;
for(int i=0;i<ovalShape.Count;i++)
{
if(i % shapesPerRowCount == 0) //reach end of the row, so offset Y position by Height
yPos += shapeHeight;
int xPos = i*shapeWidth;
DrawShapeAtPos(ovalShape[i], xPos, yPos); //some special function that draws the shape
}
A very generic code, but just to provide you an idea.
If it's not what you're searching for, please clarify.

Categories