如何几何保存为图像? [英] How to save Geometry as image?

查看:178
本文介绍了如何几何保存为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何保存几何作为图像



例如,我有列表与LT?; 。几何>



我想这是如下:

 的for(int i = 0; I< GeometryList.Count;我++)
{
笔TestPen =新朋(Brushes.Black,1);
GeometryDrawing TestDrawing =新GeometryDrawing(Brushes.Black,TestPen,TestGeometry);

位图B = TestDrawing为位图;

b.Save(System.AppDomain.CurrentDomain.BaseDirectory + I +巴纽,ImageFormat.Png);
}



更新:



我写了几个小时前的代码:

 私人无效CreateFontMap(字符串PathTofont)
{

GlyphTypeface字体=新GlyphTypeface(新的URI(PathTofont));


名单,LT; USHORT> fontNum =新的List<&USHORT GT;();

的foreach(KeyValuePair< INT,USHORT> KVP在font.CharacterToGlyphMap)
{
fontNum.Add(kvp.Value);
}

如果(fontNum.Count大于0)
{
INT mapWidth = 50 * 20;
INT mapHeight = 50 *(getRowNum(fontNum.Count + 1)+ 1);

位图B =新位图(mapWidth,mapHeight);
图形G = Graphics.FromImage(B);

System.Windows.Media.Pen glyphPen =新System.Windows.Media.Pen(System.Windows.Media.Brushes.Red,1);
几何glyphGeometry;

的for(int i = 0; I< fontNum.Count;我++)
{
glyphGeometry = font.GetGlyphOutline(fontNum [I],50,1);

RenderTargetBitmap BMP =新RenderTargetBitmap(50,50,96,96,PixelFormats.Pbgra32);

DrawingVisual即=新DrawingVisual();
的DrawingContext直流= viz.RenderOpen();
dc.DrawGeometry(System.Windows.Media.Brushes.Red,空,glyphGeometry);
dc.Close();

bmp.Render(即)

PngBitmapEncoder编码器=新PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(BMP));

MemoryStream的myStream =新的MemoryStream();

encoder.Save(myStream);

INT的rowNum =(getRowNum(I));
g.DrawImage(System.Drawing.Bitmap.FromStream(myStream),新的PointF((I - 的rowNum * 20)* 50,*的rowNum 50));
}
g.Dispose();
b.Save(System.AppDomain.CurrentDomain.BaseDirectory +map.png,ImageFormat.Png);
b.Dispose();
}
}

私人诠释getRowNum(INT P)
{
返回P / 20;
}



但不是,我得到 IMG2



更新2:
我改变了这一点:

  DrawingVisual即=新DrawingVisual(); 
的DrawingContext直流= viz.RenderOpen();
dc.DrawGeometry(System.Windows.Media.Brushes.Red,空,glyphGeometry);
dc.Close();





  DrawingVisual即=新DrawingVisual(); 
的DrawingContext直流= viz.RenderOpen();
dc.DrawImage(geometryImage,新的Rect(0,0,50,50));
dc.Close();

和补充:

  glyphDrawing =新GeometryDrawing(System.Windows.Media.Brushes.Black,glyphPen,glyphGeometry); 
DrawingImage geometryImage =新DrawingImage(glyphDrawing);
geometryImage.Freeze();
img1.Source = geometryImage;

和所有的工作。


解决方案

  //创建我们将呈现到
RenderTargetBitmap BMP = $位图b $ b新RenderTargetBitmap(100,100,//尺寸
96,96,// DPI
PixelFormats.Pbgra32);

//创建随机圈几何
名单℃的名单;几何> geoList =新的List<几何与GT;();
随机兰特=新的随机();
的for(int i = 0;我小于10;我++)
{
双半径= rand.Next(5,10);
点中心=新的点(rand.Next(25,75),rand.Next(25,75));
geoList.Add(新EllipseGeometry(圆心,半径,半径));
}
//将绘制几何图形
DrawingVisual即=新DrawingVisual轻巧的视觉元素();使用
(DC的DrawingContext = viz.RenderOpen())
{//直流让我们绘制到DrawingVisual直接
的foreach(在geoList变种G)
dc.DrawGeometry( Brushes.Red,空,G);
} //直流是因为它掉出来using语句

封闭//绘制可视位图上的
bmp.Render(即)

//实例化一个编码器来保存文件
PngBitmapEncoder包括PNGEncoder =新PngBitmapEncoder();
//添加此位设置帧的
pngEncoder.Frames.Add(BitmapFrame.Create(BMP))的编码器;

//使用(的FileStream文件=新的FileStream(Spots.png,FileMode.Create))
pngEncoder.Save位图保存为PNG文件
(文件);

根据您的意见,以上面的部分,它看起来像你想创建一个表对字形字体和出保存为图像文件。这里是你如何做到这一点:

  //我产生了不同的字形进行测试。 
//我的fontName =宋体
字样的脸=新的字体(的fontName)测试;
GlyphTypeface字体;

如果
回报率(face.TryGetGlyphTypeface(出字体)!); //保释金,如果出现错误

INT列数= 10;
INT MaxDrawCount = 30; //使用int.MaxValue吸引他们所有
双fontSize的= 50D;
//每个单元的高度必须包括在/ underhanging字形
规格= CELLSIZE新的大小(fontSize的,fontSize的* font.Height);

