Alpha blends a color with its destination pixel using the standard formula - CSharp System.Drawing

CSharp examples for System.Drawing:Color

Description

Alpha blends a color with its destination pixel using the standard formula

Demo Code

// (c) Copyright Microsoft Corporation.
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.IO;/*from   www . ja va 2s.c  o m*/
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        // Alpha blends a color with its destination pixel using the standard formula
        private static unsafe void WriteAlphaBlended(UInt32 *pixel, Color c)
        {
            byte* component = (byte*)pixel;
            ushort alpha = (ushort)c.A;
            component[3] += (byte)(((ushort)(255 - component[3]) * alpha) / 255);
            component[2] = (byte)(((ushort)component[2] * (255 - alpha) + (ushort)c.R * alpha) / 255);
            component[1] = (byte)(((ushort)component[1] * (255 - alpha) + (ushort)c.G * alpha) / 255);
            component[0] = (byte)(((ushort)component[0] * (255 - alpha) + (ushort)c.B * alpha) / 255);
        }
}

Related Tutorials