C#像Javascript一样使用位图生成画布图像

C#像Javascript一样使用位图生成画布图像,javascript,c#,canvas,Javascript,C#,Canvas,我试图使一个图像,就像我做的javascript与位图,但我有一些问题。首先,字体看起来比javascript厚,而且定位不好 Javascript看起来像: var el = document.createElement("canvas"); el.width = 32, el.height = 32; var cnt = el.getContext("2d"); cnt.font = "12pt Arial"; cnt.fillText("Hey", 2, 24); el.toDataURL

我试图使一个图像,就像我做的javascript与位图,但我有一些问题。首先,字体看起来比javascript厚,而且定位不好

Javascript看起来像:

var el = document.createElement("canvas");
el.width = 32, el.height = 32;
var cnt = el.getContext("2d");
cnt.font = "12pt Arial";
cnt.fillText("Hey", 2, 24);
el.toDataURL();//Data image so u can see the javascript image
Bitmap objBmpImage = new Bitmap(32, 32);
Font objFont = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point);
Graphics objGraphics = Graphics.FromImage(objBmpImage);

objGraphics.Clear(Color.Transparent);

Brush textBrush = new SolidBrush(Color.Black);
objGraphics.DrawString("Hey", objFont, textBrush, 1, 12);//This doesnt position it right I think
objBmpImage.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);//This image is thicker font
我制作的C#看起来像:

var el = document.createElement("canvas");
el.width = 32, el.height = 32;
var cnt = el.getContext("2d");
cnt.font = "12pt Arial";
cnt.fillText("Hey", 2, 24);
el.toDataURL();//Data image so u can see the javascript image
Bitmap objBmpImage = new Bitmap(32, 32);
Font objFont = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point);
Graphics objGraphics = Graphics.FromImage(objBmpImage);

objGraphics.Clear(Color.Transparent);

Brush textBrush = new SolidBrush(Color.Black);
objGraphics.DrawString("Hey", objFont, textBrush, 1, 12);//This doesnt position it right I think
objBmpImage.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);//This image is thicker font
所以我遇到的问题是

1) 文本位置不正确

2) 图像字体看起来比javascript字体厚


3) 还希望具有相同的数据:图像url与c#

objGraphics.textrendinghint=textrendinghint.AntiAliasGridFit中的javascript相同。玩
objGraphics.TextContrast
并查看课程。它被用来定义文本的正确位置。有一个重载
DrawString()
,它接受StringFormat参数。如果您不知道要设置什么,请只指定
StringFormat.genericyprographic
(这是一个预设值)。保存完字体、图形对象、画笔、位图对象后,您必须将其处理掉。@Jimi我知道如何处理,我将查看这些选项