コード例 #1
0
        public void DrawMap(Map map)
        {
            var maxDepth = map.GetMaxDepth();

            for (var x = 0; x < map.Width; x++)
            {
                for (var y = 0; y < map.Height; y++)
                {
                    var cell = map.GetCell(new Point(x, y));

                    var drawing = new DrawingVisual();

                    SolidColorBrush brush;
                    if (cell.Depth > 0)
                    {
                        var depthCoefficient = cell.Depth / maxDepth;
                        var redValue         = 255 - 255 * depthCoefficient;
                        brush = new SolidColorBrush(Color.FromArgb(255, (byte)redValue, 255, 0));
                    }
                    else
                    {
                        brush = new SolidColorBrush(Color.FromArgb(64, 255, 0, 0));
                    }

                    using (var context = drawing.RenderOpen())
                    {
                        context.DrawRectangle(brush, new Pen(Brushes.Black, 2),
                                              new Rect(new GraphicsPoint(0, 0), new GraphicsPoint(Constants.CellSize, Constants.CellSize)));

                        var text = DrawingHelpers.GetFormattedText(cell.Depth.ToString(CultureInfo.CurrentCulture), 10, new SolidColorBrush(Colors.Black));
                        context.DrawText(text, new GraphicsPoint(10, 25));
                    }


                    var source = new RenderTargetBitmap(Constants.CellSize, Constants.CellSize, 96, 96, PixelFormats.Pbgra32);
                    source.Render(drawing);

                    var drawer = new DrawnObjectViewModel(source)
                    {
                        X = x * Constants.CellSize,
                        Y = y * Constants.CellSize
                    };
                    _drawableObjects.Add(drawer);
                }
            }
        }
コード例 #2
0
        private RenderTargetBitmap GetImage()
        {
            RenderTargetBitmap source = null;

            Application.Current.Dispatcher.Invoke(() =>
            {
                var drawing = new DrawingVisual();
                using (var context = drawing.RenderOpen())
                {
                    context.DrawRectangle(Brushes.Blue, null,
                                          new Rect(new Point(0, 0), new Point(Constants.CellSize, Constants.CellSize)));

                    var text = DrawingHelpers.GetFormattedText(_ship.MoveDirection.ToString(), 10, new SolidColorBrush(Colors.White));

                    context.DrawText(text, new Point(0, 0));
                }


                source =
                    new RenderTargetBitmap(Constants.CellSize, Constants.CellSize, 96, 96, PixelFormats.Pbgra32);
                source.Render(drawing);
            });
            return(source);
        }