I have a function:
private static Image ScaleAndPadBlack(Image original_img, Thumbnail thumb)
{
if (thumb.OptimalSizeHeight <= 0)
{
throw new ArgumentOutOfRangeException("OptimalSizeHeight", "OptimalSizeHeight must be declared and positive when using the ScaleAndCrop method.");
}
if (thumb.OptimalSizeWidth <= 0)
{
throw new ArgumentOutOfRangeException("OptimalSizeWidth", "OptimalSizeWidth must be declared and positive when using the ScaleAndCrop method.");
}
Size dest_size = new Size(thumb.OptimalSizeWidth, thumb.OptimalSizeHeight);
decimal act_width_rate = (decimal)dest_size.Width / (decimal)original_img.Width;
decimal act_height_rate = (decimal)dest_size.Height / (decimal)original_img.Height;
decimal scale_rate;
if (act_width_rate <= act_height_rate)
scale_rate = act_width_rate;
else
scale_rate = act_height_rate;
Size act_size = new Size(Convert.ToInt32(original_img.Width * scale_rate), Convert.ToInt32(original_img.Height * scale_rate));
var res = resizeImage(original_img, act_size);
var b = new Bitmap(thumb.OptimalSizeWidth, thumb.OptimalSizeHeight);
var g = Graphics.FromImage(b);
Point p = new Point(((dest_size.Width - act_size.Width) / 2), ((dest_size.Height - act_size.Height) / 2));
g.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), dest_size));
g.SmoothingMode = SmoothingMode.None;
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(res, p);
res.Dispose();
res = null;
return b;
}
what this does is it resizes the image to fit inside a bounding box, creating a new image, fill it with black and put the resized image onto the black image.
The problem is that resizing a fully white jpg (761×896) image into a 270×180 bounding box results in this:

You can see that there is a grey bar at the top of the image which is because the resized image somehow has a transparent edge.
Why is this happening? Resizing an image can result in transparent edges? That is definately not desired in this case.
Or is that top edge there because some other reason?
How should I write this function that there are no black-grey edges on cases like this? I want only to fill the part where there is no image, should I do something completely differently?
EDIT:
The g.SmoothingMode = SmoothingMode.None; g.InterpolationMode = InterpolationMode.NearestNeighbor; are there because I was testing this issue. They does not seem to matter.
I have changed the drawimage and fillrectangle part to:
and it seems to be working with my test cases. Still I’m not sure what caused the original issue.