public static int[] CreateDottedImage(int width, int height) { Random rnd = new Random(); const int pointsNum = 100000; const double radius = 1.5; var randomPoints = Enumerable.Range(0, pointsNum).Select(_ => new Point(rnd.NextDouble() * width, rnd.NextDouble() * height)); randomPoints = Filter(randomPoints, radius); DrawingGroup drawing = new DrawingGroup(); var dc = drawing.Append(); foreach (var point in randomPoints) { HsbColor color = new HsbColor(0, 0, Math.Round(5 * rnd.NextDouble()) / 4); SolidColorBrush brush = new SolidColorBrush(color.ToArgbColor()); //drawing.Children.Add(new GeometryDrawing(brush, null, new EllipseGeometry(point, radius, radius))); dc.DrawEllipse(brush, null, point, radius, radius); } dc.Close(); DrawingImage drawingImage = new DrawingImage(); drawingImage.Drawing = drawing; drawingImage.Freeze(); if ((imageCreatingThread.ThreadState | ThreadState.Running) != imageCreatingThread.ThreadState) imageCreatingThread.Start(); imageQueue.Add(new RequestInfo { Width = width, Heigth = height, DrawingImage = drawingImage }); var pixels = resultQueue.Take(); return pixels; }
public void SetImage() { var text = String.IsNullOrWhiteSpace(textBox.Text) ? "Enter some text above" : textBox.Text; //preview.Source = Renderer.RenderImage(text); const int width = 300; var words = new List<TextSegment>(); var output = new DrawingGroup(); //TextLines always include a line terminator, even for the last line in the string. string fullText = text + "\n"; int lineStart = 0; //In characters double top = 0; //In pixels foreach (var line in Measurer.MeasureLines(text, width, format, output)) { int lastSpace = lineStart; while (lastSpace < lineStart + line.Length - 1) { while (lastSpace < lineStart + line.Length && Char.IsWhiteSpace(fullText[lastSpace])) lastSpace++; //Skip over the previous chunk of whitespace if (lastSpace == lineStart + line.Length) continue; //If the line ends in whitespace, skip it entirely //Find the next space within this line int nextSpace = fullText.IndexOfAny(whitespaceChars, lastSpace + 1, line.Length - (lastSpace + 1 - lineStart)); if (nextSpace < 0) //Include the last word, even if it doesn't end with a space. nextSpace = lineStart + line.Length - 1; //if (nextSpace == lastSpace) continue; //Entirely Skip double spaces var word = text.Substring(lastSpace, nextSpace - lastSpace); var bounds = line.GetTextBounds(lastSpace, word.Length); //bounds is relative to the line words.Add(new TextSegment(Rect.Offset(bounds[0].Rectangle, 0, top), word)); lastSpace = nextSpace; } lineStart += line.Length; top += line.Height; line.Dispose(); } using (var dc = output.Append()) { var pen = new Pen(new SolidColorBrush(Color.FromArgb(128, 0, 0, 255)), 1); foreach (var word in words) { dc.DrawRectangle(null, pen, word.Bounds); } } preview.Source = new DrawingImage(output); }