Blob doesn`t match tracking object - c#

hi,
I need to use color tracking.
The blob does not match my target object when I run my code in surface pro 4.
but the code is correct when I test it on my Acer.
anyone knows that why?
public void forobject(Bitmap image)//put setted color value into image1
{
BlobCounter blobCounter = new BlobCounter();
blobCounter.MinWidth = 5;
blobCounter.MinHeight = 5;
blobCounter.FilterBlobs = true;
blobCounter.ObjectsOrder = ObjectsOrder.Size;
//Grayscale griFiltre = new Grayscale(0.2125, 0.7154, 0.0721);
//Grayscale griFiltre = new Grayscale(0.2, 0.2, 0.2);
//Bitmap griImage = griFiltre.Apply(image);
BitmapData objectsData = image.LockBits(new Rectangle(0, 0, image.Width/2, image.Height/2), ImageLockMode.ReadOnly, image.PixelFormat);
// grayscaling
Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData));
// unlock image
image.UnlockBits(objectsData);
blobCounter.ProcessImage(image);
Rectangle[] rects = blobCounter.GetObjectsRectangles();
Blob[] blobs = blobCounter.GetObjectsInformation();
pictureBox_tracking.Image = image;//????
if (rdiobtn_singletracking.Checked)
{
// Single Tracking--------
foreach (Rectangle recs in rects)
{
if (rects.Length > 0)
{
Rectangle objectRect = rects[0];
//Graphics g = Graphics.FromImage(image);
Graphics g = pictureBox_real.CreateGraphics();
using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
{
g.DrawRectangle(pen, objectRect);
}
//Drawn by the rectangle coordinates is taken away.
//int objectX = objectRect.X; //+ (objectRect.Width / 2);
//int objectY = objectRect.Y; //+ (objectRect.Height / 2);
x = objectRect.X;
y = objectRect.Y;
w = objectRect.Width;
h = objectRect.Height;
// g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1));
g.Dispose();
}

Related

How to set fontweight?

I'm writing a simple application in c# .net core 6 and winform to "assemble" some pngs and write some text on it.
This is the Photoshop output. The Title "Baldur the invincible" is wrote in Diablo Light when i'm writing it with DrawString with Font("Diablo", 30, FontStyle.Regular)
and this is my output with the Font.
In photoshop i used Diablo Light font but in the list of system fonts, from C# code, i see just Diablo Regular.. and the result is different.
How i can set the fontweight ?
here some code:
// ---------------------------------------------------------------------------------------------- +
// All TEXT ------------------------------------------------------------------------------------- +
// ---------------------------------------------------------------------------------------------- +
{
//string familyName;
//string familyList = "";
//FontFamily[] fontFamilies;
//InstalledFontCollection installedFontCollection = new InstalledFontCollection();
//fontFamilies = installedFontCollection.Families;
Font title_font = new Font("Diablo", 30, FontStyle.Regular);
Font sub_font = new Font("Diablo", 25, FontStyle.Regular);
List<Font> list_fonts = new List<Font>();
list_fonts.Add(title_font);
list_fonts.Add(sub_font);
RectangleF tit_rect = new RectangleF(165, 70, w - (165 * 2), h);
//RectangleF sub_rect = new RectangleF(165, 82 + 30, w - (165 * 2), h);
var v = m_dict_titles["Title"];
string curr_string = tabbed_value[v].ToUpper();
float offsety = WriteTitleTextf2(map_back, 0, TL(curr_string, false), tit_rect, list_fonts, StringAlignment.Near, 5);
}
public void WriteOnBitmapf(Bitmap bmp, int col, Font font, String text, RectangleF rectf, StringAlignment st_align, float stringi = 0.0f)
{
float size = font.Size;
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
StringFormat format = new StringFormat()
{
Alignment = st_align,
LineAlignment = StringAlignment.Near
};
Brush br = col == 0 ? new SolidBrush(Color.FromArgb(0, 0, 0)) : new SolidBrush(Color.FromArgb(255, 255, 255));
g.DrawString(text, font, br, rectf, format);
g.Flush();
}

System.Drawing quality issue with large offset matrix

I've run into an issue with drawing text and basic graphic drawing operations not having the proper placement & quality when the drawing matrix has large offset values. I've tried number SmoothMode, InterpolationMode, & TextRenderingHint options with no luck.
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public void RenderImageClosePointDrawing()
{
float x = 68336, y = 99460;
PointF anchorPoint = new PointF(17494176, 25461836);
PointF anchorPoint2= new PointF(17494076, 25461836);
string textLabel = "9318";
float textFontSize = 20;
float symbolsize = 34;
string fontFamly = "Arial";
Bitmap bitmap = new Bitmap(256, 256);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
graphics.Transform = new Matrix(1, 0, 0, 1, -x * 256, -y * 256);
//Draw the circle
Pen polyPen = new Pen(new SolidBrush(Color.Black), 2);
Brush polyBrush = new SolidBrush(Color.Teal);
graphics.DrawEllipse(polyPen, anchorPoint.X, anchorPoint.Y, symbolsize, symbolsize);
graphics.FillEllipse(polyBrush, anchorPoint.X, anchorPoint.Y, symbolsize, symbolsize);
RectangleF drawnArea = new RectangleF(anchorPoint.X, anchorPoint.Y, symbolsize, symbolsize);
Pen polyPen2 = new Pen(new SolidBrush(Color.Black), 1);
Brush polyBrush2 = new SolidBrush(Color.Teal);
graphics.DrawEllipse(polyPen2, anchorPoint2.X, anchorPoint2.Y, symbolsize, symbolsize);
graphics.FillEllipse(polyBrush2, anchorPoint2.X, anchorPoint2.Y, symbolsize, symbolsize);
RectangleF drawnArea2 = new RectangleF(anchorPoint2.X, anchorPoint2.Y, symbolsize, symbolsize);
Pen polyPen3 = new Pen(new SolidBrush(Color.Red), 1);
graphics.DrawRectangle(polyPen3, drawnArea.X, drawnArea.Y, drawnArea.Width, drawnArea.Height);
graphics.DrawRectangle(polyPen3, drawnArea2.X, drawnArea2.Y, drawnArea2.Width, drawnArea2.Height);
//Draw the text
Pen textOutlinePen = new Pen(new SolidBrush(Color.Orange), (float)4);
textOutlinePen.EndCap = LineCap.Round;
textOutlinePen.LineJoin = LineJoin.Round;
textOutlinePen.MiterLimit = 0;
Brush textFillBrush = new SolidBrush(Color.Teal);
FontFamily textFontFamily = new FontFamily(fontFamly);
PointF textAnchor = new PointF(anchorPoint.X, anchorPoint.Y);
ShiftTextAnchor_NW(textLabel, textFontSize, ref drawnArea, textFontFamily, ref textAnchor);
var textPath = new GraphicsPath();
textPath.AddString(textLabel,
textFontFamily,
(int)FontStyle.Bold,
textFontSize,
textAnchor,
new StringFormat()
);
graphics.DrawPath(textOutlinePen, textPath);
graphics.FillPath(textFillBrush, textPath);
//Draw the text2
Pen textOutlinePen2 = new Pen(new SolidBrush(Color.Orange), (float)1);
textOutlinePen.EndCap = LineCap.Round;
textOutlinePen.LineJoin = LineJoin.Round;
textOutlinePen.MiterLimit = 0;
PointF textAnchor2 = new PointF(anchorPoint2.X, anchorPoint2.Y);
ShiftTextAnchor_NW(textLabel, textFontSize, ref drawnArea2, textFontFamily, ref textAnchor2);
var textPath2 = new GraphicsPath();
textPath2.AddString(textLabel,
textFontFamily,
(int)FontStyle.Bold,
textFontSize,
textAnchor2,
new StringFormat()
);
graphics.DrawPath(textOutlinePen2, textPath2);
graphics.FillPath(textFillBrush, textPath2);
}
bitmap.Save(#"C:\ClosePointDrawing.png", ImageFormat.Png);
}
private static void ShiftTextAnchor_NW(string textLabel, float textFontSize, ref RectangleF drawnArea, FontFamily textFontFamily, ref PointF textAnchor)
{
GraphicsPath tempPath = new GraphicsPath();
tempPath.AddString(
textLabel,
textFontFamily,
(int)FontStyle.Bold,
textFontSize,
textAnchor,
new StringFormat()
);
var textBounds = tempPath.GetBounds();
var offsetX = textBounds.X - textAnchor.X;
var offsetY = textBounds.Y - textAnchor.Y;
textAnchor = new PointF(drawnArea.Left - (textBounds.Width + offsetX), drawnArea.Top - (textBounds.Height + offsetY));
}
When you run this code you'll get this for output: ClosePointDrawing.png
You'll notice that the text doesn't not look nice (distorted some) and also that the Black stroke outline around the circle is only lined up properly with its Teal color filled circle with the one on the left when using a 1 pixel stroke. The one on the right uses a 2 pixel stroke. You'll also see that the Red Squares are not lined up with the Teal Circle, they should be completely encapsulating it.
Now if you change the first few values in the code so that it doesn't use a large offset as follows:
float x = 0, y = 0;
PointF anchorPoint = new PointF(150, 50);
PointF anchorPoint2 = new PointF(50, 50);
You'll get this for output: ClosePointDrawing2.png
Notice that the Text looks much better, and that the strokes are perfectly lined up with the filled circles as well as the red squares.
Is there anything that can be done, so that it will render it properly with the larger matrix?

How to apply more than one filter to the image using Aforge

I can filter red and blue color separately but i want to filter both at the same time.. so my code is like this
//FOR RED COLOR
ColorFiltering filter = new ColorFiltering();
filter.Red = new IntRange(100, 255);
filter.Green = new IntRange(0, 75);
filter.Blue = new IntRange(0, 75);
filter.ApplyInPlace(image1);
MyDraw(image1);
// FOR BLUE COLOR
EuclideanColorFiltering filter2 = new EuclideanColorFiltering();
filter2.CenterColor = new RGB(Color.FromArgb(9, 39, 101));
filter2.Radius = 50;
filter2.ApplyInPlace(image1);
MyDraw(image1);
public void MyDraw(Bitmap image)
{
BlobCounter blobCounter = new BlobCounter();
blobCounter.MinWidth = 2;
blobCounter.MinHeight = 2;
blobCounter.FilterBlobs = true;
blobCounter.ObjectsOrder = ObjectsOrder.Size;
Grayscale grayFilter = new Grayscale(0.2125, 0.7154, 0.0721);
Bitmap grayImage = grayFilter.Apply(image);
blobCounter.ProcessImage(grayImage);
Rectangle[] rects = blobCounter.GetObjectsRectangles();
foreach (Rectangle recs in rects)
{
if (rects.Length > 0)
{
Rectangle objectRect = rects[0];
//Graphics g = Graphics.FromImage(image);
Graphics g = pictureBox1.CreateGraphics();
reception = "Cam," + objectRect.X + "," + objectRect.Y;
Console.WriteLine("X: " + objectRect.X + " Y:" + objectRect.Y.ToString());
using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
{
g.DrawRectangle(pen, objectRect);
}
int objectX = objectRect.X + (objectRect.Width / 2);
int objectY = objectRect.Y + (objectRect.Height / 2);
g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1));
g.Dispose();
}
}
}
So i want to recognize blue and red shapes on webcam and draw a rectangle around recognized shape. For now, I can do it as red or blue. But I want to recognize red and blue colors at the same time
how can i add multiple filters?

Auto crop a rectangle

I want to auto crop the image which I took it from tablet camera. Actually I want to take the picture of id cards and need to auto corp it, I am using black background but sometimes it is too glaze and there is a reflection also, at that time its not cropped properly. Can anyone assist me with c# code to crop the images(Note : I am using phone cameras to take the image of id cards, mostly the background will be black and sometime it will be some other things).
Below I mentioned my code also:
private void ProcessImage(Bitmap bitmap)
{
// lock image
BitmapData bitmapData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite, bitmap.PixelFormat);
// step 1 - turn background to black
ColorFiltering colorFilter = new ColorFiltering();
colorFilter.Red = new IntRange(0, 64);
colorFilter.Green = new IntRange(0, 64);
colorFilter.Blue = new IntRange(0, 64);
colorFilter.FillOutsideRange = false;
colorFilter.ApplyInPlace(bitmapData);
// step 2 - locating objects
BlobCounter blobCounter = new BlobCounter();
blobCounter.FilterBlobs = true;
blobCounter.MinHeight = 5;
blobCounter.MinWidth = 5;
blobCounter.ProcessImage(bitmapData);
Blob[] blobs = blobCounter.GetObjectsInformation();
bitmap.UnlockBits(bitmapData);
// step 3 - check objects' type and highlight
SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
Graphics g = Graphics.FromImage(bitmap);
Pen yellowPen = new Pen(Color.Yellow, 2); // circles
Pen redPen = new Pen(Color.Red, 2); // quadrilateral
Pen brownPen = new Pen(Color.Brown, 2); // quadrilateral with known sub-type
Pen greenPen = new Pen(Color.Green, 2); // known triangle
Pen bluePen = new Pen(Color.Blue, 2); // triangle
for (int i = 0, n = blobs.Length; i < n; i++)
{
List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
DoublePoint center;
double radius;
List<IntPoint> corners;
// is triangle or quadrilateral
if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
{
// get sub-type
PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);
Pen pen;
if (subType == PolygonSubType.Rectangle)
{
pen = (corners.Count == 4) ? redPen : bluePen;
}
else
{
pen = (corners.Count == 4) ? brownPen : greenPen;
}
//g.DrawPolygon( pen, ToPointsArray( corners ) );
if (pen.Color.Name == "Red")
{
int intdummy = 0;
g.DrawRectangle(pen, corners[0].X, corners[0].Y, 1150, 750);
//Code for corpping///////
pictureBox2.Image = bitmap;
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
Bitmap croppedBitmap = new Bitmap("E:\\xxx\\Vicas Tablet\\WinImager\\cropimage\\cropimage\\Testing Images\\Image1.jpg");
//croppedBitmap = croppedBitmap.Clone(new Rectangle(corners[0].X, corners[0].Y,1150,750),System.Drawing.Imaging.PixelFormat.DontCare);
croppedBitmap = croppedBitmap.Clone(
new Rectangle(corners[0].X, corners[0].Y, ((int)corners[1].X - (int)corners[0].X), ((int)corners[2].Y - (int)corners[1].Y)), System.Drawing.Imaging.PixelFormat.DontCare);
pictureBox2.Image = croppedBitmap;
////End code for cropping///////////
}
}

How to save Geometry as image?

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.
For anyone who is wanting to render geometry centered in a fixed size, this is the code to do it:
const int TargetSize = 14;
private static void Save(Geometry geometry, string fileName)
{
var rect = geometry.GetRenderBounds(new Pen(Brushes.Black, 0));
var bigger = rect.Width > rect.Height ? rect.Width : rect.Height;
var scale = TargetSize / bigger;
Geometry scaledGeometry = Geometry.Combine(geometry, geometry, GeometryCombineMode.Intersect, new ScaleTransform(scale, scale));
rect = scaledGeometry.GetRenderBounds(new Pen(Brushes.Black, 0));
Geometry transformedGeometry = Geometry.Combine(scaledGeometry, scaledGeometry, GeometryCombineMode.Intersect, new TranslateTransform(((TargetSize - rect.Width) / 2) - rect.Left, ((TargetSize - rect.Height) / 2) - rect.Top));
RenderTargetBitmap bmp = new RenderTargetBitmap(TargetSize, TargetSize, // Size
96, 96, // DPI
PixelFormats.Pbgra32);
DrawingVisual viz = new DrawingVisual();
using (DrawingContext dc = viz.RenderOpen())
{
dc.DrawGeometry(Brushes.Black, null, transformedGeometry);
}
bmp.Render(viz);
PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(bmp));
using (FileStream file = new FileStream(fileName, FileMode.Create))
pngEncoder.Save(file);
}
// 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);
I use this:
private void GenerateFontMap(string PathTofont, int GlyphsPerRow, int WidthAndHeight)
{
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 = WidthAndHeight * GlyphsPerRow;
int mapHeight = WidthAndHeight * ((fontNum.Count + 1) / GlyphsPerRow + 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.Black, 1);
Geometry glyphGeometry;
GeometryDrawing glyphDrawing;
PngBitmapEncoder encoder;
RenderTargetBitmap bmp;
DrawingVisual viz;
for (int i = 0; i < fontNum.Count; i++)
{
glyphGeometry = font.GetGlyphOutline(fontNum[i], WidthAndHeight, 1);
glyphDrawing = new GeometryDrawing(System.Windows.Media.Brushes.Black, glyphPen, glyphGeometry);
DrawingImage geometryImage = new DrawingImage(glyphDrawing);
geometryImage.Freeze();
viz = new DrawingVisual();
DrawingContext dc = viz.RenderOpen();
dc.DrawImage(geometryImage, new Rect(0, 0, geometryImage.Width, geometryImage.Height));
dc.Close();
bmp = new RenderTargetBitmap(WidthAndHeight, WidthAndHeight, 96, 96, PixelFormats.Pbgra32);
bmp.Render(viz);
encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
MemoryStream myStream = new MemoryStream();
encoder.Save(myStream);
g.DrawImage(System.Drawing.Bitmap.FromStream(myStream), new PointF((i - (i / GlyphsPerRow) * GlyphsPerRow) * WidthAndHeight, i / GlyphsPerRow * WidthAndHeight));
}
g.Dispose();
b.Save("map.png", ImageFormat.Png);
b.Dispose();
}
}

Categories