简体   繁体   中英

How can I save the location of every single pixel of my image in c#?

I am writing a programme that I need to save the location of every single pixel in my bitmap image in an array and later on in need to for example randomly turn off 300 of black pixels randomly. However I am not sure how to do that. I have written the following code but of course it does not work. Can anyone please tell me the right way of doing that?

The locations of every pixel are constant (every pixel has exactly one x and one y coordinate) so the requirement of save the location of every single pixel is vague.

I guess what you try to do is: Turn 300 pixels in an image black, but save the previous color so you can restore single pixels?

You could try this:

class PixelHelper
{
    public Point Coordinate;
    public Color PixelColor;
}

PixelHelper[] pixelBackup = new PixelHelper[300];

Random r = new Random();
for (int i = 0; i < 300; i++)
{
    int xRandom = r.Next(bmp.Width);
    int yRandom = r.Next(bmp.Height);

    Color c = bmp.GetPixel(xRandom, yRandom);
    PixelHelper[i] = new PixelHelper() { Point = new Point(xRandom, yRandom), PixelColor = c };
}

After that the pixelBackup array contains 300 objects that contain a coordinate and the previous color.


EDIT: I guess from the comment that you want to turn 300 random black pixels white and then save the result as an image again?

Random r = new Random();

int n = 0;
while (n < 300)
{
    int xRandom = r.Next(bmp.Width);
    int yRandom = r.Next(bmp.Height);

    if (bmp.GetPixel(xRandom, yRandom) == Color.Black)
    {
        bmp.SetPixel(xRandom, yRandom, Color.White);
        n++;
    }
}

bmp.Save(<filename>);

This turns 300 distinct pixels in your image from black to white. The while loop is used so I can increase n only if a black pixel is hit. If the random coordinate hits a white pixel, another pixel is picked.

Please note that this code loops forever in case there are less than 300 pixels in your image in total.

The following will open an image into memory, and then copy the pixel data into a 2d array. It then randomly converts 300 pixels in the 2d array to black. As an added bonus, it then saves the pixel data back into the bitmap object, and saves the file back to disk.

I edited the code to ensure 300 distinct pixels were selected.

        int x = 0, y = 0;

        ///Get Data
        Bitmap myBitmap = new Bitmap("mold.jpg");
        Color[,] pixelData = new Color[myBitmap.Width, myBitmap.Height];

        for (y = 0; y < myBitmap.Height; y++)
            for (x = 0; x < myBitmap.Width; x++)
                pixelData[x,y] = myBitmap.GetPixel(x, y);


        ///Randomly convert 3 pixels to black
        Random rand = new Random();
        List<Point> Used = new List<Point>();  

        for (int i = 0; i < 300; i++)
        {
            x = rand.Next(0, myBitmap.Width);
            y = rand.Next(0, myBitmap.Height);

            //Ensure we use 300 distinct pixels
            while (Used.Contains(new Point(x,y)) || pixelData[x,y] != Color.Black)
            {
                x = rand.Next(0, myBitmap.Width);
                y = rand.Next(0, myBitmap.Height);
            }
            Used.Add(new Point(x, y));   //Store the pixel we have used

            pixelData[x, y] = Color.White;
        }


        ///Save the new image
        for (y = 0; y < myBitmap.Height; y++)
            for (x = 0; x < myBitmap.Width; x++)
                myBitmap.SetPixel(x, y, pixelData[x, y]);

        myBitmap.Save("mold2.jpg");

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