Пример #1
0
        BitmapBrush CreateGridPatternBrush(RenderTarget pRenderTarget)
        {
            // Create a compatible render target.
            BitmapRenderTarget spCompatibleRenderTarget =
                pRenderTarget.CreateCompatibleRenderTarget(CompatibleRenderTargetOptions.None, (new SizeF(10.0f, 10.0f)));

            // Draw a pattern.
            SolidColorBrush spGridBrush =
                spCompatibleRenderTarget.CreateSolidColorBrush(
                    new ColorF(0.93f, 0.94f, 0.96f, 1.0f));

            spCompatibleRenderTarget.BeginDraw();

            spCompatibleRenderTarget.FillRectangle(new RectF(0.0f, 0.0f, 10.0f, 1.0f), spGridBrush);
            spCompatibleRenderTarget.FillRectangle(new RectF(0.0f, 0.1f, 1.0f, 10.0f), spGridBrush);
            spCompatibleRenderTarget.EndDraw();

            // Retrieve the bitmap from the render target.
            D2DBitmap spGridBitmap = spCompatibleRenderTarget.Bitmap;

            // Choose the tiling mode for the bitmap brush.
            BitmapBrushProperties brushProperties =
                new BitmapBrushProperties(ExtendMode.Wrap, ExtendMode.Wrap, BitmapInterpolationMode.Linear);

            // Create the bitmap brush.
            return(textureRenderTarget.CreateBitmapBrush(spGridBitmap, brushProperties));
        }
Пример #2
0
        /// <summary>
        /// Creates and returns a copy of the brush in the new render target.
        /// Used for changing render targets.
        /// A brush belongs to a render target, so when you want to draw with same brush in another render target
        /// - you need to create a copy of the brush in the new render target.
        /// </summary>
        /// <param name="sourceBrush">The brush.</param>
        /// <param name="newRenderTarget">The new render target.</param>
        /// <returns></returns>
        protected internal Brush CopyBrushToRenderTarget(Brush sourceBrush, RenderTarget newRenderTarget)
        {
            if (sourceBrush == null || newRenderTarget == null)
            {
                return(null);
            }
            Brush newBrush;

            if (sourceBrush is SolidColorBrush)
            {
                newBrush = newRenderTarget.CreateSolidColorBrush(
                    ((SolidColorBrush)sourceBrush).Color,
                    new BrushProperties(sourceBrush.Opacity, sourceBrush.Transform));
                sourceBrush.Dispose();
                return(newBrush);
            }
            if (sourceBrush is LinearGradientBrush)
            {
                var oldGSC = ((LinearGradientBrush)sourceBrush).GradientStops;
                var newGSC = newRenderTarget.CreateGradientStopCollection(oldGSC, oldGSC.ColorInterpolationGamma, oldGSC.ExtendMode);
                oldGSC.Dispose();
                newBrush = newRenderTarget.CreateLinearGradientBrush(
                    new LinearGradientBrushProperties(
                        ((LinearGradientBrush)sourceBrush).StartPoint,
                        ((LinearGradientBrush)sourceBrush).EndPoint),
                    newGSC,
                    new BrushProperties(sourceBrush.Opacity, sourceBrush.Transform));
                sourceBrush.Dispose();
                return(newBrush);
            }
            if (sourceBrush is RadialGradientBrush)
            {
                var oldGSC = ((RadialGradientBrush)sourceBrush).GradientStops;
                var newGSC = newRenderTarget.CreateGradientStopCollection(oldGSC, oldGSC.ColorInterpolationGamma, oldGSC.ExtendMode);
                oldGSC.Dispose();
                newBrush = newRenderTarget.CreateRadialGradientBrush(
                    new RadialGradientBrushProperties(
                        ((RadialGradientBrush)sourceBrush).Center,
                        ((RadialGradientBrush)sourceBrush).GradientOriginOffset,
                        ((RadialGradientBrush)sourceBrush).RadiusX,
                        ((RadialGradientBrush)sourceBrush).RadiusY),
                    newGSC,
                    new BrushProperties(sourceBrush.Opacity, sourceBrush.Transform));
                sourceBrush.Dispose();
                return(newBrush);
            }
            if (sourceBrush is BitmapBrush)
            {
                newBrush = newRenderTarget.CreateBitmapBrush(
                    Bitmap,
                    new BitmapBrushProperties(
                        ((BitmapBrush)sourceBrush).ExtendModeX,
                        ((BitmapBrush)sourceBrush).ExtendModeY,
                        ((BitmapBrush)sourceBrush).InterpolationMode),
                    new BrushProperties(sourceBrush.Opacity, sourceBrush.Transform));
                sourceBrush.Dispose();
                return(newBrush);
            }
            throw new NotImplementedException("Unknown brush type used");
        }
Пример #3
0
        protected override void CreateResources()
        {
            _sceneBrush = RenderTarget.CreateSolidColorBrush(Color.Black);

            // Background grid brush
            var bitmapTarget = RenderTarget.CreateCompatibleRenderTarget(new SizeF(10, 10));
            var gridBrush    = bitmapTarget.CreateSolidColorBrush(new ColorF(.93f, .94f, .96f));

            bitmapTarget.BeginDraw();
            bitmapTarget.FillRectangle(RectangleF.FromLTRB(0, 0, 10, 1), gridBrush);
            bitmapTarget.FillRectangle(RectangleF.FromLTRB(0, 0, 1, 10), gridBrush);
            bitmapTarget.EndDraw().ThrowIfFailed();
            IBitmap bitmap = bitmapTarget.GetBitmap();

            _gridPatternBrush = RenderTarget.CreateBitmapBrush(bitmap, new BitmapBrushProperties(ExtendMode.Wrap, ExtendMode.Wrap));

            // Gradient brush
            Span <GradientStop> stops = stackalloc[]
            {
                new GradientStop(0.0f, Color.Gold),
                new GradientStop(0.85f, new ColorF(Color.Orange, 0.8f)),
                new GradientStop(1.0f, new ColorF(Color.OrangeRed, 0.7f))
            };

            _radialGradientBrush = RenderTarget.CreateRadialGradientBrush(
                new RadialGradientBrushProperties(new PointF(330, 330), new PointF(140, 140), 140, 140),
                RenderTarget.CreateGradienStopCollection(stops));
        }
