Esempio n. 1
0
        public static async Task <D2D.Brush> ToSharpDX(
            this Jupiter.Media.Brush brush,
            D2D.RenderTarget renderTarget,
            RectangleF rect)
        {
            if (brush == null)
            {
                return(null);
            }

            var solidColorBrush = brush as Jupiter.Media.SolidColorBrush;

            if (solidColorBrush != null)
            {
                var color = solidColorBrush.Color.ToSharpDX();

                return(new D2D.SolidColorBrush(
                           renderTarget,
                           color,
                           new D2D.BrushProperties
                {
                    Opacity = (float)solidColorBrush.Opacity
                }));
            }

            var linearGradientBrush = brush as Jupiter.Media.LinearGradientBrush;

            if (linearGradientBrush != null)
            {
                var properties = new D2D.LinearGradientBrushProperties();
                //properties.StartPoint =
                //    new Vector2(
                //        (float)(linearGradientBrush.StartPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.StartPoint.Y * renderTarget.Size.Height));
                //properties.EndPoint =
                //    new Vector2(
                //        (float)(linearGradientBrush.EndPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.EndPoint.Y * renderTarget.Size.Height));
                properties.StartPoint =
                    new Vector2(
                        rect.Left + (float)(linearGradientBrush.StartPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.StartPoint.Y * rect.Height));
                properties.EndPoint =
                    new Vector2(
                        rect.Left + (float)(linearGradientBrush.EndPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.EndPoint.Y * rect.Height));

                var brushProperties = new D2D.BrushProperties();

                brushProperties.Opacity = (float)linearGradientBrush.Opacity;

                if (linearGradientBrush.Transform != null)
                {
                    brushProperties.Transform = linearGradientBrush.Transform.ToSharpDX();
                }

                var gradientStopCollection = linearGradientBrush.GradientStops.ToSharpDX(renderTarget);

                return(new D2D.LinearGradientBrush(
                           renderTarget,
                           properties,
                           brushProperties,
                           gradientStopCollection));
            }

            var imageBrush = brush as Jupiter.Media.ImageBrush;

            if (imageBrush != null)
            {
                var bitmap = await imageBrush.ImageSource.ToSharpDX(renderTarget);

                var       w         = bitmap.PixelSize.Width;
                var       h         = bitmap.PixelSize.Height;
                Matrix3x2 transform = Matrix3x2.Identity;

                switch (imageBrush.Stretch)
                {
                case Stretch.None:
                    transform.M31 += rect.Left + rect.Width * 0.5f - w / 2;
                    transform.M32 += rect.Top + rect.Height * 0.5f - h / 2;
                    break;

                case Stretch.Fill:
                    transform = Matrix3x2.Scaling(
                        rect.Width / w,
                        rect.Height / h);
                    transform.M31 += rect.Left;
                    transform.M32 += rect.Top;
                    break;

                case Stretch.Uniform:
                    var bitmapAspectRatio  = (float)w / h;
                    var elementAspectRatio = rect.Width / rect.Height;

                    if (bitmapAspectRatio > elementAspectRatio)
                    {
                        var scale = rect.Width / w;
                        transform      = Matrix3x2.Scaling(scale);
                        transform.M31 += rect.Left;
                        transform.M32 += rect.Top + rect.Height * 0.5f - scale * h / 2;
                    }
                    else     // (elementAspectRatio >= bitmapAspectRatio)
                    {
                        var scale = rect.Height / h;
                        transform      = Matrix3x2.Scaling(scale);
                        transform.M31 += rect.Left + rect.Width * 0.5f - scale * w / 2;
                        transform.M32 += rect.Top;
                    }

                    break;

                case Stretch.UniformToFill:
                    var bitmapAspectRatio2  = (float)w / h;
                    var elementAspectRatio2 = rect.Width / rect.Height;

                    if (bitmapAspectRatio2 > elementAspectRatio2)
                    {
                        var scale = rect.Height / h;
                        transform      = Matrix3x2.Scaling(scale);
                        transform.M31 += rect.Left + rect.Width * 0.5f - scale * w / 2;
                        transform.M32 += rect.Top;
                    }
                    else     // (elementAspectRatio >= bitmapAspectRatio)
                    {
                        var scale = rect.Width / w;
                        transform      = Matrix3x2.Scaling(scale);
                        transform.M31 += rect.Left;
                        transform.M32 += rect.Top + rect.Height * 0.5f - scale * h / 2;
                    }

                    break;
                }


                return(new D2D.BitmapBrush1(
                           (D2D.DeviceContext)renderTarget,
                           bitmap,
                           new D2D.BitmapBrushProperties1
                {
                    ExtendModeX = D2D.ExtendMode.Clamp,
                    ExtendModeY = D2D.ExtendMode.Clamp,
                    InterpolationMode = D2D.InterpolationMode.HighQualityCubic
                })
                {
                    Opacity = (float)imageBrush.Opacity,
                    Transform = transform
                });
                //    var writeableBitmap = imageBrush.ImageSource as WriteableBitmap;
                //    var bitmapImage = imageBrush.ImageSource as BitmapImage;

                //    if (bitmapImage != null)
                //    {
                //        writeableBitmap =
                //            await WriteableBitmapFromBitmapImageExtension.FromBitmapImage(bitmapImage);
                //    }
                //    CompositionEngine c;

                //    return new D2D.BitmapBrush(
                //        renderTarget,
                //        writeableBitmap.ToSharpDX(),
                //}
            }

#if DEBUG
            throw new NotSupportedException("Only SolidColorBrush supported for now");
#else
            return(new D2D.SolidColorBrush(renderTarget, Color.Transparent));
#endif
        }
