Exemple #1
0
        public Bitmap CreateDeviceFromLogicalImage(Bitmap logicalBitmapStrip, System.Drawing.Size logicalImageSize, System.Drawing.Color backgroundColor, ImageScalingMode scalingMode = ImageScalingMode.Default)
        {
            Validate.IsNotNull(logicalBitmapStrip, "logicalBitmapStrip");
            Validate.IsNotNull(logicalImageSize, "logicalImageSize");
            if (logicalImageSize.Width == 0 || logicalBitmapStrip.Height % logicalImageSize.Width != 0 || logicalImageSize.Height != logicalBitmapStrip.Height)
            {
                throw new ArgumentException("logicalImageSize not matching the logicalBitmap size");
            }
            int    num          = logicalBitmapStrip.Width / logicalImageSize.Width;
            int    deviceUnitsX = LogicalToDeviceUnitsX(logicalImageSize.Width);
            int    deviceUnitsY = LogicalToDeviceUnitsY(logicalImageSize.Height);
            Bitmap bitmap       = new Bitmap(num * deviceUnitsX, deviceUnitsY, logicalBitmapStrip.PixelFormat);

            using (Graphics graphics1 = Graphics.FromImage(bitmap))
            {
                graphics1.InterpolationMode = InterpolationMode.NearestNeighbor;
                for (int index = 0; index < num; ++index)
                {
                    RectangleF srcRect = new RectangleF(index * logicalImageSize.Width, 0.0f, logicalImageSize.Width, logicalImageSize.Height);
                    srcRect.Offset(-0.5f, -0.5f);
                    RectangleF destRect    = new RectangleF(0.0f, 0.0f, logicalImageSize.Width, logicalImageSize.Height);
                    Bitmap     bitmapImage = new Bitmap(logicalImageSize.Width, logicalImageSize.Height, logicalBitmapStrip.PixelFormat);
                    using (Graphics graphics2 = Graphics.FromImage(bitmapImage))
                    {
                        graphics2.InterpolationMode = InterpolationMode.NearestNeighbor;
                        graphics2.DrawImage(logicalBitmapStrip, destRect, srcRect, GraphicsUnit.Pixel);
                    }
                    LogicalToDeviceUnits(ref bitmapImage, backgroundColor, scalingMode);
                    srcRect = new RectangleF(0.0f, 0.0f, deviceUnitsX, deviceUnitsY);
                    srcRect.Offset(-0.5f, -0.5f);
                    destRect = new RectangleF(index * deviceUnitsX, 0.0f, deviceUnitsX, deviceUnitsY);
                    graphics1.DrawImage(bitmapImage, destRect, srcRect, GraphicsUnit.Pixel);
                }
            }
            return(bitmap);
        }
Exemple #2
0
 public static void FocusPossiblyUnloadedElement(FrameworkElement element)
 {
     Validate.IsNotNull(element, "element");
     PendingFocusHelper.SetFocusOnLoad(element);
 }
Exemple #3
0
 public static void SetFocusTarget(UIElement element, UIElement value)
 {
     Validate.IsNotNull(element, "element");
     element.SetValue(FocusTargetProperty, value);
 }
Exemple #4
0
 public static UIElement GetFocusTarget(UIElement element)
 {
     Validate.IsNotNull(element, "element");
     return((UIElement)element.GetValue(FocusTargetProperty));
 }
