简体   繁体   中英

C# Crop transparent parts in an image


I have this image:

在此处输入图像描述

and I wrote a code that supposed to crop only the part with the black dots
(the code is built for 1-color images only),
without all of the transparent pixels around the dots,
and then return the image after the crop,
but for some reason, when it gets to a black pixel,
it does not recognize that this is a black pixel,
and because of that, it skips the 'if' statement.

Here is the code:

    private Image cropTransparency(Image image)
    {
        Bitmap imageCrop = new Bitmap(image);
        imageCrop.Save(@"C:\Users\Nehoray\Desktop\Test.png");
        Point min = new Point(imageCrop.Width, imageCrop.Height);
        Point max = new Point(imageCrop.Width, imageCrop.Height);
        for (int w = 0; w < imageCrop.Width; w++)
        {
            //'w' stands for Width
            for (int h = 0; h < imageCrop.Height; h++)
            {
                //'h' stands for Height
                Color check = imageCrop.GetPixel(w, h);
                if (check == Color.Black)
                {
                    MessageBox.Show("Found a white pixel!");
                    if (w < min.X)
                    {
                        min.X = w;
                    }
                    if (h < min.Y)
                    {
                        min.Y = h;
                    }
                    if (w > max.X)
                    {
                        max.X = w;
                    }
                    if (h > max.Y)
                    {
                        max.Y = h;
                    }
                }
            }
        }
        imageCrop = new Bitmap(max.X - min.X, max.Y - min.Y);
        Graphics g = Graphics.FromImage(imageCrop);
        Rectangle cropRect = new Rectangle(new Point(0, 0), new Size(max.X - min.X, max.Y - min.Y));
        g.DrawImage(image, new Rectangle(0, 0, max.X - min.X, max.Y - min.Y), cropRect, GraphicsUnit.Pixel);
        g.Save();
        return imageCrop;
    }

If you find out why it does not recognize when there is a black pixel, please let me know..
thanks anyway :)

There are quite a few issues with this code:

    Point max = new Point(imageCrop.Width, imageCrop.Height);

How will a point ever be greater than the max, when the max is initialised to the maximum value? This should be (0,0)

Color check = imageCrop.GetPixel(w, h);
if (check == Color.Black)

I'm not sure this does what you think it will. You have a 32-bit image, with an alpha channel, so you need to take the alpha values into account. Also, you're comparing against a predefined colour which has a reference that won't match your pixel even if all 4 channels are a match. You possibly just want to check for the alpha component being non-zero. If you only compare the colour channels, be aware that transparent pixels may well have a matching colour, producing unexpected results.

Rectangle cropRect = new Rectangle(new Point(0, 0), new Size(max.X - min.X, max.Y - min.Y));

Why are you cropping from 0,0 ? Your rectangle should begin at min.X, min.Y

g.Save();

This doesn't save the image, you know that right? You save the image, unmodified at the start of your code, and then never re-save it once you've cropped it (I assume this stuff, including the hard-coded path, is for debug, but even then it seems you probably meant to write the image here)

You are comparing: (check == Color.Black) which means: is the reference check pointing to the same instance as the reference Color.Black --> this will never be true.

you have to compare the actual color:

(check.ToArgb() == Color.Black.ToArgb())
private Image cropTransparency(Image image)
    {
        Bitmap imageCrop = new Bitmap(image); // aca paso la imagen original a Bitmap
        //imageCrop.Save(@"tmp.png");
        Point min = new Point(imageCrop.Width, imageCrop.Height);
        Point max = new Point(0, 0);
        for (int w = 0; w < imageCrop.Width; w++)
        {
            //'w' stands for Width
            for (int h = 0; h < imageCrop.Height; h++)
            {
                //'h' stands for Height
                Color check = imageCrop.GetPixel(w, h);
                if (check == Color.FromArgb(255, 0, 0, 0))
                {
                    Console.WriteLine("Found a white pixel!");
                    if (w < min.X)
                    {
                        min.X = w;
                    }
                    if (h < min.Y)
                    {
                        min.Y = h;
                    }
                    if (w > max.X)
                    {
                        max.X = w;
                    }
                    if (h > max.Y)
                    {
                        max.Y = h;
                    }
                }
            }
        }
        imageCrop = new Bitmap(max.X - min.X, max.Y - min.Y);
        Graphics g = Graphics.FromImage(imageCrop);
        Rectangle cropRect = new Rectangle(new Point(min.X,min.Y), new Size(max.X - min.X, max.Y - min.Y));
        g.DrawImage(image, new Rectangle(0, 0, max.X - min.X, max.Y - min.Y), cropRect, GraphicsUnit.Pixel);
        g.Save();
        return imageCrop;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM