Example #1
0
        //This sucks.
        static public Bitmap generateAlpha(Bitmap source)
        {
            Bitmap    output = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppArgb);
            Rectangle r      = new Rectangle(0, 0, source.Width, source.Height);

            unsafe
            {
                BitmapData sourceData = source.LockBits(r, ImageLockMode.ReadOnly, source.PixelFormat);
                BitmapData outputData = output.LockBits(r, ImageLockMode.WriteOnly, output.PixelFormat);

                if (sourceData.PixelFormat == PixelFormat.Format24bppRgb)
                {
                    PixelData24 *sourceBase = (PixelData24 *)sourceData.Scan0;
                    PixelData32 *outputBase = (PixelData32 *)outputData.Scan0;
                    int          width      = source.Width;
                    int          height     = source.Height;
                    for (int x = 0; x < width; x++)
                    {
                        for (int y = 0; y < height; y++)
                        {
                            PixelData24 *pSourcePixel = sourceBase + y * width + x;
                            PixelData32 *pOututPixel  = outputBase + y * width + x;
                            byte         blue         = pSourcePixel->blue;
                            pOututPixel->alpha = (byte)(blue);
                            pOututPixel->red   = (byte)(blue);
                            pOututPixel->blue  = (byte)(blue);
                            pOututPixel->green = (byte)(blue);
                        }
                    }
                }
                else if (sourceData.PixelFormat == PixelFormat.Format32bppArgb)
                {
                    PixelData32 *sourceBase = (PixelData32 *)sourceData.Scan0;
                    PixelData32 *outputBase = (PixelData32 *)outputData.Scan0;
                    int          width      = source.Width;
                    int          height     = source.Height;
                    for (int x = 0; x < width; x++)
                    {
                        for (int y = 0; y < height; y++)
                        {
                            PixelData32 *pSourcePixel = sourceBase + y * width + x;
                            PixelData32 *pOututPixel  = outputBase + y * width + x;
                            byte         alpha        = pSourcePixel->alpha;
                            pOututPixel->alpha = (byte)(alpha);
                            pOututPixel->red   = (byte)(alpha);
                            pOututPixel->blue  = (byte)(alpha);
                            pOututPixel->green = (byte)(alpha);
                        }
                    }
                }
                source.UnlockBits(sourceData);
                output.UnlockBits(outputData);
            }
            return(output);
        }
Example #2
0
        public void SetPixel(int x, int y, byte alpha, byte red, byte green, byte blue)
        {
            switch (bitmap.PixelFormat)
            {
            case PixelFormat.Format24bppRgb:
                PixelData24 *pixel24 = PixelAt24(x, y);
                *            pixel24 = new PixelData24(red, green, blue);
                break;

            case PixelFormat.Format32bppArgb:
                PixelData32 *pixel32 = PixelAt32(x, y);
                *            pixel32 = new PixelData32(alpha, red, green, blue);
                break;

            default:
                throw new NotSupportedException();
            }
        }