Beispiel #1
0
        private static unsafe BitmapSource ConvertToGrayScale(BitmapSource image, Color biasColor)
        {
            Validate.IsNotNull(image, nameof(image));
            if (image.Format != PixelFormats.Bgra32)
            {
                throw new ArgumentException();
            }
            int stride = image.PixelWidth * 4;
            int num    = image.PixelWidth * image.PixelHeight * 4;

            using (ReusableResourceHolder <byte[]> reusableResourceHolder = ImageThemingUtilities.AcquireConversionBuffer(num))
            {
                byte[] resource = reusableResourceHolder.Resource;
                image.CopyPixels(resource, stride, 0);
                ImageThemingUtilities.GrayscaleDIBits(resource, num, biasColor);
                byte[]       numArray;
                BitmapSource bitmapSource;
                fixed(void *_ = numArray = resource)
                {
                    void *voidPtr;

                    if (numArray.Length == 0)
                    {
                        voidPtr = null;
                    }
                    else
                        fixed(void *tmp = &numArray[0])
                        {
                            voidPtr = tmp;
                        }
                    bitmapSource = BitmapSource.Create(image.PixelWidth, image.PixelHeight, image.DpiX, image.DpiY, PixelFormats.Bgra32, image.Palette, (IntPtr)voidPtr, num, stride);
                }

                bitmapSource.Freeze();
                return(bitmapSource);
            }
        }
Beispiel #2
0
        public static BitmapSource CreateThemedBitmapSource(BitmapSource inputImage, Color backgroundColor, bool isEnabled, Color grayscaleBiasColor, bool isHighContrast)
        {
            if (inputImage.Format != PixelFormats.Bgra32)
            {
                inputImage = new FormatConvertedBitmap(inputImage, PixelFormats.Bgra32, null, 0.0);
            }
            int stride = inputImage.PixelWidth * 4;
            int num    = inputImage.PixelWidth * inputImage.PixelHeight * 4;

            using (ReusableResourceHolder <byte[]> reusableResourceHolder = AcquireConversionBuffer(num))
            {
                byte[] resource = reusableResourceHolder.Resource;
                inputImage.CopyPixels(resource, stride, 0);
                uint backgroundRgba = (uint)(backgroundColor.B << 16 | backgroundColor.G << 8) | backgroundColor.R;
                ThemeDIBits(num, resource, inputImage.PixelWidth, inputImage.PixelHeight, true, backgroundRgba, isHighContrast);
                if (!isEnabled)
                {
                    GrayscaleDIBits(resource, num, grayscaleBiasColor);
                }
                BitmapSource bitmapSource = BitmapSource.Create(inputImage.PixelWidth, inputImage.PixelHeight, inputImage.DpiX, inputImage.DpiY, PixelFormats.Bgra32, inputImage.Palette, resource, stride);
                bitmapSource.Freeze();
                return(bitmapSource);
            }
        }