Ejemplo n.º 1
0
        private Bitmap RenderImage()
        {
            Bitmap bitmap = TokenControl.RenderVisual(TokenControl.ZoomControl);

            //Apply the special filter.
            BitmapHelper.AddFilter(bitmap, _vm.SpecialFilterColor, _specialFilter);

            // Modify the rendered image.
            if (_overlayInfo != null)
            {
                var overlay = ImageLoader.LoadBitmapFromFile(_overlayInfo.Info.File, TokenControl.ImageResolutionWidth, TokenControl.ImageResolutionHeight);
                BitmapHelper.AddFilter(overlay, _overlayTintColor, _overlayBlendFilter);
                if (!string.IsNullOrWhiteSpace(_overlayInfo.Info.Mask))
                {
                    BitmapHelper.ApplyMaskToImage(overlay, ImageLoader.LoadBitmapFromFile(_overlayInfo.Info.Mask, TokenControl.ImageResolutionWidth, TokenControl.ImageResolutionHeight));
                }
                BitmapHelper.AddLayerToImage(bitmap, overlay);
            }

            if (_borderInfo != null)
            {
                if (!string.IsNullOrWhiteSpace(_borderInfo.Info.Mask))
                {
                    BitmapHelper.ApplyMaskToImage(bitmap, ImageLoader.LoadBitmapFromFile(_borderInfo.Info.Mask, TokenControl.ImageResolutionWidth, TokenControl.ImageResolutionHeight));
                }
                var border = ImageLoader.LoadBitmapFromFile(_borderInfo.Info.File, TokenControl.ImageResolutionWidth, TokenControl.ImageResolutionHeight);
                BitmapHelper.AddFilter(border, _borderTintColor, _borderBlendFilter);
                BitmapHelper.AddLayerToImage(bitmap, border);  //Draw the border
            }

            if (TokenControl.ZoomControl_Text.Visibility == Visibility.Visible)
            {
                Bitmap text = TokenControl.RenderVisual(TokenControl.ZoomControl_Text);
                BitmapHelper.AddLayerToImage(bitmap, text);
            }

            if (_vm.Autocrop)
            {
                bitmap = BitmapHelper.Autocrop(bitmap);
            }

            //Since we just spent time rendering the image, we might as well update the preview even if the user didn't ask for that specifically.
            _preWindow?.SetImage(bitmap);
            return(bitmap);
        }
Ejemplo n.º 2
0
        //TODO: This only works with local files. Add a way for it to support internet images.
        public bool UpdatePreview(Layer.LayerType layerType)
        {
            var backdrop = new Bitmap(DataAccess.Properties.Resources.LayerPreviewImage, PreviewSize, PreviewSize);

            // If no file has been specified, we dont have anything to show preview-wise
            if (string.IsNullOrWhiteSpace(File))
            {
                Image = BitmapHelper.ConvertBitmapToImageSource(backdrop);
                return(true);
            }

            Bitmap image; //Image in its actual size
            var    loadedImage = ImageLoader.TryLoadBitmapFromFile(File, out image);

            if (!loadedImage)
            {
                return(false);
            }
            Bitmap imageSmall = BitmapHelper.ConvertToPixelFormat_32bppArgb(new Bitmap(image, PreviewSize, PreviewSize)); //Image scaled to the size of the preview.

            Bitmap mask;

            switch (layerType)
            {
            case Layer.LayerType.Border:
                switch (MaskType)
                {
                case MaskTypes.None:
                    BitmapHelper.AddLayerToImage(backdrop, imageSmall);
                    break;

                case MaskTypes.User:
                    //If the user hasn't specified a mask, dont attempt to load it. Just show image without a mask.
                    if (string.IsNullOrWhiteSpace(Mask))
                    {
                        BitmapHelper.AddLayerToImage(backdrop, imageSmall);
                        break;
                    }

                    var loadedMask = ImageLoader.TryLoadBitmapFromFile(Mask, out mask, PreviewSize, PreviewSize);
                    if (!loadedMask)
                    {
                        return(false);
                    }

                    BitmapHelper.ApplyMaskToImage(backdrop, mask);         //For Border-layers we apply the mask to the backdrop
                    BitmapHelper.AddLayerToImage(backdrop, imageSmall);
                    break;

                case MaskTypes.Autogenerated:
                    var generatedMask      = BitmapHelper.GenerateMask(image);    //Generate mask for full-size image, and then scale it down afterward so we have as much detail as possible
                    var generatedMaskSmall = BitmapHelper.ConvertToPixelFormat_32bppArgb(new Bitmap(generatedMask, PreviewSize, PreviewSize));
                    BitmapHelper.ApplyMaskToImage(backdrop, generatedMaskSmall);
                    BitmapHelper.AddLayerToImage(backdrop, imageSmall);
                    GeneratedMask = generatedMask;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                break;

            case Layer.LayerType.Overlay:
                switch (MaskType)
                {
                case MaskTypes.None:
                    BitmapHelper.AddLayerToImage(backdrop, imageSmall);
                    break;

                case MaskTypes.User:
                    //If the user hasn't specified a mask, dont attempt to load it. Just show image without a mask.
                    if (string.IsNullOrWhiteSpace(Mask))
                    {
                        BitmapHelper.AddLayerToImage(backdrop, imageSmall);
                        break;
                    }

                    var loadedMask = ImageLoader.TryLoadBitmapFromFile(Mask, out mask, PreviewSize, PreviewSize);
                    if (!loadedMask)
                    {
                        return(false);
                    }

                    BitmapHelper.ApplyMaskToImage(imageSmall, mask);         //For Overlay-layers we apply the mask to the overlay
                    BitmapHelper.AddLayerToImage(backdrop, imageSmall);
                    break;

                case MaskTypes.Autogenerated:
                    throw new InvalidEnumArgumentException("Overlays cant have autogenerated masks");

                default:
                    throw new ArgumentOutOfRangeException();
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(layerType), layerType, null);
            }

            Image = BitmapHelper.ConvertBitmapToImageSource(backdrop);
            return(true);
        }