Пример #4
0
        private void addBitmapBrushBotton_Click(object sender, EventArgs e)
        {
            ExtendMode ex = extendedModeXComboBox.SelectedIndex > 0 ? (ExtendMode)extendedModeXComboBox.SelectedIndex : ExtendMode.Wrap;
            ExtendMode ey = extendedModeYComboBox.SelectedIndex > 0 ? (ExtendMode)extendedModeYComboBox.SelectedIndex : ExtendMode.Wrap;

            D2DBitmap   brushBitmap = BitmapUtilities.LoadBitmapFromFile(renderTarget, parent.wicFactory, imageFilename);
            BitmapBrush brush       = renderTarget.CreateBitmapBrush(
                brushBitmap,
                new BitmapBrushProperties(
                    ex, ey,
                    BitmapInterpolationMode.NearestNeighbor),
                new BrushProperties(
                    opacity,
                    Matrix3x2F.Identity));

            parent.brushes.Add(brush);
            parent.currentBrushIndex = parent.brushes.Count - 1;
            FillBrushesListBox();
        }
Пример #5
0
        private BitmapBrush RandomBitmapBrush()
        {
#if _D2DTRACE
            Trace.WriteLine("SolidBrush:");
#endif
            BitmapInterpolationMode interpolationMode = Random.NextDouble() < 0.25 ? BitmapInterpolationMode.Linear : BitmapInterpolationMode.NearestNeighbor;
#if _D2DTRACE
            Trace.WriteLine("BitmapInterpolationMode: " + interpolationMode);
#endif
            BitmapBrush ret = RenderTarget.CreateBitmapBrush(
                Bitmap,
                new BitmapBrushProperties(
                    RandomExtendMode(),
                    RandomExtendMode(),
                    interpolationMode),
                new BrushProperties(
                    RandomOpacity(),
                    RandomMatrix3x2()));
            return(ret);
        }
 /// <summary>
 /// Creates and returns a copy of the brush in the new render target.
 /// Used for changing render targets.
 /// A brush belongs to a render target, so when you want to draw with same brush in another render target
 /// - you need to create a copy of the brush in the new render target.
 /// </summary>
 /// <param name="sourceBrush">The brush.</param>
 /// <param name="newRenderTarget">The new render target.</param>
 /// <returns></returns>
 protected internal Brush CopyBrushToRenderTarget(Brush sourceBrush, RenderTarget newRenderTarget)
 {
     if (sourceBrush == null || newRenderTarget == null)
         return null;
     Brush newBrush;
     if (sourceBrush is SolidColorBrush)
     {
         newBrush = newRenderTarget.CreateSolidColorBrush(
             ((SolidColorBrush)sourceBrush).Color,
             new BrushProperties(sourceBrush.Opacity, sourceBrush.Transform));
         sourceBrush.Dispose();
         return newBrush;
     }
     if (sourceBrush is LinearGradientBrush)
     {
         var oldGSC = ((LinearGradientBrush)sourceBrush).GradientStops;
         var newGSC = newRenderTarget.CreateGradientStopCollection(oldGSC, oldGSC.ColorInterpolationGamma, oldGSC.ExtendMode);
         oldGSC.Dispose();
         newBrush = newRenderTarget.CreateLinearGradientBrush(
             new LinearGradientBrushProperties(
                 ((LinearGradientBrush)sourceBrush).StartPoint,
                 ((LinearGradientBrush)sourceBrush).EndPoint),
                 newGSC,
                 new BrushProperties(sourceBrush.Opacity, sourceBrush.Transform));
         sourceBrush.Dispose();
         return newBrush;
     }
     if (sourceBrush is RadialGradientBrush)
     {
         var oldGSC = ((RadialGradientBrush)sourceBrush).GradientStops;
         var newGSC = newRenderTarget.CreateGradientStopCollection(oldGSC, oldGSC.ColorInterpolationGamma, oldGSC.ExtendMode);
         oldGSC.Dispose();
         newBrush = newRenderTarget.CreateRadialGradientBrush(
             new RadialGradientBrushProperties(
                 ((RadialGradientBrush)sourceBrush).Center,
                 ((RadialGradientBrush)sourceBrush).GradientOriginOffset,
                 ((RadialGradientBrush)sourceBrush).RadiusX,
                 ((RadialGradientBrush)sourceBrush).RadiusY),
                 newGSC,
                 new BrushProperties(sourceBrush.Opacity, sourceBrush.Transform));
         sourceBrush.Dispose();
         return newBrush;
     }
     if (sourceBrush is BitmapBrush)
     {
         newBrush = newRenderTarget.CreateBitmapBrush(
             Bitmap,
             new BitmapBrushProperties(
                 ((BitmapBrush)sourceBrush).ExtendModeX,
                 ((BitmapBrush)sourceBrush).ExtendModeY,
                 ((BitmapBrush)sourceBrush).InterpolationMode),
             new BrushProperties(sourceBrush.Opacity, sourceBrush.Transform));
         sourceBrush.Dispose();
         return newBrush;
     }
     throw new NotImplementedException("Unknown brush type used");
 }