Наследование: SoundInTheory.DynamicImage.DirtyTrackingObject
Пример #1
0
        public static Sd.Fill ToFill(this Wg.Color input)
        {
            Sd.Fill fill = new Sd.Fill();
            fill.BackgroundColor = input.ToDynamicColor();

            return(fill);
        }
Пример #2
0
        private BitmapSource CreateImage(ImageGenerationContext context)
        {
            ValidateParameters();

            // First, we process layers which have a specific size.
            foreach (Layer layer in VisibleLayers)
            {
                if (layer.HasFixedSize)
                {
                    layer.Process(context);
                }
            }

            // Second, for SizeMode = Auto, we calculate the output dimensions
            // based on the union of all layers' (which have an explicit size) dimensions.
            int outputWidth, outputHeight;

            if (AutoSize)
            {
                Int32Rect outputDimensions = Int32Rect.Empty;
                foreach (Layer layer in VisibleLayers)
                {
                    // Calculate dimensions of layers; the dimensions of some layers may be omitted
                    // at design time and are then derived from layers which have a fixed size.
                    // Only include layer in bounds calculation if the Anchor property is None.
                    if (layer.Anchor == AnchorStyles.None)
                    {
                        Int32Rect?bounds = layer.Bounds;
                        if (bounds != null)
                        {
                            outputDimensions = Int32RectUtility.Union(outputDimensions, bounds.Value);
                        }
                    }
                }
                outputWidth  = outputDimensions.Width;
                outputHeight = outputDimensions.Height;
            }
            else
            {
                outputWidth  = Width.Value;
                outputHeight = Height.Value;
            }

            // If at this point we don't have a valid size, return - this means that
            // layers without an explicit size will never force an image to be created.
            if (outputWidth == 0 && outputHeight == 0)
            {
                return(null);
            }

            // Second, layers which don't have explicit sizes set, we now set their sizes
            // based on the overall composition size, and process the layer.
            foreach (Layer layer in this.VisibleLayers)
            {
                if (!layer.HasFixedSize)
                {
                    layer.CalculatedWidth  = outputWidth;
                    layer.CalculatedHeight = outputHeight;

                    layer.Process(context);
                }
            }

            // If any of the layers are not present, we don't create the image
            foreach (Layer layer in this.VisibleLayers)
            {
                if (layer.Bitmap == null)
                {
                    return(null);
                }
            }

            // Calculate X and Y for layers which are anchored.
            foreach (Layer layer in this.VisibleLayers)
            {
                if (layer.Bitmap != null && layer.Anchor != AnchorStyles.None)
                {
                    // Set X.
                    switch (layer.Anchor)
                    {
                    case AnchorStyles.BottomCenter:
                    case AnchorStyles.MiddleCenter:
                    case AnchorStyles.TopCenter:
                        layer.X = (outputWidth - layer.Size.Value.Width) / 2;
                        break;

                    case AnchorStyles.BottomLeft:
                    case AnchorStyles.MiddleLeft:
                    case AnchorStyles.TopLeft:
                        layer.X = Unit.GetCalculatedValue(layer.AnchorPadding, outputWidth - layer.Size.Value.Width);
                        break;

                    case AnchorStyles.BottomRight:
                    case AnchorStyles.MiddleRight:
                    case AnchorStyles.TopRight:
                        layer.X = outputWidth - layer.Size.Value.Width -
                                  Unit.GetCalculatedValue(layer.AnchorPadding, outputWidth - layer.Size.Value.Width);
                        break;
                    }

                    // Set Y.
                    switch (layer.Anchor)
                    {
                    case AnchorStyles.BottomCenter:
                    case AnchorStyles.BottomLeft:
                    case AnchorStyles.BottomRight:
                        layer.Y = outputHeight - layer.Size.Value.Height -
                                  Unit.GetCalculatedValue(layer.AnchorPadding, outputHeight - layer.Size.Value.Height);
                        break;

                    case AnchorStyles.MiddleCenter:
                    case AnchorStyles.MiddleLeft:
                    case AnchorStyles.MiddleRight:
                        layer.Y = (outputHeight - layer.Size.Value.Height) / 2;
                        break;

                    case AnchorStyles.TopCenter:
                    case AnchorStyles.TopLeft:
                    case AnchorStyles.TopRight:
                        layer.Y = Unit.GetCalculatedValue(layer.AnchorPadding, outputHeight - layer.Size.Value.Height);
                        break;
                    }
                }
            }

            // Apply fill.
            DrawingVisual  dv = new DrawingVisual();
            DrawingContext dc = dv.RenderOpen();

            // Apply fill.
            Fill.Apply(dc, new Rect(0, 0, outputWidth, outputHeight));

            dc.Close();

            // create output bitmap and lock data
            RenderTargetBitmap rtb = RenderTargetBitmapUtility.CreateRenderTargetBitmap(outputWidth, outputHeight);

            rtb.Render(dv);
            FastBitmap output = new FastBitmap(rtb);

            // Blend layers using specified blend mode.
            output = LayerBlender.BlendLayers(output, VisibleLayers);

            // Apply global filters.
            foreach (Filter filter in Filters)
            {
                if (filter.Enabled)
                {
                    filter.ApplyFilter(context, output);
                }
            }

            // If image format doesn't support transparency, make all transparent pixels totally opaque.
            // Otherwise WPF wants to save them as black.
            if (ImageFormat == DynamicImageFormat.Bmp || ImageFormat == DynamicImageFormat.Jpeg)
            {
                output.Lock();
                for (int y = 0; y < output.Height; ++y)
                {
                    for (int x = 0; x < output.Width; ++x)
                    {
                        var c = output[x, y];
                        //if (output[x, y].A == 0 && output[x, y].R == 0 && output[x, y].G == 0 && output[x, y].B == 0)
                        output[x, y] = SWMColor.FromArgb(255, c.R, c.G, c.B);
                    }
                }
                output.Unlock();
            }

            // Freeze the bitmap - this means any thread can access it, as well
            // as perhaps providing some performance benefit.
            output.InnerBitmap.Freeze();
            return(output.InnerBitmap);
        }