在 .Net 中用笔划将文本绘制到图像上

发布于 2024-08-17 13:37:39 字数 1440 浏览 4 评论 0 原文

我目前正在使用 .Net 中的 System.Drawing.Graphics.DrawString() 方法在图像顶部绘制文本,然后将其保存到新文件中:

// define image file paths
string sourceImagePath = HttpContext.Current.Server.MapPath("~/img/sourceImage.jpg");
string destinationImagePath = HttpContext.Current.Server.MapPath("~/img/") + "finalImage.jpg";

// create new graphic to draw to
Bitmap bm = new Bitmap(200, 200);
Graphics gr = Graphics.FromImage(bm);

// open and draw image into new graphic
System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(sourceImagePath, true);
gr.DrawImage(sourceImage, 0, 0);
sourceImage.Dispose();

// write "my text" on center of image
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;

PrivateFontCollection fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(HttpContext.Current.Server.MapPath("~/fonts/HelveticaNeueLTStd-BlkCn.ttf"));
// prototype (1)
gr.DrawString("my text", new Font(fontCollection.Families[0], 14, FontStyle.Bold), new SolidBrush(Color.FromArgb(255, 255, 255)), 100, 0, sf);
fontCollection.Dispose();

// save new image
if (File.Exists(destinationImagePath))
{
    File.Delete(destinationImagePath);
}
bm.Save(destinationImagePath);
bm.Dispose();
gr.Dispose();

这种在图像上添加文本的方法没有提供一种方法(据我所知)向文本添加笔画。例如,我真正需要做的是向图像上写入的文本添加某种颜色的 2px 描边。

如何使用 .Net Framework <= v3.5 来完成此操作?

I am currently using the System.Drawing.Graphics.DrawString() method in .Net to draw text on top of an image, and then save it to a new file:

// define image file paths
string sourceImagePath = HttpContext.Current.Server.MapPath("~/img/sourceImage.jpg");
string destinationImagePath = HttpContext.Current.Server.MapPath("~/img/") + "finalImage.jpg";

// create new graphic to draw to
Bitmap bm = new Bitmap(200, 200);
Graphics gr = Graphics.FromImage(bm);

// open and draw image into new graphic
System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(sourceImagePath, true);
gr.DrawImage(sourceImage, 0, 0);
sourceImage.Dispose();

// write "my text" on center of image
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;

PrivateFontCollection fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(HttpContext.Current.Server.MapPath("~/fonts/HelveticaNeueLTStd-BlkCn.ttf"));
// prototype (1)
gr.DrawString("my text", new Font(fontCollection.Families[0], 14, FontStyle.Bold), new SolidBrush(Color.FromArgb(255, 255, 255)), 100, 0, sf);
fontCollection.Dispose();

// save new image
if (File.Exists(destinationImagePath))
{
    File.Delete(destinationImagePath);
}
bm.Save(destinationImagePath);
bm.Dispose();
gr.Dispose();

This method of adding text on an image does not provide a way (that I know of) to add a stroke to the text. For example, what I really need to do is add a 2px stroke of a certain color to the text that is written on the image.

How can this be done with the .Net Framework <= v3.5?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲影 2024-08-24 13:37:39

所有文本均具有笔画

如果您所说的描边指的是轮廓,则创建一个 GraphicsPath 并使用 AddString,然后您可以使用 DrawPath 而不是 msdn 上的示例中使用的 FillPath 来勾画路径。

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // Create a GraphicsPath object.
        GraphicsPath myPath = new GraphicsPath();

        // Set up all the string parameters.
        string stringText = "Sample Text";
        FontFamily family = new FontFamily("Arial");
        int fontStyle = (int)FontStyle.Italic;
        int emSize = 96;
        Point origin = new Point(20, 20);
        StringFormat format = StringFormat.GenericDefault;

        // Add the string to the path.
        myPath.AddString(stringText,
            family,
            fontStyle,
            emSize,
            origin,
            format);

        //Draw the path to the screen.
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        e.Graphics.FillPath(Brushes.BlanchedAlmond, myPath);
        e.Graphics.DrawPath(new Pen(Brushes.Azure, 2), myPath);
    }

如果您所说的笔画是指罢工,则使用删除线 字体样式。

如果笔划是指 serif,则使用不同的字体。

All text has strokes.

If by stroke you mean outline, then you create a GraphicsPath and add the string using AddString, you can then outline the path using DrawPath rather than FillPath as used in the example on msdn.

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // Create a GraphicsPath object.
        GraphicsPath myPath = new GraphicsPath();

        // Set up all the string parameters.
        string stringText = "Sample Text";
        FontFamily family = new FontFamily("Arial");
        int fontStyle = (int)FontStyle.Italic;
        int emSize = 96;
        Point origin = new Point(20, 20);
        StringFormat format = StringFormat.GenericDefault;

        // Add the string to the path.
        myPath.AddString(stringText,
            family,
            fontStyle,
            emSize,
            origin,
            format);

        //Draw the path to the screen.
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        e.Graphics.FillPath(Brushes.BlanchedAlmond, myPath);
        e.Graphics.DrawPath(new Pen(Brushes.Azure, 2), myPath);
    }

If by stroke you mean strike, then use the Strikeout font style.

If by stroke you mean serif, then use a different font.

温柔一刀 2024-08-24 13:37:39

有点黑客,但您可以将两组文本一组放在另一组之上,下层稍微放大。可能不适用于所有字体,但值得一试。

A bit of a hack, but you could layer two sets of text one atop the other, with the lower layer scaled up just slightly. Might not work perfectly for all fonts but it's worth a try.

又爬满兰若 2024-08-24 13:37:39

不确定这是否正是您正在寻找的,但请尝试使用 LinearGradientBrush 而不是 SolidBrush

我不确定这个问题中的“描边”是什么意思,但是如果您希望第 7 行和第 8 行上的所有像素均为红色,则可以使用 System.Drawing.TextureBrush而不是 SolidBrush。您将创建一个 Bitmap ,其宽度和高度足以容纳您的字体(除了第 7 行和第 8 行为红色之外,全部为黑色),然后使用 TextureBrush 的构造函数将此位图作为参数。

Not sure if this is exactly what you're looking for, but try using a LinearGradientBrush instead of a SolidBrush in your code.

I'm not sure what you mean by "stroke" in this question, but if you want, say, all pixels on rows 7 and 8 to be red, you can use a System.Drawing.TextureBrush instead of a SolidBrush. You would create a Bitmap one pixel wide and tall enough for your font (all black except for rows 7 and 8 which would be red), and then use the constructor for TextureBrush that takes this Bitmap as a parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文