Exemple #5
0
        private Image ScaleLogicalImageForDeviceSize(Image logicalImage, System.Drawing.Size deviceImageSize, System.Drawing.Color backgroundColor, ImageScalingMode scalingMode)
        {
            Validate.IsNotNull(logicalImage, "logicalImage");
            InterpolationMode interpolationMode = GetInterpolationMode(scalingMode);

            System.Drawing.Imaging.PixelFormat pixelFormat = logicalImage.PixelFormat;
            System.Drawing.Color clrMagenta          = System.Drawing.Color.FromArgb(byte.MaxValue, 0, byte.MaxValue);
            System.Drawing.Color clrNearGreen        = System.Drawing.Color.FromArgb(0, 254, 0);
            System.Drawing.Color clrTransparentHalo  = System.Drawing.Color.FromArgb(0, 246, 246, 246);
            System.Drawing.Color clrActualBackground = backgroundColor;
            Bitmap image1 = logicalImage as Bitmap;

            if (scalingMode != ImageScalingMode.NearestNeighbor && image1 != null)
            {
                if (pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                {
                    Rectangle rect = new Rectangle(0, 0, logicalImage.Width, logicalImage.Height);
                    logicalImage = image1.Clone(rect, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    image1       = (Bitmap)logicalImage;
                    if (backgroundColor != System.Drawing.Color.Transparent && backgroundColor.A != byte.MaxValue)
                    {
                        backgroundColor = System.Drawing.Color.FromArgb(byte.MaxValue, backgroundColor);
                    }
                }
                ProcessBitmapPixels(image1, (ref byte alpha, ref byte red, ref byte green, ref byte blue) =>
                {
                    if (backgroundColor != System.Drawing.Color.Transparent)
                    {
                        if (alpha != backgroundColor.A || red != backgroundColor.R || (green != backgroundColor.G || blue != backgroundColor.B))
                        {
                            return;
                        }
                        alpha = clrTransparentHalo.A;
                        red   = clrTransparentHalo.R;
                        green = clrTransparentHalo.G;
                        blue  = clrTransparentHalo.B;
                        clrActualBackground = backgroundColor;
                    }
                    else if (alpha == clrMagenta.A && red == clrMagenta.R && (green == clrMagenta.G && blue == clrMagenta.B))
                    {
                        alpha = clrTransparentHalo.A;
                        red   = clrTransparentHalo.R;
                        green = clrTransparentHalo.G;
                        blue  = clrTransparentHalo.B;
                        clrActualBackground = clrMagenta;
                    }
                    else
                    {
                        if (alpha != clrNearGreen.A || red != clrNearGreen.R || (green != clrNearGreen.G || blue != clrNearGreen.B))
                        {
                            return;
                        }
                        alpha = clrTransparentHalo.A;
                        red   = clrTransparentHalo.R;
                        green = clrTransparentHalo.G;
                        blue  = clrTransparentHalo.B;
                        clrActualBackground = clrNearGreen;
                    }
                });
                if (clrActualBackground == System.Drawing.Color.Transparent && pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                {
                    clrActualBackground = !(backgroundColor != System.Drawing.Color.Transparent) ? clrMagenta : backgroundColor;
                }
            }
            Image image2;

            if (logicalImage is Bitmap)
            {
                image2 = new Bitmap(deviceImageSize.Width, deviceImageSize.Height, logicalImage.PixelFormat);
            }
            else
            {
                if (!(logicalImage is Metafile))
                {
                    throw new ArgumentException("Unsupported image type for High DPI conversion", nameof(logicalImage));
                }
                IntPtr dc = NativeMethods.User32.GetDC(IntPtr.Zero);
                try
                {
                    image2 = new Metafile(dc, EmfType.EmfPlusDual);
                }
                finally
                {
                    NativeMethods.User32.ReleaseDC(IntPtr.Zero, dc);
                }
            }
            using (Graphics graphics = Graphics.FromImage(image2))
            {
                graphics.InterpolationMode = interpolationMode;
                graphics.Clear(backgroundColor);
                RectangleF srcRect = new RectangleF(0.0f, 0.0f, logicalImage.Size.Width, logicalImage.Size.Height);
                srcRect.Offset(-0.5f, -0.5f);
                RectangleF destRect = new RectangleF(0.0f, 0.0f, deviceImageSize.Width, deviceImageSize.Height);
                if (scalingMode == ImageScalingMode.BorderOnly)
                {
                    destRect = new RectangleF(0.0f, 0.0f, srcRect.Width, srcRect.Height);
                    destRect.Offset((float)((deviceImageSize.Width - (double)srcRect.Width) / 2.0), (float)((deviceImageSize.Height - (double)srcRect.Height) / 2.0));
                }
                graphics.DrawImage(logicalImage, destRect, srcRect, GraphicsUnit.Pixel);
            }
            Bitmap image3 = image2 as Bitmap;

            if (scalingMode != ImageScalingMode.NearestNeighbor && image3 != null)
            {
                ProcessBitmapPixels(image3, (ref byte alpha, ref byte red, ref byte green, ref byte blue) =>
                {
                    if (alpha == byte.MaxValue)
                    {
                        return;
                    }
                    alpha = clrActualBackground.A;
                    red   = clrActualBackground.R;
                    green = clrActualBackground.G;
                    blue  = clrActualBackground.B;
                });
                if (pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                {
                    Rectangle rect = new Rectangle(0, 0, image2.Width, image2.Height);
                    image2 = image3.Clone(rect, pixelFormat);
                }
            }
            return(image2);
        }
Exemple #6
0
 public PathParser(string path)
 {
     Validate.IsNotNull(path, "path");
     Path = path;
 }