1

I have looked at other questions such as Ghost-borders ('ringing') when resizing in GDI+ to try and fix my problem, but they don't quite address the issue I am having as they are only fixing the problem when this is on the edge of an image.

I have an image that is transparent on one half and opaque on the other:

image with transparency

When I resize this image I get a very faint grey line where the transparent and the opaque join equivalent to red 254, green 254, green 254 and alpha 199.

The code I am using to resize the image is:

public static Bitmap ResizeBitmap(Bitmap sourceBMP, int width, int height)
    {
        try
        {
            Bitmap result = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(result))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(sourceBMP, 0, 0, width, height);
            }
            sourceBMP.Dispose();
            sourceBMP = (Bitmap)result.Clone();
            result.Dispose();
            return sourceBMP;
        }
        catch (Exception ex)
        {
            return sourceBMP;
        }
    }

I have tried changing the compositing mode, and the wrap mode in image attributes (which I realised was no use because my problem isn't on the edge!) to no avail. I would greatly appreciate any help in coming up with a solution to this if possible.

Community
  • 1
  • 1
John
  • 685
  • 1
  • 6
  • 20
  • 1
    Can you try InterpolationMode.NearestNeighbor ? – TaW Feb 27 '17 at 18:58
  • I think this is always going to be the case for any interpolation mode when resizing to anything that isn't a 1:4 area ratio. otherwise the photo would just not be the closest interpretation of the original photo. you'll have to manually code something that removes the line. Ie, pixels at location < width/2 are transparent else white. but this may not allow for flexibility to a different photo – Adam M. Feb 27 '17 at 19:06
  • 1
    It is the exact same case and caused by the InterpolationMode setting. The higher the quality you select, the more pixels it uses to interpolate. Inevitably some of the transparent pixels are going to affect the non-transparent part of the image. The more abrupt the pixel value change, the more obvious it gets. Alpha changing from 0 to 255 is as abrupt as it gets. Gets extra visible because they all occur at the same location. You'd have to use a blur transform next to suppress the ringing artifacts. Only NearestNeighbor can avoid it completely. – Hans Passant Feb 27 '17 at 19:36
  • I feared this might be the case, thanks. – John Feb 27 '17 at 20:30
  • It's not that surprising. GDI+ is probably blending the alpha and color channels separately, and the color underlying the transparent pixels is likely (0, 0, 0) - i.e. black. So the grey line is half-transparent, half-white and half-black. I might attack this by resizing the color and alpha channels independently as separate bitmaps, then compositing them together. And, before resizing the color bitmap, fill in any transparent pixels by recursively interpolating between adjacent non-transparent pixels. – dbc Feb 27 '17 at 22:30

0 Answers0