// Calculate the scaling factors for the new dimensions
float xScale = (float)newWidth / (float)oldWidth;
float yScale = (float)newHeight / (float)oldHeight;
// Create a new bitmap with the new dimensions
Bitmap newImage = new Bitmap(newWidth, newHeight);
// Loop through each pixel in the new image
for (int y = 0; y < newHeight; y++)
{
for (int x = 0; x < newWidth; x++)
{
// Calculate the corresponding pixel in the original image
float oldX = x / xScale;
float oldY = y / yScale;
// Use bilinear interpolation to calculate the value of the new pixel
Color pixel = BilinearInterpolate(oldX, oldY, oldImage);
// Set the value of the new pixel in the new image
newImage.SetPixel(x, y, pixel);
}
}
// Save the new image with the resized dimensions
newImage.Save(...);
------------
private Color BilinearInterpolate(float x, float y, Bitmap image)
{
// Calculate the coordinates of the surrounding pixels
int x1 = (int)Math.Floor(x);
int x2 = (int)Math.Ceiling(x);
int y1 = (int)Math.Floor(y);
int y2 = (int)Math.Ceiling(y);
// Calculate the weights for the surrounding pixels
float w1 = (x2 - x) * (y2 - y);
float w2 = (x2 - x) * (y - y1);
float w3 = (x - x1) * (y2 - y);
float w4 = (x - x1) * (y - y1);
// Get the values of the surrounding pixels
Color p1 = image.GetPixel(x1, y1);
Color p2 = image.GetPixel(x2, y1);
Color p3 = image.GetPixel(x1, y2);
Color p4 = image.GetPixel(x2, y2);
// Calculate the weighted average of the surrounding pixels
----------------
// Determine the new size of the image
int newWidth = 100;
int newHeight = 100;
// Create a new image with the desired dimensions
Bitmap resizedImage = new Bitmap(newWidth, newHeight);
// Iterate over the new image
for (int x = 0; x < newWidth; x++)
{
for (int y = 0; y < newHeight; y++)
{
// Calculate the corresponding location in the original image
float xOld = (x / (float)newWidth) * originalImage.Width;
float yOld = (y / (float)newHeight) * originalImage.Height;
// Use bilinear interpolation to calculate the new pixel value
Color newPixel = GetBilinearInterpolatedPixel(originalImage, xOld, yOld);
// Set the new pixel value in the resized image
resizedImage.SetPixel(x, y, newPixel);
}
}
----------------
--?-?-?-
public static Bitmap BilinearInterpolation(Bitmap bmp, int newWidth, int newHeight)
{
Bitmap newBitmap = new Bitmap(newWidth, newHeight);
// Calculate the scale factor in the x and y directions
float scaleX = (float)newWidth / bmp.Width;
float scaleY = (float)newHeight / bmp.Height;
// Loop through the pixels in the new bitmap
for (int x = 0; x < newWidth; x++)
{
for (int y = 0; y < newHeight; y++)
{
// Calculate the corresponding pixel in the original bitmap
float originalX = x / scaleX;
float originalY = y / scaleY;
// Calculate the integer and fractional parts of the coordinates
int intX = (int)originalX;
int intY = (int)originalY;
float fracX = originalX - intX;
float fracY = originalY - intY;
// Get the color values of the surrounding pixels
Color c1 = bmp.GetPixel(intX, intY);
Color c2 = bmp.GetPixel(intX + 1, intY);
Color c3 = bmp.GetPixel(intX, intY + 1);
Color c4 = bmp.GetPixel(intX + 1, intY + 1);
// Perform the interpolation
int red = (int)((1 - fracX) * (1 - fracY) * c1.R + fracX * (1 - fracY)
* c2.R + (1 - fracX) * fracY * c3.R + fracX * fracY * c4.R);
int green = (int)((1 - fracX) * (1 - fracY) * c1.G + fracX * (1 -
fracY) * c2.G + (1 - fracX) * fracY * c3.G + fracX * fracY * c4.G);
int blue = (int)((1 - fracX) * (1 - fracY) * c1.B + fracX * (1 - fracY)
* c2.B + (1 - fracX) * fracY * c3.B + fracX * fracY * c4.B);