Ich lese eine Bitmap über das LockBit-Verfahren ([VB 2010] Tutorial: LockBits) zu einer Auflistung von Bytes ein. Diese konvertiere ich in eine List<Color> um sie besser handhaben zu können. (Konvertierungsmethoden weiter unten)
Anschließend filtere ich die Liste etwas (prüfe auf Farbvorkommen ...). Ich habe also nun eine gekürzte Liste.
Diese möchte ich wieder zu einer Bitmap zusammenfügen. (bzw. ich will was sehen )
Ich habe es auf zwei Arten probiert.
Konvertierungsextensions
Hier auch mal die Methode zum Filtern. Ihre Funktion ist es, zu schauen wo komplett weiße "Streifen" sind. Danach holt sie sich die Farbwerte die zwischen zwei weißen Streifen liegen die nicht direkt untereinander liegen. (Bild im Anhang)
Methode
Hat jemand vielleicht eine Idee wie man die Bytes zu einer Bitmap zusammenfügen kann, oder auch wo sonst ein Fehler liegen könnte ?
Für jegliche Art von Hilfe, auch Anmerkungen für schlechten Codestil(!) ist erwünscht.
Viele Grüße
LaMiy
Anschließend filtere ich die Liste etwas (prüfe auf Farbvorkommen ...). Ich habe also nun eine gekürzte Liste.
Diese möchte ich wieder zu einer Bitmap zusammenfügen. (bzw. ich will was sehen )
Ich habe es auf zwei Arten probiert.
- UnlockBits. Dabei habe ich die Ursprungswerte beim LockBits gelassen und nur die anderen Bytes übergeben. Fehler: "Allgemeiner Fehler in GDI"
C#-Quellcode
- /// <summary>Convert an array of bytes to a bitmap</summary>
- /// <param name="bytes">The input bytearray</param>
- /// <returns></returns>
- public Bitmap UnlockBits(byte[] bytes)
- {
- System.Runtime.InteropServices.Marshal.Copy(bytes, 0, this.ptr, bytes.Count());
- bmp.UnlockBits(bmpData); // Hier tritt der Fehler auf
- return bmp;
- }
- TypeConverter.ConvertFrom() msdn.microsoft.com/de-de/library/ef83h055.aspx Fehler: "Ungültiger Parameter."
C#-Quellcode
- /// <summary>
- /// Represents some useful extensions
- /// </summary>
- static class Extensions
- {
- /// <summary>Convert an list of bytes into a list of colors</summary>
- /// <param name="bytes">The list of bytes</param>
- /// <param name="length">Bytes per pixel</param>
- /// <returns>List of the colors</returns>
- public static List<Color> ToColorList(this List<byte> bytes, int length)
- {
- if (bytes.Count > 0 && (length == 3 || length == 4))
- {
- List<Color> lOutput = new List<Color>();
- // Fill with colorvalues
- for (int counter = 0; counter < bytes.Count(); counter += length)
- {
- // with alphachannel or
- if (length == 3)
- lOutput.Add(Color.FromArgb(bytes[counter + 2], bytes[counter + 1], bytes[counter]));
- else if (length == 4)
- lOutput.Add(Color.FromArgb(bytes[counter + 3], bytes[counter + 2], bytes[counter + 1], bytes[counter]));
- }
- return lOutput;
- }
- else
- throw new ArgumentException();
- }
- /// <summary>Convert a list of color into a list of byte</summary>
- /// <param name="colors">The input list of color</param>
- /// <param name="length">Bytes per pixel</param>
- /// <returns>List of byte</returns>
- public static List<byte> ToByteList(this List<Color> colors, int length)
- {
- if (colors.Count > 0 && (length == 3 || length == 4))
- {
- List<byte> lOutput = new List<byte>();
- for (int i = 0; i < colors.Count; i += length)
- {
- lOutput.Add(colors[i].B);
- lOutput.Add(colors[i + 1].G);
- lOutput.Add(colors[i + 2].R);
- if (length == 4)
- lOutput.Add(colors[i + 3].A);
- }
- return lOutput;
- }
- else
- throw new ArgumentException();
- }
- }
Hier auch mal die Methode zum Filtern. Ihre Funktion ist es, zu schauen wo komplett weiße "Streifen" sind. Danach holt sie sich die Farbwerte die zwischen zwei weißen Streifen liegen die nicht direkt untereinander liegen. (Bild im Anhang)
C#-Quellcode
- /// <summary>
- /// Search for strings in the given color array
- /// </summary>
- /// <param name="bytes">The given colors of the bitmap</param>
- /// <param name="width">Length of one strip</param>
- /// <returns></returns>
- public List<Color> SearchStrings(IEnumerable<Color> color, long width)
- {
- List<Color> lColors = (List<Color>) color;
- List<Stop> lWhiteLines = new List<Stop>();
- List<Color> lOutput = new List<Color>();
- long counter = 0;
- bool newLine = false;
- long start = 0;
- for (int i = 0; i < lColors.Count(); i ++)
- {
- // If new line
- if ((i == 0) || (i % width == 0))
- {
- newLine = true;
- start = i;
- }
- // If a completely white and new line exists
- if (newLine == true)
- {
- // If the actual field is white too
- if (lColors[i].A == 255 && lColors[i].R == 255 && lColors[i].G == 255 && lColors[i].B == 255)
- {
- counter++;
- // If there is a complete white strip
- if (counter == width)
- {
- // Add strip to the list and restart the procedurce
- lWhiteLines.Add(new Stop(start, i - start));
- counter = 0;
- newLine = false;
- }
- }
- else
- {
- newLine = false;
- counter = 0;
- }
- }
- }
- for (int i = 1; i < lWhiteLines.Count - 1; i++)
- {
- // If from the last line to the next is no whitespace
- if (lWhiteLines[i - 1].Position + lWhiteLines[i - 1].Count + 1 != lWhiteLines[i].Position)
- {
- // Here must be something between whiteLines[i] and whitelines[i - 1]
- for (int n = (int) (lWhiteLines[i - 1].Position + lWhiteLines[i - 1].Count); n < lWhiteLines[i].Position + lWhiteLines[i].Count - 1; n++)
- lOutput.Add(lColors[n]);
- }
- }
- return lOutput;
- }
Hat jemand vielleicht eine Idee wie man die Bytes zu einer Bitmap zusammenfügen kann, oder auch wo sonst ein Fehler liegen könnte ?
Für jegliche Art von Hilfe, auch Anmerkungen für schlechten Codestil(!) ist erwünscht.
Viele Grüße
LaMiy