none
Painting a WriteableBitmap pixel by pixel

    Question

  • I'm trying to modify some example code. I have a large 2D array of ints that is the exact size of my canvas / screen.

    If the number in a particular element is negative I wan to draw a blue pixel at that location. If it's positive, I want to draw a Red pixel at that location. Later, I will change this for different shades of red and blue depending on the value. Here's the code:

           public static void DrawROIColors()
            {
         
                const int width = 1155;
                const int height = 805;
    
                WriteableBitmap wbitmap = new WriteableBitmap(
                    width, height, 96, 96, PixelFormats.Bgra32, null);
                byte[,,] pixels = new byte[height, width, 4];
    
            //    ((MainWindow)Application.Current.MainWindow).ROICanvas.Children.Clear();
                for (int x = 0; x < 1155; x++)
                {
                    for (int y = 0; y < 805; y++)
                    {
                        if (ROI.ROIMap[x, y] > 0) // Red
                        {
                            pixels[x, y, 2] = 255;
    
                        }
                        else if (ROI.ROIMap[x, y] < 0) // Blue
                        {
                            pixels[x, y, 0] = 255;
                        }
                    }// y
                }// x
    
             
                // Copy the data into a one-dimensional array.
                byte[] pixels1d = new byte[height * width * 4];
                int index = 0;
                for (int row = 0; row < height; row++)
                {
                    for (int col = 0; col < width; col++)
                    {
                        for (int i = 0; i < 4; i++)
                            pixels1d[index++] = pixels[row, col, i];
                    }
                }
    
                // Update writeable bitmap with the colorArray to the image.
                Int32Rect rect = new Int32Rect(0, 0, width, height);
                int stride = 4 * width;
                wbitmap.WritePixels(rect, pixels1d, stride, 0);
    
                // Create an Image to display the bitmap.
                System.Windows.Controls.Image image = new System.Windows.Controls.Image();
                image.Stretch = Stretch.None;
                image.Margin = new Thickness(0);
    
    
                ((MainWindow)Application.Current.MainWindow).ROICanvas.Children.Add(image);
    
             //   ((MainWindow)Application.Current.MainWindow).ROIImage.Source = wbitmap;
             
            }

    It's throwing this error: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

    I suspect that it's happening where I assign the color values to pixels.H

    Help would be appreciated! Thanks!

    Friday, May 24, 2019 8:09 PM

Answers

  • Try several modifications:

       byte[,,] pixels = new byte[width, height, 4];

       . . .

       pixels1d[index++] = pixels[col, row, i];

     

    Also add:

       pixels[x, y, 3] = 255;

       . . .

       image.Source = wbitmap;

    • Marked as answer by Zetar Saturday, May 25, 2019 12:49 PM
    Saturday, May 25, 2019 8:18 AM

All replies

  • Try several modifications:

       byte[,,] pixels = new byte[width, height, 4];

       . . .

       pixels1d[index++] = pixels[col, row, i];

     

    Also add:

       pixels[x, y, 3] = 255;

       . . .

       image.Source = wbitmap;

    • Marked as answer by Zetar Saturday, May 25, 2019 12:49 PM
    Saturday, May 25, 2019 8:18 AM
  • First, thanks! I vaguely remember something about X & Y (Row and Col) reversed. I haven't done this in a couple of years.

    Second, I'm only getting black, no red or blue. And I want to be able to adjust the intensity of the red and blue based on the values in 2D array. How would I change this call:

                            pixels[x, y, 3] = 255;

    For a standard RGB call. Even better would be if I could set alpha, too.

    Thanks, again!

    Saturday, May 25, 2019 12:51 PM
  • I think that pixels[x,y,3] is the alpha value, between 0 (transparent) and 255 (opaque).

    Also set pixels[x,y,2] (Red), pixels[x,y,1] (Green) and pixels[x,y,0] (Blue), and the colour that corresponds to ‘ROI.ROIMap[x, y] == 0’.

    Saturday, May 25, 2019 1:26 PM
  • You are absolutely correct; pixels[x, y, 3] = 125; clearly sets the alpha to half transparent.

    So, I assume that pixels[x, y, 0] sets the Blue value, pixels[x, y, 1] sets the Green value and pixels[x, y, 2] sets the red value. Again, RGB is backwards from what I expected. I do seem to remember that now (row, columns and RGB values are all backwards from what I expected).

    Again, thanks!

    Saturday, May 25, 2019 2:31 PM