Esempio n. 2
0
        public static async Task<D2D.Brush> ToSharpDX(
            this Jupiter.Media.Brush brush,
            D2D.RenderTarget renderTarget,
            RectangleF rect)
        {
            if (brush == null)
                return null;

            var solidColorBrush = brush as Jupiter.Media.SolidColorBrush;

            if (solidColorBrush != null)
            {
                var color = solidColorBrush.Color.ToSharpDX();
                
                return new D2D.SolidColorBrush(
                    renderTarget,
                    color,
                    new D2D.BrushProperties
                    {
                        Opacity = (float)solidColorBrush.Opacity
                    });
            }

            var linearGradientBrush = brush as Jupiter.Media.LinearGradientBrush;

            if (linearGradientBrush != null)
            {
                var properties = new D2D.LinearGradientBrushProperties();
                //properties.StartPoint =
                //    new Vector2(
                //        (float)(linearGradientBrush.StartPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.StartPoint.Y * renderTarget.Size.Height));
                //properties.EndPoint =
                //    new Vector2(
                //        (float)(linearGradientBrush.EndPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.EndPoint.Y * renderTarget.Size.Height));
                properties.StartPoint =
                    new Vector2(
                        rect.Left + (float)(linearGradientBrush.StartPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.StartPoint.Y * rect.Height));
                properties.EndPoint =
                    new Vector2(
                        rect.Left + (float)(linearGradientBrush.EndPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.EndPoint.Y * rect.Height));

                var brushProperties = new D2D.BrushProperties();

                brushProperties.Opacity = (float)linearGradientBrush.Opacity;

                if (linearGradientBrush.Transform != null)
                {
                    brushProperties.Transform = linearGradientBrush.Transform.ToSharpDX();
                }

                var gradientStopCollection = linearGradientBrush.GradientStops.ToSharpDX(renderTarget);

                return new D2D.LinearGradientBrush(
                    renderTarget,
                    properties,
                    brushProperties,
                    gradientStopCollection);
            }

            var imageBrush = brush as Jupiter.Media.ImageBrush;

            if (imageBrush != null)
            {
                var bitmap = await imageBrush.ImageSource.ToSharpDX(renderTarget);

                var w = bitmap.PixelSize.Width;
                var h = bitmap.PixelSize.Height;
                Matrix3x2 transform = Matrix3x2.Identity;

                switch (imageBrush.Stretch)
                {
                    case Stretch.None:
                        transform.M31 += rect.Left + rect.Width * 0.5f - w / 2;
                        transform.M32 += rect.Top + rect.Height * 0.5f - h / 2;
                        break;
                    case Stretch.Fill:
                        transform = Matrix3x2.Scaling(
                            rect.Width / w,
                            rect.Height / h);
                        transform.M31 += rect.Left;
                        transform.M32 += rect.Top;
                        break;
                    case Stretch.Uniform:
                        var bitmapAspectRatio = (float)w / h;
                        var elementAspectRatio = rect.Width / rect.Height;

                        if (bitmapAspectRatio > elementAspectRatio)
                        {
                            var scale = rect.Width / w;
                            transform = Matrix3x2.Scaling(scale);
                            transform.M31 += rect.Left;
                            transform.M32 += rect.Top + rect.Height * 0.5f - scale * h / 2;
                        }
                        else // (elementAspectRatio >= bitmapAspectRatio)
                        {
                            var scale = rect.Height / h;
                            transform = Matrix3x2.Scaling(scale);
                            transform.M31 += rect.Left + rect.Width * 0.5f - scale * w / 2;
                            transform.M32 += rect.Top;
                        }

                        break;
                    case Stretch.UniformToFill:
                        var bitmapAspectRatio2 = (float)w / h;
                        var elementAspectRatio2 = rect.Width / rect.Height;

                        if (bitmapAspectRatio2 > elementAspectRatio2)
                        {
                            var scale = rect.Height / h;
                            transform = Matrix3x2.Scaling(scale);
                            transform.M31 += rect.Left + rect.Width * 0.5f - scale * w / 2;
                            transform.M32 += rect.Top;
                        }
                        else // (elementAspectRatio >= bitmapAspectRatio)
                        {
                            var scale = rect.Width / w;
                            transform = Matrix3x2.Scaling(scale);
                            transform.M31 += rect.Left;
                            transform.M32 += rect.Top + rect.Height * 0.5f - scale * h / 2;
                        }

                        break;
                }
                

                return new D2D.BitmapBrush1(
                    (D2D.DeviceContext)renderTarget,
                    bitmap,
                    new D2D.BitmapBrushProperties1
                    {
                        ExtendModeX = D2D.ExtendMode.Clamp,
                        ExtendModeY = D2D.ExtendMode.Clamp,
                        InterpolationMode = D2D.InterpolationMode.HighQualityCubic
                    })
                    {
                        Opacity = (float)imageBrush.Opacity,
                        Transform = transform
                    };
                //    var writeableBitmap = imageBrush.ImageSource as WriteableBitmap;
                //    var bitmapImage = imageBrush.ImageSource as BitmapImage;

                //    if (bitmapImage != null)
                //    {
                //        writeableBitmap =
                //            await WriteableBitmapFromBitmapImageExtension.FromBitmapImage(bitmapImage);
                //    }
                //    CompositionEngine c;

                //    return new D2D.BitmapBrush(
                //        renderTarget,
                //        writeableBitmap.ToSharpDX(),
                //}
            }

#if DEBUG
            throw new NotSupportedException("Only SolidColorBrush supported for now");
#else
            return new D2D.SolidColorBrush(renderTarget, Color.Transparent);
#endif
        }
        public static D2D.Brush ToSharpDX(
            this Jupiter.Media.Brush brush,
            D2D.RenderTarget renderTarget,
            RectangleF rect)
        {
            if (brush == null)
            {
                return(null);
            }

            var solidColorBrush = brush as Jupiter.Media.SolidColorBrush;

            if (solidColorBrush != null)
            {
                var color = solidColorBrush.Color.ToSharpDX();

                return(new D2D.SolidColorBrush(
                           renderTarget,
                           color));
            }

            var linearGradientBrush = brush as Jupiter.Media.LinearGradientBrush;

            if (linearGradientBrush != null)
            {
                var properties = new D2D.LinearGradientBrushProperties();
                //properties.StartPoint =
                //    new DrawingPointF(
                //        (float)(linearGradientBrush.StartPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.StartPoint.Y * renderTarget.Size.Height));
                //properties.EndPoint =
                //    new DrawingPointF(
                //        (float)(linearGradientBrush.EndPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.EndPoint.Y * renderTarget.Size.Height));
                properties.StartPoint =
                    new DrawingPointF(
                        rect.Left + (float)(linearGradientBrush.StartPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.StartPoint.Y * rect.Height));
                properties.EndPoint =
                    new DrawingPointF(
                        rect.Left + (float)(linearGradientBrush.EndPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.EndPoint.Y * rect.Height));

                var brushProperties = new D2D.BrushProperties();

                brushProperties.Opacity = (float)linearGradientBrush.Opacity;

                if (linearGradientBrush.Transform != null)
                {
                    brushProperties.Transform = linearGradientBrush.Transform.ToSharpDX();
                }

                var gradientStopCollection = linearGradientBrush.GradientStops.ToSharpDX(renderTarget);

                return(new D2D.LinearGradientBrush(
                           renderTarget,
                           properties,
                           //brushProperties,
                           gradientStopCollection));
            }

            //var imageBrush = brush as Jupiter.Media.ImageBrush;

            //if (imageBrush != null)
            //{
            //    var writeableBitmap = imageBrush.ImageSource as WriteableBitmap;
            //    var bitmapImage = imageBrush.ImageSource as BitmapImage;

            //    if (bitmapImage != null)
            //    {
            //        writeableBitmap =
            //            await WriteableBitmapFromBitmapImageExtension.FromBitmapImage(bitmapImage);
            //    }
            //    CompositionEngine c;

            //    return new D2D.BitmapBrush(
            //        renderTarget,
            //        writeableBitmap.ToSharpDX(),
            //}

#if DEBUG
            throw new NotSupportedException("Only SolidColorBrush supported for now");
#else
            return(new D2D.SolidColorBrush(renderTarget, Color.Transparent));
#endif
        }
        public static D2D.Brush ToSharpDX(
            this Jupiter.Media.Brush brush,
            D2D.RenderTarget renderTarget,
            RectangleF rect)
        {
            if (brush == null)
                return null;

            var solidColorBrush = brush as Jupiter.Media.SolidColorBrush;

            if (solidColorBrush != null)
            {
                var color = solidColorBrush.Color.ToSharpDX();

                return new D2D.SolidColorBrush(
                    renderTarget,
                    color);
            }

            var linearGradientBrush = brush as Jupiter.Media.LinearGradientBrush;

            if (linearGradientBrush != null)
            {
                var properties = new D2D.LinearGradientBrushProperties();
                //properties.StartPoint =
                //    new DrawingPointF(
                //        (float)(linearGradientBrush.StartPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.StartPoint.Y * renderTarget.Size.Height));
                //properties.EndPoint =
                //    new DrawingPointF(
                //        (float)(linearGradientBrush.EndPoint.X * renderTarget.Size.Width),
                //        (float)(linearGradientBrush.EndPoint.Y * renderTarget.Size.Height));
                properties.StartPoint =
                    new DrawingPointF(
                        rect.Left + (float)(linearGradientBrush.StartPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.StartPoint.Y * rect.Height));
                properties.EndPoint =
                    new DrawingPointF(
                        rect.Left + (float)(linearGradientBrush.EndPoint.X * rect.Width),
                        rect.Top + (float)(linearGradientBrush.EndPoint.Y * rect.Height));

                var brushProperties = new D2D.BrushProperties();

                brushProperties.Opacity = (float)linearGradientBrush.Opacity;

                if (linearGradientBrush.Transform != null)
                {
                    brushProperties.Transform = linearGradientBrush.Transform.ToSharpDX();
                }

                var gradientStopCollection = linearGradientBrush.GradientStops.ToSharpDX(renderTarget);

                return new D2D.LinearGradientBrush(
                    renderTarget,
                    properties,
                    //brushProperties,
                    gradientStopCollection);
            }

            //var imageBrush = brush as Jupiter.Media.ImageBrush;

            //if (imageBrush != null)
            //{
            //    var writeableBitmap = imageBrush.ImageSource as WriteableBitmap;
            //    var bitmapImage = imageBrush.ImageSource as BitmapImage;

            //    if (bitmapImage != null)
            //    {
            //        writeableBitmap =
            //            await WriteableBitmapFromBitmapImageExtension.FromBitmapImage(bitmapImage);
            //    }
            //    CompositionEngine c;

            //    return new D2D.BitmapBrush(
            //        renderTarget,
            //        writeableBitmap.ToSharpDX(),
            //}

#if DEBUG
            throw new NotSupportedException("Only SolidColorBrush supported for now");
#else
            return new D2D.SolidColorBrush(renderTarget, Color.Transparent);
#endif
        }