Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create bitmap from int 2D array , using the setPixel method, but when I open the output image using any image viewer, it seems that it is all black

in my code data is a 2D dimensional array of integers with values either 1 or -1

What I have tried:

Bitmap bmp = new Bitmap(80, 80);
                for (int row = 0; row < 80; row++)
                {
                    for (int col = 0; col < 80; col++)
                    {
                        // Parse the value at the current position
                        int val = data[row, col];
                        // Set the pixel color based on the value
                        if (val == 1)
                        {
                            bmp.SetPixel(col, row, Color.White);
                        }
                        else
                        {
                            bmp.SetPixel(col, row, Color.Black);
                        }
                    }
                }

                // Save the output image
                bmp.Save(OutputFileName); 
Posted
Updated 1-May-23 11:12am
Comments
Richard MacCutchan 1-May-23 15:43pm    
Just tried your code and it works fine. You need to look at the values that come from the data array.
Sabry1905 1-May-23 18:17pm    
The Data object is a 2D dimensional array of integers 1 and -1 and I used the debugger to make sure of its content and it is right

and I am not using exception handling keywords
even when I tried to make an array with just all values equal 1 , again the output image comes out black , even I tried before saving the image to check on of the pixel values using Color color = bmp.GetPixel(20, 20); ,it comes black not white !!?
I don't think that the setPixel method is working

I'd start with the debugger and find out exactly what values you are passing to that code fragment - it's fine saying "My data is this" but unless you have absolutely checked using the debugger you can't be sure - because without testing it (and I'm not in position to do that ATM) that code looks like it should work.
 
Share this answer
 
Comments
Sabry1905 1-May-23 18:19pm    
The Data object is a 2D dimensional array of integers 1 and -1 and I used the debugger to make sure of its content and it is right

and I am not using exception handling keywords
even when I tried to make an array with just all values equal 1 , again the output image comes out black , even I tried before saving the image to check on of the pixel values using Color color = bmp.GetPixel(20, 20); ,it comes black not white !!?
I don't think that the setPixel method is working
Sabry1905 1-May-23 19:03pm    
I apologize you were right, the method is working, there was some wrong logic in reading the data from the txt files, I discovered it while debugging , again thank you for your time, appreciated
OriginalGriff 2-May-23 0:48am    
You're welcome!
The code you've posted will generate a bitmap SO LONG AS you have a valid data array with 2 dimensions, of at least 80 rows and 80 columns.

If you're not getting an exception, you are probably running this code from inside a try/catch block that is swallowing the exception, preventing you from even knowing about a severe problem with your code.
 
Share this answer
 
Comments
Sabry1905 1-May-23 17:13pm    
The Data object is a 2D dimensional array of integers 1 and -1 and I used the debugger to make sure of its content and it is right

and I am not using exception handling keywords
Dave Kreskowiak 1-May-23 17:20pm    
Well, the code you posted works. The only possible failures are the data coming into the code and/or the filename you're supplying to the Save method.
Sabry1905 1-May-23 18:18pm    
even when I tried to make an array with just all values equal 1 , again the output image comes out black , even I tried before saving the image to check on of the pixel values using Color color = bmp.GetPixel(20, 20); ,it comes black not white !!?
I don't think that the setPixel method is working
Dave Kreskowiak 1-May-23 18:24pm    
AGAIN, you either VERIFY exactly what's in the data array under the debugger, or you're not going to get this solved. Period. End of story.
Sabry1905 1-May-23 18:32pm    
I checked the content of the data object under the debugger , the data are correct

I tried to post a screenshot in the reply to convince you that I did, but unfortunately I wasn't able to do it
The Data object is a 2D dimensional array of integers 1 and -1 and I used the debugger to make sure of its content and it is right

and I am not using exception handling keywords
 
Share this answer
 

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