变种字形从glyphIndex =在font.CharacterToGlyphMap.Values
选择font.GetGlyphOutline(glyphIndex,fontSize的,1D);

//现在创建我们将它们画到
DrawingVisual即=新DrawingVisual视觉();
INT drawCount = -1;使用
(DC的DrawingContext = viz.RenderOpen())
{
的foreach(在字形变种G)
{
drawCount ++;
如果(drawCount> = MaxDrawCount)
中断; //不画比你更想
如果(g.IsEmpty())继续; //不水平绘制空白那些
//中心在细胞
双xOffset =(drawCount%列数)* cellSize.Width + cellSize.Width / 2D - g.Bounds.Width / 2D ;
//把角色对细胞
双yOffset =(drawCount /列数)* cellSize.Height + fontSize的* font.Baseline的基线;
dc.PushTransform(新TranslateTransform(xOffset,yOffset));
dc.DrawGeometry(Brushes.Red,空,G);
dc.Pop(); //摆脱变换
}
}

INT行数= drawCount /列数;
如果(drawCount%列数!= 0)
行数++; //包括部分行,
INT位宽=(int)的Math.Ceiling(cellSize.Width *列数);
INT bitHeight =(int)的Math.Ceiling(cellSize.Height *行数);
RenderTargetBitmap BMP =新RenderTargetBitmap(
位宽,bitHeight,
96,96,
PixelFormats.Pbgra32);
bmp.Render(即)

PngBitmapEncoder编码器=新PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(BMP));
使用(的FileStream文件=新的FileStream(FontTable.png,FileMode.Create))
encoder.Save(文件);


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.

解决方案

// Create the bitmap we'll render to
RenderTargetBitmap bmp = 
            new RenderTargetBitmap(100, 100, // Size
                                                    96, 96, // DPI 
                                                    PixelFormats.Pbgra32);

// Create a list of random circle geometries
List<Geometry> geoList = new List<Geometry>();
Random rand = new Random();
for (int i=0; i<10; i++)
{
    double radius = rand.Next(5, 10);
    Point center = new Point(rand.Next(25, 75), rand.Next(25,75));
    geoList.Add(new EllipseGeometry(center, radius, radius));
}
// The light-weight visual element that will draw the geometries
DrawingVisual viz = new DrawingVisual();
using (DrawingContext dc = viz.RenderOpen())
{ // The DC lets us draw to the DrawingVisual directly
    foreach (var g in geoList)
        dc.DrawGeometry(Brushes.Red, null, g);
} // the DC is closed as it falls out of the using statement

// draw the visual on the bitmap
bmp.Render(viz);

// instantiate an encoder to save the file
PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
// add this bitmap to the encoders set of frames
pngEncoder.Frames.Add(BitmapFrame.Create(bmp));

// save the bitmap as an .png file
using (FileStream file = new FileStream("Spots.png", FileMode.Create))
    pngEncoder.Save(file);

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'm generating the glyphs differently for testing.
// I tested with fontName="Arial"
Typeface face = new Typeface(fontName);
GlyphTypeface font;

if (!face.TryGetGlyphTypeface(out font))
    return; // bail if something goes wrong

int ColumnCount = 10;
int MaxDrawCount = 30; // use int.MaxValue to draw them all            
double fontSize = 50d;
// the height of each cell has to include over/underhanging glyphs
Size cellSize = new Size(fontSize, fontSize * font.Height);

var Glyphs = from glyphIndex in font.CharacterToGlyphMap.Values
                    select font.GetGlyphOutline(glyphIndex, fontSize, 1d);            

// now create the visual we'll draw them to
DrawingVisual viz = new DrawingVisual();
int drawCount = -1;
using (DrawingContext dc = viz.RenderOpen())
{
    foreach (var g in Glyphs)
    {
        drawCount++;
        if (drawCount >= MaxDrawCount)
            break; // don't draw more than you want
        if (g.IsEmpty()) continue; // don't draw the blank ones
        // center horizontally in the cell
        double xOffset = (drawCount % ColumnCount) * cellSize.Width + cellSize.Width / 2d - g.Bounds.Width / 2d;
        // place the character on the baseline of the cell
        double yOffset = (drawCount / ColumnCount) * cellSize.Height + fontSize * font.Baseline;
        dc.PushTransform(new TranslateTransform(xOffset, yOffset));
        dc.DrawGeometry(Brushes.Red, null, g);
        dc.Pop(); // get rid of the transform
    }
}

int RowCount = drawCount / ColumnCount;
if (drawCount % ColumnCount != 0) 
    RowCount++; // to include partial rows
int bitWidth = (int)Math.Ceiling(cellSize.Width * ColumnCount);
int bitHeight = (int)Math.Ceiling(cellSize.Height * RowCount);
RenderTargetBitmap bmp = new RenderTargetBitmap(
                                                bitWidth, bitHeight,
                                                96, 96,
                                                PixelFormats.Pbgra32);
bmp.Render(viz);

PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
using (FileStream file = new FileStream("FontTable.png", FileMode.Create))
    encoder.Save(file);

这篇关于如何几何保存为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