C # ColorMatrix Index out of bounds

I am trying to run a little modified code from an MSDN article as part of a school project. The goal is to use colormatrix to repaint the bitmap in the image window. Here is my code:

float[][] colorMatrixElements = { new float[] {rScale, 0, 0, 0}, new float[] {0, gScale, 0, 0}, new float[] {0, 0, bScale, 0}, new float[] {0, 0, 0, 1}}; ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements); 

where rScale, gScale and bScale are float with values ​​from 0.0f to 1. The original MSDN article is here: https://msdn.microsoft.com/en-us/library/6tf7sa87%28v=vs.110%29.aspx

When it moves to the last line, "ColorMatrix colorMatrix = new ...", my code gets into a runtime error. In the debugger, I get colorMatrixElements as float [4] []. Like it's not a 4x4 array. I tried something in my work for copy-paste, or I just don’t understand how C # handles 2D arrays?

Thanks for the help.

+5
source share
1 answer

On the very page that you are linking to, you need to pass a 5 by 5 array to this constructor. You pass a 4 by 4 array, so naturally you get an IndexOutOfBoundsException .

Try

  float[][] colorMatrixElements = { new float[] {rScale, 0, 0, 0, 0}, new float[] {0, gScale, 0, 0, 0}, new float[] {0, 0, bScale, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} }; ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements); 
+4
source

Source: https://habr.com/ru/post/1237396/


All Articles