Exception und Verlorene Referenz
-
Hallo Gemeinde,
Ich schreibe eine Bildbearbeitungssoftware.
Als erstes möchte ich Filter anwenden, den Mittelwertfilter.
Das klappt auch ganz gut ABER ich habe Probleme mit der "Randbetrachtung".
Ich benutze eine Maske von 3x3 Pixeln. Wenn ich nun auf das erste Pixel zugreife und die lokale Umgebung auslese ist ja klar das 5 von 9 Pixeln ausserhalb des Bildes liegen. Dies fange ich über einetry...catch(Exception)
ab.
Nun meine Fragen:
1. Anstatt der Exception möchte ich etwas Passenderes nehmen, wie zum Beispiel IndexOutOfBoundsExcetion. Geht das?2. Irgendwann "verliere" ich das Bild. Soll heißen beim debuggen ist mir aufgefallen, das nach einiger Zeit mein Bitmap "leer" ist. Es ist kein Verweis mehr auf das Bild vorhanden.
Kennt jemand dieses Phänomen und wie löse ich es.
(Hinweis: Wenn x und y so gewählt werden, das keine Exception ausgelößt werden muss, weil ich nicht die Arraygrenzen des Bildes verlasse klappt diese Funktion einwandfrei. Es muss also etwas mit try{}Catch(){} zu tun haben)Der gasamte Code der Funktion lautet:
public struct MaskChannels { public byte Red; public byte Green; public byte Blue; } protected Size maskSize = new Size(3, 3); protected void FillMask(int x, int y, Size maskSize) { int tmp_x=0, tmp_y=0; this.sum.Red = 0; this.sum.Green = 0; this.sum.Blue = 0; for(int j=-(maskSize.Width/2); j<=(maskSize.Width/2); j++) { tmp_y = 0; for(int k=-(maskSize.Height/2); k<=(maskSize.Height/2); k++) { try { this.mask[tmp_x, tmp_y].Red = this.image.GetPixel(x+j, y+k).R; this.sum.Red += this.mask[tmp_x, tmp_y].Red; this.mask[tmp_x, tmp_y].Green = this.image.GetPixel(x+j, y+k).G; this.sum.Green += this.mask[tmp_x, tmp_y].Green; this.mask[tmp_x, tmp_y].Blue = this.image.GetPixel(x+j, y+k).B; this.sum.Blue += this.mask[tmp_x, tmp_y].Blue; } catch(Exception) { MaskChannels colorValue; #region Randbetrachtung switch(randMode) { case Randbetrachtung.LAST_VALUE: colorValue.Red = this.image.GetPixel(x, y).R; colorValue.Green = this.image.GetPixel(x, y).G; colorValue.Blue = this.image.GetPixel(x, y).B; break; case Randbetrachtung.VALUE_127: colorValue.Red = 127; colorValue.Green = 127; colorValue.Blue = 127; break; case Randbetrachtung.ZERO_PADDING: colorValue.Red = 0; colorValue.Green = 0; colorValue.Blue = 0; break; default: colorValue.Red = 0; colorValue.Green = 0; colorValue.Blue = 0; break; } #endregion this.mask[tmp_x, tmp_y].Red = colorValue.Red; this.sum.Red += colorValue.Red; this.mask[tmp_x, tmp_y].Green = colorValue.Green; this.sum.Green += colorValue.Green; this.mask[tmp_x, tmp_y].Blue = colorValue.Blue; this.sum.Blue += colorValue.Blue; } tmp_y++; } tmp_x++; } }
Vielen Dank für eure Zeit.
Ich hoffe sehr das Ihr mir helfen könnt.
-
Eigene Exception kannste Dir basteln:
namespace myException { public class MyException : System.ApplicationException { public class MyException(string message): base message { } } // irgendwelcher Code in einer anderen Klasse catch (MyException ex) { Console.Writeline("MyException Message: {0}", ex.message); }