Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to save Geometry as image?
    primarykey
    data
    text
    <p>How to save <code>Geometry</code> as image?</p> <p>For example i have <code>List&lt;Geometry&gt;</code>.</p> <p>I want it to be as follows:</p> <pre><code>for (int i = 0; i &lt; GeometryList.Count; i++) { Pen TestPen = new Pen(Brushes.Black, 1); GeometryDrawing TestDrawing = new GeometryDrawing(Brushes.Black, TestPen, TestGeometry); Bitmap b = TestDrawing as Bitmap; b.Save(System.AppDomain.CurrentDomain.BaseDirectory + i + ".png", ImageFormat.Png); } </code></pre> <p><strong>Update:</strong></p> <p>The code I wrote a few hours ago:</p> <pre><code> private void CreateFontMap(string PathTofont) { GlyphTypeface font = new GlyphTypeface(new Uri(PathTofont)); List&lt;ushort&gt; fontNum = new List&lt;ushort&gt;(); foreach (KeyValuePair&lt;int, ushort&gt; kvp in font.CharacterToGlyphMap) { fontNum.Add(kvp.Value); } if (fontNum.Count &gt; 0) { int mapWidth = 50 * 20; int mapHeight = 50 * (getRowNum(fontNum.Count + 1) + 1); Bitmap b = new Bitmap(mapWidth, mapHeight); Graphics g = Graphics.FromImage(b); System.Windows.Media.Pen glyphPen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Red, 1); Geometry glyphGeometry; for (int i = 0; i &lt; fontNum.Count; i++) { glyphGeometry = font.GetGlyphOutline(fontNum[i], 50, 1); RenderTargetBitmap bmp = new RenderTargetBitmap(50, 50, 96, 96, PixelFormats.Pbgra32); DrawingVisual viz = new DrawingVisual(); DrawingContext dc = viz.RenderOpen(); dc.DrawGeometry(System.Windows.Media.Brushes.Red, null, glyphGeometry); dc.Close(); bmp.Render(viz); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); MemoryStream myStream = new MemoryStream(); encoder.Save(myStream); int rowNum = (getRowNum(i)); g.DrawImage(System.Drawing.Bitmap.FromStream(myStream), new PointF((i - rowNum * 20) * 50, rowNum * 50)); } g.Dispose(); b.Save(System.AppDomain.CurrentDomain.BaseDirectory + "map.png", ImageFormat.Png); b.Dispose(); } } private int getRowNum(int p) { return p / 20; } </code></pre> <p>But instead of <img src="https://i.stack.imgur.com/4F0DY.png" alt="Img1">, I get <a href="https://i.stack.imgur.com/4pxXw.png" rel="nofollow noreferrer">Img2</a>.</p> <p><strong>Update 2:</strong> I changed this: </p> <pre><code>DrawingVisual viz = new DrawingVisual(); DrawingContext dc = viz.RenderOpen(); dc.DrawGeometry(System.Windows.Media.Brushes.Red, null, glyphGeometry); dc.Close(); </code></pre> <p>to:</p> <pre><code>DrawingVisual viz = new DrawingVisual(); DrawingContext dc = viz.RenderOpen(); dc.DrawImage(geometryImage, new Rect(0, 0, 50, 50)); dc.Close(); </code></pre> <p>and added:</p> <pre><code>glyphDrawing = new GeometryDrawing(System.Windows.Media.Brushes.Black, glyphPen, glyphGeometry); DrawingImage geometryImage = new DrawingImage(glyphDrawing); geometryImage.Freeze(); img1.Source = geometryImage; </code></pre> <p>And all working.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload