public static void DrawGeometry( this IPixelCanvas pc, int x, int y, Geometry geometry, Brush fillBrush, Pen pen, BlendMode blendMode) { var size = geometry.GetRenderBounds(pen).Size; var width = (int)size.Width; var height = (int)size.Height; var bmp = geometry.RenderToBitmap( size, new Point(), size, fillBrush, pen, PixelFormats.Pbgra32); int[] pixels; if (blendMode == BlendMode.Copy && width == pc.Width && height == pc.Height) { pixels = pc.GetPixels(); bmp.CopyPixels(pixels, pc.Stride, 0); } else { var pc2 = new PixelArrayCanvas(width, height); pixels = pc2.GetPixels(); bmp.CopyPixels(pixels, pc2.Stride, 0); pc.Blit( new Rectangle(x, y, width, height), pc2, new Rectangle(0, 0, width, height), 255, 255, 255, 255, blendMode); } }
public static void DrawVisual(this IPixelCanvas pc, int x, int y, ContainerVisual visual, BlendMode blendMode) { if (visual.ContentBounds.IsEmpty) return; var width = (int)visual.ContentBounds.Width; var height = (int)visual.ContentBounds.Height; var bmp = visual.RenderToBitmap(visual.ContentBounds.Size, new Point(0, 0)); if (blendMode == BlendMode.Copy && width == pc.Width && height == pc.Height) { var pac = pc as PixelArrayCanvas; if(pac == null) { throw new NotSupportedException("BlendMode.Copy is only supported with PixelArrayCanvas"); } bmp.CopyPixels(pac.Pixels, pc.Stride, 0); } else { var sw = Stopwatch.StartNew(); var pc2 = new PixelArrayCanvas(width, height); bmp.CopyPixels(pc2.Pixels, pc2.Stride, 0); Debug.WriteLine("Copy Pixels " + sw.GetElapsedAndRestart().TotalMilliseconds); pc.Blit( new Rectangle(x, y, width, height), pc2, new Rectangle(0, 0, width, height), 255, 255, 255, 255, blendMode); Debug.WriteLine("Blit " + sw.GetElapsedAndRestart().TotalMilliseconds); } }
public static void DrawLine(this IPixelCanvas pc, int x1, int y1, int x2, int y2, System.Windows.Media.Color color, double width) { var l = new Shapes.Line(); l.X1 = x1; l.Y1 = y1; l.X2 = x2; l.Y2 = y2; l.Stroke = new SolidColorBrush(color); l.StrokeThickness = width; l.Arrange(pc.Bounds.ToRect()); var sw = Stopwatch.StartNew(); var bitmap_source = l.RenderToBitmap(pc.Bounds.ToSize(), new Point(x1, y1)); var pixels = new int[pc.Length]; bitmap_source.CopyPixels(pixels, pc.Stride, 0); var pc2 = new PixelArrayCanvas(pc.Width, pc.Height); pc2.ReplaceFromPixels(pixels, pc.Width, pc.Height); pc.Blit(pc2, BlendMode.Alpha); }