C# Resized images have black borders

前端 未结 9 1287
长发绾君心
长发绾君心 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:37

    How does the following work for you? This is the code I've used to do the same thing. The main difference I notice is that I don't use SetResolution (and I assume a square input and output, since that was the case for me).

    /// 
    /// Resizes a square image
    /// 
    /// Image to resize
    /// Width and height of new image
    /// A scaled version of the image
    internal static Image ResizeImage( Image OriginalImage, int Size )
    {
        Image finalImage = new Bitmap( Size, Size );
    
        Graphics graphic = Graphics.FromImage( finalImage );
    
        graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
        graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
        graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    
        Rectangle rectangle = new Rectangle( 0, 0, Size, Size );
    
        graphic.DrawImage( OriginalImage, rectangle );
    
        return finalImage;
    }
    

提交回复
热议问题