0

I am trying to make an image enlarger that takes 500x500 pixel images and outputs a 1000x1000 pixel image that is exactly the same and with no distortion. I want it to work like this: for each pixel in the original image it should add 4 pixels of the same colour of that pixel into the new image and repeat for each pixel. So one pixel in the original becomes 4 pixels in the new one (4 pixels in a square). I have tried to make this but I cannot get it to work properly. I have written this so far but it just outputs the same size image on a 1000x1000 pixel image in the corner:

        Bitmap oldImg = new Bitmap(Image.FromFile(@"C:\ServerSystem\Testing\20140127T154500.png"));
        Bitmap newImg = new Bitmap(1000, 1000);

        System.Drawing.Imaging.BitmapData data = oldImg.LockBits(new Rectangle(0, 0, 500, 500), System.Drawing.Imaging.ImageLockMode.ReadOnly, oldImg.PixelFormat);
        oldImg.UnlockBits(data); byte[] rgba = new byte[data.Stride * 500];
        System.Runtime.InteropServices.Marshal.Copy(data.Scan0, rgba, 0, data.Stride * 500);

        using (Graphics g = Graphics.FromImage(newImg))
        {
            for (int x = 0; x < 500; x++)
            {
                for (int y = 0; y < 500; y++)
                {
                    newImg.SetPixel(x, y, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
                    newImg.SetPixel(x + 1, y, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
                    newImg.SetPixel(x, y + 1, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
                    newImg.SetPixel(x + 1, y + 1, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
                }
            }
            newImg.Save(@"C:\ServerSystem\Testing\20140127T154500HIRES.png");
        }

How can I make it work properly and get it to create 4 pixels out of 1? Thank you

Henry Hunt
  • 181
  • 5
  • 12
  • 1
    Your indexes in the new image are all wrong. `x` and `y` run from `0` to `499`, so you never draw in anything but the top corner. What you need is `newImg.SetPixel(x*2, y*2, ...)` `(x*2)+1,(y*2)+1` and so on. – Matt Burland Jan 27 '14 at 16:36
  • Also: `exactly the same and with no distortion` - isn't doubling the size of the image a distortion by definition? – Matt Burland Jan 27 '14 at 16:36
  • I mean no distortion as in like when you resize an image and it tries to stretch the pixels over multiple pixels and you get all the fringing and anti-aliasing. I am trying to create an enlarged perfect copy of it. – Henry Hunt Jan 27 '14 at 16:38
  • @MattBurland if you count "0" as a valid index, doesn't that mean you're still setting 500 pixels? – Timothy Groote Jan 27 '14 at 16:38
  • @HenryHunt Why do you copy the pixel data to that byte array if you're not using it? (you won't need it for `GetPixel` and `SetPixel`) – Timothy Groote Jan 27 '14 at 16:39
  • @TimothyGroote: What are you talking about? My comment is basically exactly your answer. – Matt Burland Jan 27 '14 at 16:40
  • @MattBurland I just realised that. forget i said anything :) – Timothy Groote Jan 27 '14 at 16:41

1 Answers1

1

Since you just want to make it twice as big, you were almost there. All you'd have to do is this:

for (int x = 0; x < 500; x++)
{
  for (int y = 0; y < 500; y++)
  {
    newImg.SetPixel(x*2, y*2, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
    newImg.SetPixel(x*2 + 1, y*2, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
    newImg.SetPixel(x*2, y*2 + 1, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
    newImg.SetPixel(x*2 + 1, y*2 + 1, Color.FromArgb(oldImg.GetPixel(x, y).ToArgb()));
  }
}
Timothy Groote
  • 8,614
  • 26
  • 52