1

usage

public UserControlTester()
        {
            InitializeComponent();

            GetSetPixelsRandom();
        }

code

private void GetSetPixelsRandom()
        {
            Bitmap img = new Bitmap(@"d:\\downloaded images\\test.gif");
            for (int i = 0; i < img.Width; i++)
            {
                for (int j = 0; j < img.Height; j++)
                {
                    Color pixel = img.GetPixel(i, j);
                    pixels.Add(pixel);
                }
            }

            Bitmap img1 = new Bitmap(512,512);

            for (int i = 0; i < img1.Width; i++)
            {
                for (int j = 0; j < img1.Height; j++)
                {
                    for (int c = 0; c < pixels.Count; c++)
                    {
                        img1.SetPixel(i, j, pixels[c]);
                    }
                }
            }

            img1.Save(@"d:\\downloaded images\\test3.gif");
        }

Not sure if this is the right way. the inner loop with the variable c take a lot of time because there are more then 290,000 pixels.

BenBen Shmil
  • 437
  • 6
  • 16
  • 3
    Does this answer your question? [Fast work with Bitmaps in C#](https://stackoverflow.com/questions/1563038/fast-work-with-bitmaps-in-c-sharp) –  Nov 26 '22 at 10:23

1 Answers1

0

Assuming you want to resize the image, this can be done easily. The Bitmap constructor has rescaling build in:

Bitmap img = new Bitmap(@"d:\\downloaded images\\test.gif");
Bitmap img1 = new Bitmap(img, new Size(512, 512));
img1.Save(@"d:\\downloaded images\\test3.gif");

If you do not want to resize but indeed take random pixels take a look at what at MickyD mentioned.

Note that in your provided example every pixel in the new image is effectively set to the last pixel in the pixels list because you loop over every pixel in pixels for every pixel in the new image.