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.
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.
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.