Hej udviklere!
Jeg har et program der skal kunne
pixel'ere et billede.
Har fundet en kode på nettet, og den melder ingen fejl. Det er en
private static:
- private static Bitmap Pixelate(Bitmap image, Rectangle rectangle, Int32 pixelateSize)
- {
- Bitmap pixelated = new System.Drawing.Bitmap(image.Width, image.Height);
-
- // make an exact copy of the bitmap provided
- using (Graphics graphics = System.Drawing.Graphics.FromImage(pixelated))
- graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
- new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
-
- // look at every pixel in the rectangle while making sure we're within the image bounds
- for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx += pixelateSize)
- {
- for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy += pixelateSize)
- {
- Int32 offsetX = pixelateSize / 2;
- Int32 offsetY = pixelateSize / 2;
-
- // make sure that the offset is within the boundry of the image
- while (xx + offsetX >= image.Width) offsetX--;
- while (yy + offsetY >= image.Height) offsetY--;
-
- // get the pixel color in the center of the soon to be pixelated area
- Color pixel = pixelated.GetPixel(xx + offsetX, yy + offsetY);
-
- // for each pixel in the pixelate size, set it to the center color
- for (Int32 x = xx; x < xx + pixelateSize && x < image.Width; x++)
- for (Int32 y = yy; y < yy + pixelateSize && y < image.Height; y++)
- pixelated.SetPixel(x, y, pixel);
- }
- }
-
- return pixelated;
Har også en knap der skal affyre den. Men hvordan? Hvad skal jeg helt præcis skrive i knappen?
- private void pixelateButton_Click(object sender, EventArgs e)
- {
-
- }