Laden...

Bildbearbeitung LockBits --> Bild ist Rot, Grün und Blau gestreift

Erstellt von slayerofyourmind vor 8 Jahren Letzter Beitrag vor 8 Jahren 2.034 Views
S
slayerofyourmind Themenstarter:in
10 Beiträge seit 2014
vor 8 Jahren
Bildbearbeitung LockBits --> Bild ist Rot, Grün und Blau gestreift

Guten Tag,

ich habe ein Problem in meiner Anwendung.

Ich möchte alle schwarzen Pixel in meiner Zeichnung umfärben.
Da die Methoden: "Bitmap.GetPixel" und "Bitmap.SetPixel" sehr langsam sind, hatte ich mir den Beitrag: Work with bitmaps faster in C# auf CodeProject angesehen.

Ich habe die gesamte Klasse: "LockBitmap" übernommen, und auch so verwendet, wie er es unten zeigt.

Hier ist mein Code:


Bitmap bmp = new Bitmap(@"C:\Users\Anwender\Pictures\Processing\Text.png");

                LockBitmap lBitmap = new LockBitmap(bmp);
                lBitmap.LockBits();

                for (int x = 0; x < lBitmap.Width; x++)
                {
                    for (int y = 0; y < lBitmap.Height; y++)
                    {
                            if(lBitmap.GetPixel(x, y).ToArgb() == Color.Black.ToArgb())
                                lBitmap.SetPixel(x, y, Color.Green);
                    }
                }

                lBitmap.UnlockBits();

                bmp.Save(@"C:\Users\Anwender\Pictures\Processing\End.png", ImageFormat.Png);

Die Klasse LockBitmap:


public class LockBitmap
    {
        Bitmap source = null;
        IntPtr Iptr = IntPtr.Zero;
        BitmapData bitmapData = null;

        public byte[] Pixels { get; set; }
        public int Depth { get; private set; }
        public int Width { get; private set; }
        public int Height { get; private set; }

        public LockBitmap(Bitmap source)
        {
            this.source = source;
        }

        /// <summary>
        /// Lock bitmap data
        /// </summary>
        public void LockBits()
        {
            try
            {
                // Get width and height of bitmap
                Width = source.Width;
                Height = source.Height;

                // get total locked pixels count
                int PixelCount = Width * Height;

                // Create rectangle to lock
                Rectangle rect = new Rectangle(0, 0, Width, Height);

                // get source bitmap pixel format size
                Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);

                // Check if bpp (Bits Per Pixel) is 8, 24, or 32
                if (Depth != 8 && Depth != 24 && Depth != 32)
                {
                    throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
                }

                // Lock bitmap and return bitmap data
                bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
                                             source.PixelFormat);

                // create byte array to copy pixel values
                int step = Depth / 8;
                Pixels = new byte[PixelCount * step];
                Iptr = bitmapData.Scan0;

                // Copy data from pointer to array
                Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Unlock bitmap data
        /// </summary>
        public void UnlockBits()
        {
            try
            {
                // Copy data from byte array to pointer
                Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);

                // Unlock bitmap data
                source.UnlockBits(bitmapData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get the color of the specified pixel
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public Color GetPixel(int x, int y)
        {
            Color clr = Color.Empty;

            // Get color components count
            int cCount = Depth / 8;

            // Get start index of the specified pixel
            int i = ((y * Width) + x) * cCount;

            if (i > Pixels.Length - cCount)
                throw new IndexOutOfRangeException();

            if (Depth == 32) // For 32 bpp get Red, Green, Blue and Alpha
            {
                byte b = Pixels[i];
                byte g = Pixels[i + 1];
                byte r = Pixels[i + 2];
                byte a = Pixels[i + 3]; // a
                clr = Color.FromArgb(a, r, g, b);
            }
            if (Depth == 24) // For 24 bpp get Red, Green and Blue
            {
                byte b = Pixels[i];
                byte g = Pixels[i + 1];
                byte r = Pixels[i + 2];
                clr = Color.FromArgb(r, g, b);
            }
            if (Depth == 8)
            // For 8 bpp get color value (Red, Green and Blue values are the same)
            {
                byte c = Pixels[i];
                clr = Color.FromArgb(c, c, c);
            }
            return clr;
        }

        /// <summary>
        /// Set the color of the specified pixel
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="color"></param>
        public void SetPixel(int x, int y, Color color)
        {
            // Get color components count
            int cCount = Depth / 8;

            // Get start index of the specified pixel
            int i = ((y * Width) + x) * cCount;

            if (Depth == 32) // For 32 bpp set Red, Green, Blue and Alpha
            {
                Pixels[i] = color.B;
                Pixels[i + 1] = color.G;
                Pixels[i + 2] = color.R;
                Pixels[i + 3] = color.A;
            }
            if (Depth == 24) // For 24 bpp set Red, Green and Blue
            {
                Pixels[i] = color.B;
                Pixels[i + 1] = color.G;
                Pixels[i + 2] = color.R;
            }
            if (Depth == 8)
            // For 8 bpp set color value (Red, Green and Blue values are the same)
            {
                Pixels[i] = color.B;
            }
        }
    }

Könnt Ihr erkennen was mein Fehler ist?

Im Anhang befindet sich das Bild, welches am Ende herauskommt.

Mit freundlichen Grüßen
Slayerofyourmind

J
251 Beiträge seit 2012
vor 8 Jahren

[Artikel] Debugger: Wie verwende ich den von Visual Studio?

Das heißt, du nimmst von i-wem dem Quellcode und willst von uns nu ne Analyse? 😕


if(lBitmap.GetPixel(x, y).ToArgb() == Color.Black.ToArgb())
                                lBitmap.SetPixel(x, y, Color.Green);

Sieht theo. erstmal richtig aus, wenn man es liest (wenn schwarz, dann grün) mit dem Debugger kannst du gern schauen, ob es stimmt, was er da macht

[Edit] Eign ist es schön, dass du uns das Ergebnis => Schwarzes Bild zeigst... Aber was war es vorher? Vom Zustand vor dem Aufruf bis nach dem Aufruf, könntest du doch theo. interpretieren, was dein Code macht [/Edit]

[Edit2]
pixelColor Abfrage beschleunigen?
bei dem Stichwort Performance und das am selben Tag 😃[/Edit2]

3.170 Beiträge seit 2006
vor 8 Jahren

Hallo,

wenn ich es richtig sehe beachtest Du den Stride nicht. Das ist sozusagen die "eigentliche" Länge einer Zeile in der Bitmap - die Zeilen werden ggf. hinten mit einigen bytes aufgefüllt.

Schu Dir an wie das in GetPixel und SetPixel um Längen geschlagen. 800 mal schneller im zweiten Codeblock behandelt wird.

Gruß, MarsStein

Non quia difficilia sunt, non audemus, sed quia non audemus, difficilia sunt! - Seneca