如何将几何图形保存为图像?
如何将Geometry
另存为图像?
例如,我有 List
。
我希望它如下:
for (int i = 0; i < 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);
}
更新:
我几个小时前编写的代码:
private void CreateFontMap(string PathTofont)
{
GlyphTypeface font = new GlyphTypeface(new Uri(PathTofont));
List<ushort> fontNum = new List<ushort>();
foreach (KeyValuePair<int, ushort> kvp in font.CharacterToGlyphMap)
{
fontNum.Add(kvp.Value);
}
if (fontNum.Count > 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 < 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;
}
但不是 ,我得到Img2。
更新2: 我将其更改
DrawingVisual viz = new DrawingVisual();
DrawingContext dc = viz.RenderOpen();
dc.DrawGeometry(System.Windows.Media.Brushes.Red, null, glyphGeometry);
dc.Close();
为:
DrawingVisual viz = new DrawingVisual();
DrawingContext dc = viz.RenderOpen();
dc.DrawImage(geometryImage, new Rect(0, 0, 50, 50));
dc.Close();
并添加:
glyphDrawing = new GeometryDrawing(System.Windows.Media.Brushes.Black, glyphPen, glyphGeometry);
DrawingImage geometryImage = new DrawingImage(glyphDrawing);
geometryImage.Freeze();
img1.Source = geometryImage;
一切正常。
How to save Geometry
as image?
For example i have List<Geometry>
.
I want it to be as follows:
for (int i = 0; i < 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);
}
Update:
The code I wrote a few hours ago:
private void CreateFontMap(string PathTofont)
{
GlyphTypeface font = new GlyphTypeface(new Uri(PathTofont));
List<ushort> fontNum = new List<ushort>();
foreach (KeyValuePair<int, ushort> kvp in font.CharacterToGlyphMap)
{
fontNum.Add(kvp.Value);
}
if (fontNum.Count > 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 < 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;
}
But instead of , I get Img2.
Update 2:
I changed this:
DrawingVisual viz = new DrawingVisual();
DrawingContext dc = viz.RenderOpen();
dc.DrawGeometry(System.Windows.Media.Brushes.Red, null, glyphGeometry);
dc.Close();
to:
DrawingVisual viz = new DrawingVisual();
DrawingContext dc = viz.RenderOpen();
dc.DrawImage(geometryImage, new Rect(0, 0, 50, 50));
dc.Close();
and added:
glyphDrawing = new GeometryDrawing(System.Windows.Media.Brushes.Black, glyphPen, glyphGeometry);
DrawingImage geometryImage = new DrawingImage(glyphDrawing);
geometryImage.Freeze();
img1.Source = geometryImage;
And all working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于任何想要渲染以固定大小为中心的几何体的人,这是执行此操作的代码:
For anyone who is wanting to render geometry centered in a fixed size, this is the code to do it:
根据您对上面部分的评论,您似乎正在尝试为字体创建字形表并将其保存到图像文件中。以下是实现此目的的方法:
Based on your comments to the section above, it looks like you're trying to create a table of glyphs for a font and save it out to an image file. Here's how you accomplish this:
我用这个:
I use this: