MyNick
MyNick

Reputation: 566

C# Load scaled bitmap

Im trying to load a n by n image and make in nm by nm by duplicating each pixel to be m x m. What i mean: if the image was:

1 2

3 4

and m is 2 so the new image will be

1 1 2 2

1 1 2 2

3 3 4 4

3 3 4 4

So far i just done the obvious way:

Bitmap scaledImage = new Bitmap(image.Width * BeadWidth, image.Height * BeadHeight);
  for (int w = 0; w < scaledImage.Width; ++w) {
      for (int h = 0; h < scaledImage.Height; ++h) {
          scaledImage.SetPixel(w, h, image.GetPixel(w / BeadWidth, h / BeadHeight));
     }
  }

but it take such a long time. How can i get the same result in a much faster time?

Upvotes: 1

Views: 94

Answers (2)

Abdullah
Abdullah

Reputation: 637

DrawImage(Image, Int32, Int32, Int32, Int32)
Draws the specified Image at the specified location and with the specified size.

Bitmap bmp = new Bitmap(width*2, height*2);
Graphics graph = Graphics.FromImage(bmp);
graph.InterpolationMode = InterpolationMode.High;
graph.CompositingQuality = CompositingQuality.HighQuality;
graph.SmoothingMode = SmoothingMode.AntiAlias;
graph.DrawImage(image, new Rectangle(0, 0, width*2, height*2));

While this will be high quality, I don't think he actually wants anti-aliased, interpolating results. If that is so, the correct settings would be:

graph.InterpolationMode = InterpolationMode.NearestNeighbor; 
graph.SmoothingMode = SmoothingMode.None;

Upvotes: 2

Ivan Stoev
Ivan Stoev

Reputation: 205849

You can achieve that by simply using the following Bitmap Constructor (Image, Int32, Int32) overload like this

var scaledImage = new Bitmap(image, image.Width * BeadWidth, image.Height * BeadHeight);

Upvotes: 0

Related Questions