Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static long CleanIfTransparent(Bitmap bmp)
- {
- long rtn = 0;
- //does our image contain an alpha channel?
- if ((bmp.Flags & 0x2) != 0 && bmp.PixelFormat == PixelFormat.Format32bppArgb)
- {
- //get int pointer to bmp.
- BitmapData bData = bmp.LockBits(
- new Rectangle(0, 0, bmp.Width, bmp.Height),
- ImageLockMode.ReadOnly,
- PixelFormat.Format32bppArgb);
- IntPtr iptr = bData.Scan0;
- //get byteCount of bmp.
- int len = bmp.Height * Math.Abs(bData.Stride) / 4;
- unsafe
- {
- //get pointer from intpointer
- byte* ptr = (byte*)iptr.ToPointer();
- //loop through image.
- for (int i = 0; i < len; i++)
- {
- //the pixel value in bgra
- //inverse alpha
- byte a = (Byte)(0xFF - (*(ptr + 3)));
- //do we have a value for our alpha?
- if (a > 0)
- {
- rtn++;
- //blue
- *ptr = (byte)(Math.Min(*ptr + a, 255));
- ptr++;
- //green
- *ptr = (byte)(Math.Min(*ptr + a, 255));
- ptr++;
- //red
- *ptr = (byte)(Math.Min(*ptr + a, 255));
- ptr++;
- //alpha
- *ptr = 0xFF;
- }
- else
- {
- ptr = ptr + 3;
- }
- if (i < len - 1)
- {
- ptr++;
- }
- }
- }
- bmp.UnlockBits(bData);
- }
- return rtn;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement