Reputation: 147
I have a Matrix [y][x] with x= arround 5000 and y= arround 13 values in it. And i need to mark them in different colours (depending on the value) on an Image.
My Problem: The Image is much bigger than the Matrix. If i Colour 1 Pixel for 1 Value i only get 1/3 in X and only like 1/20 in y painted. But i need to Paint the whole Image with the amount of Values i have.
Solution i tried: Marking multiple Pixels at once with each Value Problem: I can not scale X and Y at the same time -> way to complicated for me :/
Solution i can think about: Scaling the Image to the exact Size of the Matrix, then painting the pixels (1 pixel = 1 value) and after that resize the Drawn Image to a Size that it is visible.
It would be great if you could help me here, maybe you have an even better solution for this Problem, than i can think off... Note: I am a newby on C#.
My current try: (marking 5 pixels with each value of the matrix)
Bitmap image1;
image1 = new Bitmap(@"C:\Users\Downloads\test.bmp", true);
Bitmap newImage = new Bitmap(image1.Width, image1.Height);
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.DrawImage(image1, 0, 0);
}
Color pixelColor = newImage.GetPixel(0, 0); //set pixel color to white
int x, y; //loop for matrix
double akt_Wert = 0;
int x1=0, y1=0; //loop for picture
for (x = 0; x < max_Col; x++) /
{
for (y = max_Rows - 1; y >= 0; y--)
{
try
{
akt_Wert = Convert.ToDouble(rows[y][x]); //tries to convert the current Value to double, if it fails its NaN -> catch
if (akt_Wert < 0.7) //if < threshold
{
for (x1 = x * 5; x1 < x*5+5; x1++)
{
Color newColor = Color.FromArgb(pixelColor.R, 50, 50);
newImage.SetPixel(x1, y1, newColor);
}
}
if (akt_Wert >= 0.7) //if >= threshold
{
for (x1 = x * 5; x1 < x*5+5; x1++)
{
Color newColor = Color.FromArgb(pixelColor.B, 10, 0);
newImage.SetPixel(x, y, newColor);
}
}
}
catch //value is NaN
{
//MessageBox.Show("Spalte = NaN");
}
}
pictureBox1.Image = newImage; // Set the PictureBox to display the image.
System.Threading.Thread.Sleep(5); //needed to avoid error
}
}
Thanks in advance! Regards Christian
Upvotes: 2
Views: 1475
Reputation: 54453
Here is an example.
Note the use of floats
to avoid data loss by integer division.
Also note that I create a semi-transparent brush color so we can see the original image shine through..
Bitmap b = (Bitmap)Image.FromFile(fileName);
// the data array sizes:
int numX = 3000;
int numY = 30;
int[,] data = new int[numX, numY];
// create test data:
Random rnd = new Random(8);
for (int i = 0; i < data.GetLength(0); i++)
for (int j = 0; j < data.GetLength(1); j++)
data[i, j] = rnd.Next(123456);
// scale the tile size:
float sx = 1f * b.Width / data.GetLength(0);
float sy = 1f * b.Height / data.GetLength(1);
// now fill the tile-pixels
using (Graphics g = Graphics.FromImage(b))
{
for (int x = 0; x < data.GetLength(0); x++)
for (int y = 0; y < data.GetLength(1); y++)
{
RectangleF r = new RectangleF(x * sx, y* sy, sx, sy);
Color c = Color.FromArgb(99, Color.FromArgb(data[x, y]));
using (SolidBrush brush = new SolidBrush(c))
g.FillRectangle(brush, r);
}
// display or save or whatever..
pictureBox1.Image = b;
}
If you actually want to create a new Image you can use g.Clear(someColor)
to set its backcolor. And, of course you would create it not from a file but from scratch: Bitmap b = new Bitmap(1234,1234);
; maybe include a special PixelFormat
setting here.
Upvotes: 1