C# Resized images have black borders

Try:

graphic.CompositingMode = CompositingMode.SourceCopy;

The problem lies in the fact that your bitmap toReturn has a black background by default. Copying a new image over it makes black or gray borders.

The solution is to remove the black default background, by calling:

toReturn.MakeTransparent();

Since after this line you'll be drawing on a new image without any background color the borders will disappear.


This can be caused by pixels around the edges being wrongly interpolated. I'd call this a bug.

Here's the solution, though:

graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.PixelOffsetMode = PixelOffsetMode.Half;
graphics.InterpolationMode = InterpolationMode.NearestNeighbor;

// Draw your image here.

graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

// Draw it again.

What this does is first drawing a "background" with the edges correctly-filled, and then draw it again with interpolation. If you don't need interpolation, then this is not necessary.