2

I'm having difficulties with System.Drawing bugs in C#. I've been trying to diagnose it for 5 months.

My program basically does photoshop brushes. Example below.
Small picture of dynamics with brushes

It works well on images that are absolutely opaque. But, if the image has any transparency at all, it begins to cause strange errors. Consider this.
Image with transparent regions and lines drawn over it. Image with transparent regions and a big line with dark border.

The gray border on every line, and highly visible in the second picture, is an alpha artifact. If I draw over the same region, I can get the color as dark as (127, 127, 127) in RGB, which is perfect gray, and I don't think it's a coincidence.

This error occurs when opening/closing the dialog, undo/redo, and drawing over transparent regions.

Anyway, I'd love to get help with fixing this GDI+ issue. I've been here:
Color value with alpha of zero shows up as black
GDI+ Bug: Anti-Aliasing White On Transparency
C# Resized images have black borders
Ghost-borders ('ringing') when resizing in GDI+
How to solve grayish frame issue when Scaling a bitmap using GDI+
DrawImage() function over WinForms does not work correctly
http://www.codeproject.com/Articles/14884/BorderBug

Possibility: there is a Border Bug where resized images include pixels outside the image, which are assumed Black, and they're involved in the calculation (resulting in blackened borders)

But that doesn't explain how copying and writing the image over the source causes this effect.

Possibility: creating a new bitmap automatically premultiplies each pixel by its alpha.

I've tried a tested workaround that locks the bits and copies them over (to avoid multiplying by alpha), but this doesn't solve my issue.

I've tried all the solutions presented here, which I summarize below:

Any combination of InterpolationMode.NearestNeighbor, SmoothingMode.None, and PixelOffsetMode.Half.

Clearing with a transparent color, Color.White, and Color.Black, along with the above attempts as well.

Workarounds for cloning bitmaps, sometimes in conjunction with other workarounds above.

Using an ImageAttributes object and setting WrapMode to TileFlipXY, sometimes in conjunction with above workarounds.

Using Color.ConvertFromPremultipliedAlpha and Color.ConvertToPremultipliedAlpha, with a definitely bad effect.

Here is the main file, which includes all the relevant code.
A tiny bit more discussion on my original post off-site.

Community
  • 1
  • 1
  • For better control of the mixing and the alpha blending I prefer not to use DrawImage for brush painting but a LockiBits solution. – TaW Aug 11 '16 at 09:50
  • Thanks, but I've unfortunately already tried that. It's under the second blockquote. Also, by tested I mean that it works to correctly copy bits, but the issues persist with it, too. –  Aug 11 '16 at 16:52
  • 1
    Well, as it allows you to do anything with the bits and bytes of each channel it most certainly should be able to help, if done right, of course.. Getting alpha 'right' is imo not possible, as there are quite a few modes one can wish for and noe can be considered as the 'natrual one'.. As far (and I'm talking about a few years here) as I remember I had to compute each as the built-in blending didn't do anything I needed for any of my 5-10 blending modes. – TaW Aug 11 '16 at 17:14

0 Answers0