485 {
486
487 Bitmap resized =
new Bitmap(targetWidth, targetHeight, original.PixelFormat);
488
489 int originalWidth = original.Width;
490 int originalHeight = original.Height;
491 float xRatio = originalWidth / (float)targetWidth;
492 float yRatio = originalHeight / (float)targetHeight;
493
494
495
496 for (int y = 0; y < targetHeight; y++)
497 {
498 for (int x = 0; x < targetWidth; x++)
499 {
500 float gx = ((float)x) * xRatio;
501 float gy = ((float)y) * yRatio;
502 int gxi = (int)gx;
503 int gyi = (int)gy;
504
505 ColorS topLeft = original.GetPixel(gxi, gyi);
506 ColorS topRight = original.GetPixel(gxi + 1, gyi);
507 ColorS bottomLeft = original.GetPixel(gxi, gyi + 1);
508 ColorS bottomRight = original.GetPixel(gxi + 1, gyi + 1);
509
510 ushort red = (ushort)BilinearInterpolate(gx - gxi, gy - gyi, topLeft.R, topRight.R, bottomLeft.R, bottomRight.R);
511 ushort green = (ushort)BilinearInterpolate(gx - gxi, gy - gyi, topLeft.G, topRight.G, bottomLeft.G, bottomRight.G);
512 ushort blue = (ushort)BilinearInterpolate(gx - gxi, gy - gyi, topLeft.B, topRight.B, bottomLeft.B, bottomRight.B);
513
514 resized.SetPixel(x, y, new ColorS(red, green, blue));
515 }
516 }
517
518
519
520 return resized;
521 }