Ejemplo n.º 1
0
        private static BitmapSource Convert(BitmapSource bitmapSource)
        {
            // bitmapSource = ImageUtility.ConvertToBgra32Format(bitmapSource);

            int pixelWidth, pixelHeight, stride;

            byte[] pixelsData = ImageUtility.GetBitmapSourcePixelsData(bitmapSource, out pixelWidth, out pixelHeight, out stride);

            for (int x = 0; x < pixelWidth; x++)
            {
                for (int y = 0; y < pixelHeight; y++)
                {
                    byte alpha, blue, green, red;
                    PixelUtility.GetPixelBgra(pixelsData, x, y, stride, out blue, out green, out red, out alpha);
                    if (blue == 255 && green == 255 && red == 255)
                    {
                        PixelUtility.SetPixelBgra(pixelsData, x, y, stride, 0, 0, 0, 0);
                        continue;
                    }

                    if (alpha < 255 || blue == 255 || green == 255 || red == 255)
                    {
                        PixelUtility.SetPixelBgra(pixelsData, x, y, stride, blue, green, red, alpha);
                        continue;
                    }

                    // blue:255 alpha:blue
                    // green:255 alpha:green
                    // red:255 alpha:red

                    byte averageValue = (byte)((blue + green + red) / 3);

                    byte newAlpha = (byte)(255 - averageValue);
                    byte newBlue  = (byte)0;
                    byte newGreen = (byte)0;
                    byte newRed   = (byte)0;

                    PixelUtility.SetPixelBgra(pixelsData, x, y, stride, newBlue, newGreen, newRed, newAlpha);
                }
            }

            WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapSource);

            writeableBitmap.WritePixels(
                new Int32Rect(0, 0, pixelWidth, pixelHeight),
                pixelsData, stride, 0);
            return(writeableBitmap);
        }
Ejemplo n.º 2
0
        public static BitmapSource TransferPixels(BitmapSource bitmapSource, Color from, Color to, bool isForPng = false)
        {
            if (isForPng)
            {
                bitmapSource = ConvertToBgra32Format(bitmapSource);
            }

            int pixelWidth, pixelHeight, stride;

            byte[] pixelsData = GetBitmapSourcePixelsData(bitmapSource, out pixelWidth, out pixelHeight, out stride);
            for (int x = 0; x < pixelWidth; x++)
            {
                for (int y = 0; y < pixelHeight; y++)
                {
                    byte alpah, blue, green, red;
                    if (isForPng)
                    {
                        PixelUtility.GetPixelBgra(pixelsData, x, y, stride, out blue, out green, out red, out alpah);
                        if (blue == from.B && green == from.G && red == from.R)
                        {
                            PixelUtility.SetPixelBgra(pixelsData, x, y, stride, to.B, to.G, to.R, to.A);
                        }
                    }
                    else
                    {
                        PixelUtility.GetPixelBgr(pixelsData, x, y, stride, out blue, out green, out red);
                        if (blue == from.B && green == from.G && red == from.R)
                        {
                            PixelUtility.SetPixelBgr(pixelsData, x, y, stride, to.B, to.G, to.R);
                        }
                    }
                }
            }

            WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapSource);

            writeableBitmap.WritePixels(
                new Int32Rect(0, 0, pixelWidth, pixelHeight),
                pixelsData, stride, 0);
            return(writeableBitmap);
        }