Exemplo n.º 1
0
        /// <summary>
        /// Create the grid pattern (squares) brush
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        BitmapBrush CreateGridPatternBrush(RenderTarget target)
        {
            // Create a compatible render target.
            BitmapRenderTarget compatibleRenderTarget =
                target.CreateCompatibleRenderTarget(CompatibleRenderTargetOptions.None, new SizeF(10.0f, 10.0f));

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

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

            //// Retrieve the bitmap from the render target.
            D2DBitmap spGridBitmap;

            spGridBitmap = compatibleRenderTarget.Bitmap;

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

            //// Create the bitmap brush.
            return(renderTarget.CreateBitmapBrush(spGridBitmap, brushProperties));
        }
Exemplo n.º 2
0
        private void Render(bool shouldRecompose, bool shouldRetry)
        {
            var interpolationMode = false ? BitmapInterpolationMode.Linear : BitmapInterpolationMode.NearestNeighbor;

            _windowRenderTarget.BeginDraw();

            if (BorderVisible)
            {
                CreateCompositeRenderTarget();

                _compositeRenderTarget.BeginDraw();
                _compositeRenderTarget.Clear(_clearColor);
                _compositeRenderTarget.DrawBitmap(_screenRenderTarget.Bitmap, new RawRectangleF(48, 40, 48 + 160, 40 + 144), 1.0f, BitmapInterpolationMode.NearestNeighbor);
                _compositeRenderTarget.DrawBitmap(_borderBitmap, 1.0f, BitmapInterpolationMode.NearestNeighbor);
                _compositeRenderTarget.EndDraw();
                _windowRenderTarget.DrawBitmap(_compositeRenderTarget.Bitmap, _drawRectangle, 1.0f, interpolationMode);
            }
            else
            {
                _windowRenderTarget.DrawBitmap(_screenRenderTarget.Bitmap, _drawRectangle, 1.0f, interpolationMode);
            }

            try { _windowRenderTarget.EndDraw(); }
            catch (COMException)
            {
                // If needed, try to recreate the target.
                ResetRendering();
                // Try to render again, but only once. (We don't want to enter an infinite recursion… AKA Stack Overflow)
                if (shouldRetry)
                {
                    Render(true, false);
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Adds a shape to compatible render target - avoids the need to redraw all shapes.
 /// </summary>
 /// <param name="shape">The shape.</param>
 private void AddToCompatibleRenderTarget(DrawingShape shape)
 {
     lock (renderSyncObject)
     {
         bitmapRenderTarget.BeginDraw();
         shape.Draw(bitmapRenderTarget);
         bitmapRenderTarget.EndDraw();
     }
 }
Exemplo n.º 4
0
        private void DrawLayerOnBitmap()
        {
            bitmapRt.BeginDraw();
            bitmapRt.Clear();

            lock (Locker)
            {
                foreach (var e in elements)
                {
                    e.Draw(bitmapRt);
                }
            }
            bitmapRt.EndDraw();
        }
Exemplo n.º 5
0
        private void UpdateScreenAndPresent(object state)
        {
            SwapBitmaps();

            _screenBitmap1.CopyFromMemory(_screenBuffer, 160 * 4);

            _screenRenderTarget.BeginDraw();
            _screenRenderTarget.DrawBitmap(_screenBitmap2, new RawRectangleF(0, 0, 160, 144), 1.0f, BitmapInterpolationMode.NearestNeighbor);
            _screenRenderTarget.DrawBitmap(_screenBitmap1, new RawRectangleF(0, 0, 160, 144), 0.5f, BitmapInterpolationMode.NearestNeighbor);
            _screenRenderTarget.EndDraw();

            Render(true);

            HandleCompletion(state);
        }
Exemplo n.º 6
0
        protected override void Render(RenderTarget rt)
        {
            if (image == null)
            {
                base.Render(rt);
                return;
            }
            rt.Clear(this.SceneColorBrush.Color);

            rt.Transform = Matrix3x2.Identity;

            Size2 imageSize = this.image.PixelSize;

            double scale       = Math.Min((double)(ClientSize.Width - ImagePadding) / imageSize.Width, (double)(ClientSize.Height - ImagePadding) / imageSize.Height);
            int    imageWidth  = (int)(imageSize.Width * scale);
            int    imageHeight = (int)(imageSize.Height * scale);

            var rcBounds = new RectangleF(0, 0, imageSize.Width, imageSize.Height);
            var rcImage  = new RectangleF((ClientSize.Width - imageWidth) / 2, (ClientSize.Height - imageHeight) / 2, imageWidth, imageHeight);

            bool isBGRAImage = image.PixelFormat.Format == SharpDX.DXGI.Format.B8G8R8A8_UNorm;

            if (!isBGRAImage)
            {
                var sz = new Size2F(imageSize.Width, imageSize.Height);
                var rc = new RectangleF(0, 0, imageSize.Width, imageSize.Height);

                if (bgraImageRenderer == null || bgraImageRenderer.Size != sz)
                {
                    SharpDX.Utilities.Dispose(ref bgraImageRenderer);
                    bgraImageRenderer = new BitmapRenderTarget(RenderTarget2D, CompatibleRenderTargetOptions.None, sz);
                }

                bgraImageRenderer.BeginDraw();
                bgraImageRenderer.Clear(Color.Black);
                bgraImageRenderer.AntialiasMode = AntialiasMode.Aliased;
                bgraImageRenderer.FillOpacityMask(this.image, whiteBrush, OpacityMaskContent.Graphics, rc, rc);
                bgraImageRenderer.EndDraw();
            }

            rt.DrawBitmap(isBGRAImage ? image : bgraImageRenderer.Bitmap, rcImage, 1, BitmapInterpolationMode.NearestNeighbor, rcBounds);

            if (ImagePadding != 0)
            {
                rt.DrawRectangle(rcImage, whiteBrush, 1);
            }
        }
Exemplo n.º 7
0
        public ImageBrushImpl(
            ImageBrush brush,
            SharpDX.Direct2D1.RenderTarget target,
            Size targetSize)
        {
            if (brush.Source == null)
            {
                return;
            }

            var image            = ((BitmapImpl)brush.Source.PlatformImpl).GetDirect2DBitmap(target);
            var imageSize        = new Size(brush.Source.PixelWidth, brush.Source.PixelHeight);
            var tileMode         = brush.TileMode;
            var sourceRect       = brush.SourceRect.ToPixels(imageSize);
            var destinationRect  = brush.DestinationRect.ToPixels(targetSize);
            var scale            = brush.Stretch.CalculateScaling(destinationRect.Size, sourceRect.Size);
            var translate        = CalculateTranslate(brush, sourceRect, destinationRect, scale);
            var intermediateSize = CalculateIntermediateSize(tileMode, targetSize, destinationRect.Size);
            var brtOpts          = CompatibleRenderTargetOptions.None;

            // TODO: There are times where we don't need to draw an intermediate bitmap. Identify
            // them and directly use 'image' in those cases.
            using (var intermediate = new BitmapRenderTarget(target, brtOpts, intermediateSize))
            {
                Rect drawRect;
                var  transform = CalculateIntermediateTransform(
                    tileMode,
                    sourceRect,
                    destinationRect,
                    scale,
                    translate,
                    out drawRect);

                intermediate.BeginDraw();
                intermediate.PushAxisAlignedClip(drawRect.ToDirect2D(), AntialiasMode.Aliased);
                intermediate.Transform = transform.ToDirect2D();
                intermediate.DrawBitmap(image, 1, BitmapInterpolationMode.Linear);
                intermediate.PopAxisAlignedClip();
                intermediate.EndDraw();

                this.PlatformBrush = new BitmapBrush(
                    target,
                    intermediate.Bitmap,
                    GetBitmapBrushProperties(brush),
                    GetBrushProperties(brush, destinationRect));
            }
        }
Exemplo n.º 8
0
        void RenderD2DContentIntoSurface()
        {
            SizeF rtSize = renderTarget.Size;

            renderTarget.BeginDraw();

            if (!isOpacityRTPopulated)
            {
                opacityRenderTarget.BeginDraw();

                opacityRenderTarget.Transform = Matrix3x2F.Identity;

                opacityRenderTarget.Clear(new ColorF(GetColorValues(System.Windows.Media.Colors.Black), 0));

                opacityRenderTarget.DrawText(
                    text,
                    textFormat,
                    new RectF(
                        0,
                        0,
                        rtSize.Width,
                        rtSize.Height
                        ),
                    textBrush
                    );

                opacityRenderTarget.EndDraw();

                isOpacityRTPopulated = true;
            }

            renderTarget.Clear(new ColorF(GetColorValues(System.Windows.Media.Colors.Black)));

            renderTarget.AntiAliasMode = AntiAliasMode.Aliased;

            D2DBitmap spBitmap = opacityRenderTarget.Bitmap;

            renderTarget.FillOpacityMask(
                spBitmap,
                textBrush,
                OpacityMaskContent.TextNatural,
                new RectF(0, 0, rtSize.Width, rtSize.Height),
                new RectF(0, 0, rtSize.Width, rtSize.Height)
                );

            renderTarget.EndDraw();
        }
Exemplo n.º 9
0
        public virtual Bitmap RenderRegionBitmap(RectangleF view, int zoomLevel)
        {
            if (Coordinates == null || Values == null || Coordinates.Count == 0 || Values.Count == 0)
            {
                return(null);
            }

            RenderRegion(view, zoomLevel);
            _bitmapRenderTarget.BeginDraw();
            _bitmapRenderTarget.DrawEllipse(new Ellipse(new Vector2(128f, 128f), 32f, 32f), ((MM_Network_Map_DX)_surface).Brushes.GetBrush(Color.White), 3f);
            _bitmapRenderTarget.EndDraw();
            // _bitmapRenderTarget.draw
            var tileBmp = new Bitmap(_bitmapRenderTarget,
                                     new Size2(MM_Repository.OverallDisplay.MapTileSize.Width, MM_Repository.OverallDisplay.MapTileSize.Height),
                                     new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), 96, 96));

            tileBmp.CopyFromBitmap(_bitmapRenderTarget.Bitmap);

            return(tileBmp);
        }
Exemplo n.º 10
0
        public BitmapBrush makeBitmapBrush(string imgName, RenderTarget renderTarget, bool blankImage = false)
        {
            //TODO : 여기 바꿔라!
            string imageSrc = "";

            if (blankImage)
            {
                SharpDX.Direct2D1.PixelFormat pf = new SharpDX.Direct2D1.PixelFormat()
                {
                    AlphaMode = D3DHandler.ALPHA_MODE,
                    Format    = D3DHandler.RENDER_FORMAT
                };

                BitmapRenderTarget pallete = new BitmapRenderTarget(renderTarget, CompatibleRenderTargetOptions.GdiCompatible, pf);
                return(new BitmapBrush(renderTarget, pallete.Bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
            if (resourceCore.getFile(imgName, out ResFile resFile))
            {
                ImagingFactory imagingFactory = new ImagingFactory();

                /*NativeFileStream fileStream = new NativeFileStream(imageSrc,
                 *  NativeFileMode.Open, NativeFileAccess.Read);*/
                MemoryStream  ms            = new MemoryStream(resFile.rawData);
                BitmapDecoder bitmapDecoder = new BitmapDecoder(imagingFactory, ms, DecodeOptions.CacheOnDemand);

                BitmapFrameDecode frame = bitmapDecoder.GetFrame(0);

                FormatConverter converter = new FormatConverter(imagingFactory);
                converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);

                Bitmap bitmap = Bitmap.FromWicBitmap(renderTarget, converter);

                Utilities.Dispose(ref bitmapDecoder);
                Utilities.Dispose(ref imagingFactory);
                Utilities.Dispose(ref converter);

                return(new BitmapBrush(renderTarget, bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
            else
            {
                Console.WriteLine("{0} missing", imageSrc);

                SharpDX.Direct2D1.PixelFormat pf = new SharpDX.Direct2D1.PixelFormat()
                {
                    AlphaMode = D3DHandler.ALPHA_MODE,
                    Format    = D3DHandler.RENDER_FORMAT
                };
                BitmapRenderTarget pallete = new BitmapRenderTarget(renderTarget, CompatibleRenderTargetOptions.GdiCompatible, new Size2F(30f, 30f), new Size2(1, 1), pf);

                pallete.BeginDraw();
                pallete.Clear(Color.Purple);
                pallete.EndDraw();

                return(new BitmapBrush(renderTarget, pallete.Bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
        }
Exemplo n.º 11
0
        public static BitmapBrush makeBitmapBrush(RenderTarget renderTarget, string imgName, bool blankImage = false)
        {
            string imageSrc = Program.spriteFileDir + imgName;

            if (blankImage)
            {
                var pf = new SharpDX.Direct2D1.PixelFormat()
                {
                    AlphaMode = SharpDX.Direct2D1.AlphaMode.Premultiplied,
                    Format    = Format.B8G8R8A8_UNorm
                };

                BitmapRenderTarget pallete = new BitmapRenderTarget(renderTarget, CompatibleRenderTargetOptions.GdiCompatible, pf);
                return(new BitmapBrush(renderTarget, pallete.Bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
            if (File.Exists(imageSrc))
            {
                ImagingFactory   imagingFactory = new ImagingFactory();
                NativeFileStream fileStream     = new NativeFileStream(imageSrc,
                                                                       NativeFileMode.Open, NativeFileAccess.Read);
                BitmapDecoder bitmapDecoder = new BitmapDecoder(imagingFactory, fileStream, DecodeOptions.CacheOnDemand);

                BitmapFrameDecode frame = bitmapDecoder.GetFrame(0);

                FormatConverter converter = new FormatConverter(imagingFactory);
                converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);

                Bitmap bitmap = Bitmap.FromWicBitmap(renderTarget, converter);

                Utilities.Dispose(ref bitmapDecoder);
                Utilities.Dispose(ref fileStream);
                Utilities.Dispose(ref imagingFactory);
                Utilities.Dispose(ref converter);

                return(new BitmapBrush(renderTarget, bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
            else
            {
                Console.WriteLine("{0} missing", imageSrc);

                var pf = new SharpDX.Direct2D1.PixelFormat()
                {
                    AlphaMode = SharpDX.Direct2D1.AlphaMode.Premultiplied,
                    Format    = Format.B8G8R8A8_UNorm
                };
                BitmapRenderTarget pallete = new BitmapRenderTarget(renderTarget, CompatibleRenderTargetOptions.GdiCompatible, new Size2F(30f, 30f), new Size2(1, 1), pf);

                pallete.BeginDraw();
                pallete.Clear(Color.Purple);
                pallete.EndDraw();

                return(new BitmapBrush(renderTarget, pallete.Bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
        }
Exemplo n.º 12
0
        public static Brush GetPatternBrush(RenderTarget renderTarget, System.Drawing.Color fillColor, float opacity, xPFT.Charting.Base.FillPattern fillPattern, float patternSize = 10)
        {
            if (renderTarget != null)
            {
                BitmapRenderTarget brt = new BitmapRenderTarget(renderTarget, CompatibleRenderTargetOptions.None, new SharpDX.Size2F(patternSize, patternSize));
                Brush tempBrush        = new SolidColorBrush(brt, xPFT.DrawingBase.Convertor.ColorConvertor(fillColor, opacity));
                if (fillPattern == xPFT.Charting.Base.FillPattern.Solid)
                {
                    return(tempBrush);
                }
                brt.BeginDraw();
                brt.Clear(Color.Transparent);
                switch (fillPattern)
                {
                case xPFT.Charting.Base.FillPattern.Hachure_Double:
                    brt.DrawLine(new SharpDX.Vector2(0, 0), new SharpDX.Vector2(patternSize, patternSize), tempBrush, 1);
                    brt.DrawLine(new SharpDX.Vector2(patternSize, 0), new SharpDX.Vector2(0, patternSize), tempBrush, 1);
                    break;

                case xPFT.Charting.Base.FillPattern.Hachure:
                    brt.DrawLine(new SharpDX.Vector2(patternSize, 0), new SharpDX.Vector2(0, patternSize), tempBrush, 0.5f);
                    break;

                case xPFT.Charting.Base.FillPattern.Hashure_Back:
                    brt.DrawLine(new SharpDX.Vector2(0, 0), new SharpDX.Vector2(patternSize, patternSize), tempBrush, 0.5f);
                    break;

                case xPFT.Charting.Base.FillPattern.Dash:
                    brt.DrawLine(new SharpDX.Vector2(0, patternSize / 4), new SharpDX.Vector2(patternSize / 4, patternSize / 4), tempBrush, 0.5f);
                    brt.DrawLine(new SharpDX.Vector2(patternSize * 3 / 4, patternSize / 4), new SharpDX.Vector2(patternSize, patternSize / 4), tempBrush, 0.5f);
                    brt.DrawLine(new SharpDX.Vector2(patternSize / 4, patternSize * 3 / 4), new SharpDX.Vector2(patternSize * 3 / 4, patternSize * 3 / 4), tempBrush, 0.5f);
                    break;

                case xPFT.Charting.Base.FillPattern.Squre:
                    brt.DrawLine(new SharpDX.Vector2(0, patternSize / 2), new SharpDX.Vector2(patternSize, patternSize / 2), tempBrush, 0.5f);
                    brt.DrawLine(new SharpDX.Vector2(patternSize / 2, 0), new SharpDX.Vector2(patternSize / 2, patternSize), tempBrush, 0.5f);
                    break;

                case xPFT.Charting.Base.FillPattern.Dot:
                    brt.FillEllipse(new SharpDX.Direct2D1.Ellipse(new SharpDX.Vector2(patternSize / 4, patternSize / 4), 1, 1), tempBrush);
                    brt.FillEllipse(new SharpDX.Direct2D1.Ellipse(new SharpDX.Vector2(patternSize / 4, patternSize * 3 / 4), 1, 1), tempBrush);
                    brt.FillEllipse(new SharpDX.Direct2D1.Ellipse(new SharpDX.Vector2(patternSize * 3 / 4, patternSize / 4), 1, 1), tempBrush);
                    brt.FillEllipse(new SharpDX.Direct2D1.Ellipse(new SharpDX.Vector2(patternSize * 3 / 4, patternSize * 3 / 4), 1, 1), tempBrush);
                    break;

                case xPFT.Charting.Base.FillPattern.Zig_Zag_Horizontal:
                    brt.DrawLine(new SharpDX.Vector2(0, patternSize / 2), new SharpDX.Vector2(patternSize / 2, 0), tempBrush, 0.5f);
                    brt.DrawLine(new SharpDX.Vector2(patternSize, patternSize / 2), new SharpDX.Vector2(patternSize / 2, 0), tempBrush, 0.5f);
                    brt.DrawLine(new SharpDX.Vector2(0, patternSize), new SharpDX.Vector2(patternSize / 2, patternSize / 2), tempBrush, 0.5f);
                    brt.DrawLine(new SharpDX.Vector2(patternSize, patternSize), new SharpDX.Vector2(patternSize / 2, patternSize / 2), tempBrush, 0.5f);
                    break;

                case xPFT.Charting.Base.FillPattern.Zig_Zag_Vertical:
                    brt.DrawLine(new SharpDX.Vector2(patternSize / 2, 0), new SharpDX.Vector2(0, patternSize / 2), tempBrush, 0.5f);
                    brt.DrawLine(new SharpDX.Vector2(patternSize / 2, patternSize), new SharpDX.Vector2(0, patternSize / 2), tempBrush, 0.5f);
                    brt.DrawLine(new SharpDX.Vector2(patternSize, 0), new SharpDX.Vector2(patternSize / 2, patternSize / 2), tempBrush, 0.5f);
                    brt.DrawLine(new SharpDX.Vector2(patternSize, patternSize), new SharpDX.Vector2(patternSize / 2, patternSize / 2), tempBrush, 0.5f);
                    break;
                }
                brt.EndDraw();
                BitmapBrushProperties bmpbp = new BitmapBrushProperties();
                bmpbp.ExtendModeX       = ExtendMode.Wrap;
                bmpbp.ExtendModeY       = ExtendMode.Wrap;
                bmpbp.InterpolationMode = BitmapInterpolationMode.Linear;
                BitmapBrush br = new BitmapBrush(brt, brt.Bitmap, bmpbp);
                tempBrush.Dispose();
                brt.Dispose();
                return(br);
            }
            else
            {
                return(null);
            }
        }