public Bitmap Resize(Bitmap OriginalImage, int Width, int Height)
{
if (Width <= 0 || Height <= 0)
{
throw new Exception("Output image width and height must be positive");
}
Bitmap OutputImage = new System.Drawing.Bitmap(Width, Height);
Double rh = Convert.ToDouble(OriginalImage.Width) / Convert.ToDouble(Width); // Horizontal ration
Double rv = Convert.ToDouble(OriginalImage.Height) / Convert.ToDouble(Height); // Vertical ratio
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
int m = (int)(x * rh);
int n = (int)(y * rv);
if (m >= OriginalImage.Width) m = OriginalImage.Width - 1;
if (n >= OriginalImage.Height) n = OriginalImage.Height - 1;
Color pixel = OriginalImage.GetPixel(m, n);
OutputImage.SetPixel(x, y, pixel);
}
}
return OutputImage;
}