using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfMosaic { /// <summary> /// Interaction logic for FlowerShape.xaml /// </summary> public class FlowerShape : Grid { /// <summary> /// /// </summary> /// <param name="count"></param> public FlowerShape(int count) { Width = Height = 100; Background = new SolidColorBrush(Colors.Black); var strokeBrush= new SolidColorBrush(Colors.White); this.Children.Clear(); var bg = new SolidColorBrush(Color.FromArgb(255, (byte)Utils.rnd.Next(0, 256), (byte)Utils.rnd.Next(0, 256), (byte)Utils.rnd.Next(0, 256))); var width = Utils.rnd.Next(10, 51); for (int i = 0; i < count; i++) { var angle = i * (360 / count); var transGroup = new TransformGroup(); transGroup.Children.Add(new RotateTransform() { Angle = angle }); this.Children.Add(new Ellipse() { StrokeThickness = 1, Stroke = strokeBrush, Margin = new Thickness(0, 0, 0, 50), Fill = bg, Width = width, RenderTransformOrigin = new Point(0.5, 1) , RenderTransform = transGroup }); ; ; ; } } } }
void TesCreateShapes() { canvas.Children.Clear(); for (int i = 0; i < 100; i++) { var shap = new FlowerShape(12){ }; canvas.Children.Add(shap ); } canvas.UpdateLayout(); return; foreach (FrameworkElement shap in canvas.Children) { UISaveToPng(shap, @"C:UsersgwangPicturesMosaic" + DateTime.Now.ToFileTime() + ".png"); } Title = "done."; } void UISaveToPng(FrameworkElement ui, string fileName) { int width = (int)ui.ActualWidth; int height = (int)ui.ActualHeight; RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Pbgra32); bmp.Render(ui); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); using (FileStream fs = new FileStream(fileName, FileMode.Create)) { encoder.Save(fs); fs.Close(); } }