C# Resized images have black borders

前端 未结 9 1264
长发绾君心
长发绾君心 2020-12-15 08:58

I have a problem with image scaling in .NET. I use the standard Graphics type to resize images like in this example:

public static Image Scale(Image sourceIm         


        
9条回答
  •  死守一世寂寞
    2020-12-15 09:54

    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.

提交回复
热议问题