'Resize Image But Wrong Border Color (Black 'Auto')

    public Image resizeImage(int newWidth, int newHeight, string stPhotoPath)
    {
        Image imgPhoto = Image.FromFile(stPhotoPath);

        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;

        if (sourceWidth < sourceHeight)
        {
            int buff = newWidth;

            newWidth = newHeight;
            newHeight = buff;
        }

        int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
        float nPercent = 0, nPercentW = 0, nPercentH = 0;

        nPercentW = ((float)newWidth / (float)sourceWidth);
        nPercentH = ((float)newHeight / (float)sourceHeight);
        if (nPercentH < nPercentW)
        {
            nPercent = nPercentH;
            destX = System.Convert.ToInt16((newWidth -
                      (sourceWidth * nPercent)) / 2);
        }
        else
        {
            nPercent = nPercentW;
            destY = System.Convert.ToInt16((newHeight -
                      (sourceHeight * nPercent)) / 2);
        }

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);


        Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
                      PixelFormat.Format24bppRgb);

        bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                     imgPhoto.VerticalResolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(Color.Black);
        grPhoto.InterpolationMode =
            System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto,
            new Rectangle(destX, destY, destWidth, destHeight),
            new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        imgPhoto.Dispose();
        return bmPhoto;
    }

I use such a code to scale down images, but there is a problem that after the images are scaled down, the remaining spaces remain black. Something that should definitely not happen. What is the solution? The background color must be white.



Solution 1:[1]

public static  class ResizeImage
{
        /// <summary>
   /// Return new resized size to display the image
     /// </summary>
   /// <param name="srcrectanle">source rectangle of image or you can pass the 
    bitmap and set the size accrodingly</param>
/// <param name="initSize">initial size of the page to draw image</param>
/// <returns></returns>
public static SizeF getResizedRectangle(RectangleF srcrectanle, SizeF initSize)
{
    float sw = srcrectanle.Width;
    float sh = srcrectanle.Height;
    float dw = initSize.Width;
    float dh = initSize.Height;
    float finalHeight, finalWidth;
    float Sourceratio = sw / sh;

    if (Sourceratio >= 1)
    {
        finalWidth = (int)dw;
        float ratio = sw / dw;
        finalHeight = (sh / ratio);
    }
    else
    {
        finalHeight = (int)dh;
        float ratio = sh / dh;
        finalWidth = (sw / ratio);
    }
    return new SizeF(finalHeight, finalHeight);
}
}

Use this function to resize your image. Hope you have the knowledge of source rectangle and destination rectangle.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Uwe Keim