2015-12-04 2 views
5

Ich versuche, etwas modifizierten Code aus einem MSDN-Artikel als Teil eines Schulprojekts auszuführen. Das Ziel besteht darin, eine Farbmatrix zu verwenden, um eine Bitmap in einer Bildbox neu einzufärben. Hier ist mein Code:C# ColorMatrix Index außerhalb der Grenzen

 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); 

wo Rscale, gScale und bScale sind Schwimmer mit Werten von 0.0f zu 1. Das Original MSDN-Artikel ist hier: https://msdn.microsoft.com/en-us/library/6tf7sa87%28v=vs.110%29.aspx

Wenn es um die letzte Zeile wird nach unten, "ColorMatrix colorMatrix = new ..." mein Code trifft einen Laufzeitfehler. Im Debugger bekomme ich colorMatrixElements als float [4] []. Als wäre es kein 4x4-Array. Habe ich etwas in meinem Copy-Paste-Job verpfuscht, oder verstehe ich einfach nicht, wie C# 2D-Arrays behandelt?

Danke für die Hilfe.

Antwort

4

Für die Seite, die Sie verknüpfen, müssen Sie ein 5 von 5 Array an diesen Konstruktor übergeben. Sie passieren ein 4 mal 4 Array, also erhalten Sie natürlich eine IndexOutOfBoundsException.

Versuchen

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); 
+0

, die gut funktioniert. Es wird ein bisschen manipulieren in dem größeren Schema meines Projekts zu tun, was ich tun muss, aber es kompiliert jetzt. Danke für die Antwort! – Micah