Quantcast
Channel: WriteableBitmapEx
Viewing all articles
Browse latest Browse all 360

Commented Unassigned: Blit doesn't work well with transparent PNG images [20713]

$
0
0
Hello

I am trying to apply an transparent image (in png format) over background image. The result is not very good as applied image has too much artifacts over it.
It is known problem?

I am making an application for Windows 8.1 RT.

```
img.Blit(r, talisman, new Rect(0, 0, talisman.PixelWidth, talisman.PixelHeight));
```

Using WriteableBitmapExtensions.BlendMode.Alpha doesn't help at all. When I put both images i XAML one over another, I see no artifacts.
Comments: ** Comment from web user: lachlank **

I also found artefact bugs in Blit() with BlendMode = Alpha.

I changed the algorithm used in the Blit() method of WriteableBitmapBlitExtensions.cs to match that described in the [Alpha Compositing article on Wikipedia](http://en.wikipedia.org/wiki/Alpha_compositing), which works correctly. It is a simplistic implementation to prove the point and could of course be tidied up for performance, but proves the point and will make your code work:

```
double saX = (double)sa / 255;
double srX = (double)sr / 255;
double sgX = (double)sg / 255;
double sbX = (double)sb / 255;
double daX = (double)da / 255;
double drX = (double)dr / 255;
double dgX = (double)dg / 255;
double dbX = (double)db / 255;

var newAlphaX = (saX + (daX * (1.0 - saX)));
var newRedX = (srX * saX + (drX * daX * (1.0 - saX))) / newAlphaX;
var newGreenX = (sgX * saX + (dgX * daX * (1.0 - saX))) / newAlphaX;
var newBlueX = (sbX * saX + (dbX * daX * (1.0 - saX))) / newAlphaX;

var newAlpha = (int) (newAlphaX * 255.0);
var newRed = (int) (newRedX * 255.0);
var newGreen = (int)(newGreenX * 255.0);
var newBlue = (int)(newBlueX * 255.0);

destPixel = (newAlpha << 24) + (newRed << 16) + (newGreen << 8) + newBlue;
//destPixel = ((((sa << 8) + ((255 - sa) * da)) >> 8) << 24) +
// ((((sr << 8) + ((255 - sa) * dr)) >> 8) << 16) +
// ((((sg << 8) + ((255 - sa) * dg)) >> 8) << 8) +
// (((sb << 8) + ((255 - sa) * db)) >> 8);
// (((sb << 8) + ((255 - sa) * db)) >> 8);
```


Viewing all articles
Browse latest Browse all 360

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>