使用GDI+使用 C# 为网站创建图像

发布于 2024-10-08 11:27:45 字数 142 浏览 12 评论 0原文

我想使用 GDI 在服务器上生成带有我的字体的文本图像,以便在我的网站上提供服务。

我想在我的网站上使用任何字体,但当然不能在网站上使用任何字体......所以我想我需要自己生成文本的图像。

想法如何实现这一目标?

谢谢。

I'd like to use GDI to generate text images with my fonts on a server to be served on my site.

I want to use any font on my sites, but of course couldn't use any font on a web site... so I guess I'd need to generate images of the text myself.

Ideas how to accomplish this?

Thanks.

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

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

发布评论

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

评论(2

回忆那么伤 2024-10-15 11:27:45
private void button1_Click( object sender, EventArgs e )
{
    using( Font f = new Font( "Times New Roman", 22f ) )
    {
        pictureBox1.Image = CreateImage( "TEXT", pictureBox1.Size, f, Color.Black );
    }
}

Bitmap CreateImage( string text, Size imageSize, Font font, Color fontColor )
{
    Bitmap image = new Bitmap( imageSize.Width, imageSize.Height );
    using( Graphics g = Graphics.FromImage( image ) )
    using( Brush brush = new SolidBrush( fontColor ) )
    {
        g.DrawString( text, font, brush, new PointF( 0, 0 ) );
    }

    return image;
}

这将简单地创建一个带有一定大小的文本的图像并将其分配给图片框。您当然需要添加用于设置对齐和类似性质的功能,但这是基本思想。创建一个图像,从中获取一个 Graphics 对象,然后绘制一个字符串。

您还可以使用 Graphics.MeasureString 方法获取使用某种字体“f”绘制的字符串的宽度和高度。

private void button1_Click( object sender, EventArgs e )
{
    using( Font f = new Font( "Times New Roman", 22f ) )
    {
        pictureBox1.Image = CreateImage( "TEXT", pictureBox1.Size, f, Color.Black );
    }
}

Bitmap CreateImage( string text, Size imageSize, Font font, Color fontColor )
{
    Bitmap image = new Bitmap( imageSize.Width, imageSize.Height );
    using( Graphics g = Graphics.FromImage( image ) )
    using( Brush brush = new SolidBrush( fontColor ) )
    {
        g.DrawString( text, font, brush, new PointF( 0, 0 ) );
    }

    return image;
}

That will simply create an image with some text of a certain size and assign it to a picturebox. You would of course need to add functionality for setting alignment and things of that nature, but this is the basic idea. Create an image, get a Graphics object from it, and draw a string.

You can also obtain the width and height of a drawn string using some font 'f' by using the Graphics.MeasureString method.

放血 2024-10-15 11:27:45

为了清晰起见,采用硬编码的代码示例:

Bitmap b = new Bitmap(200, 100);
using (Graphics g = Graphics.FromImage(b)) {
    g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias;
    g.DrawString("Hello World!", new Font("Tahoma", 12), Brushes.DarkBlue, 0, 0);
    b.Save("c:\\MyPic.bmp");
}

图片大小 (200x100)、文本、字体、起始位置 (0,0) 和目标路径都是硬编码的。

Code sample with hard-coding for clarity:

Bitmap b = new Bitmap(200, 100);
using (Graphics g = Graphics.FromImage(b)) {
    g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias;
    g.DrawString("Hello World!", new Font("Tahoma", 12), Brushes.DarkBlue, 0, 0);
    b.Save("c:\\MyPic.bmp");
}

The picture size (200x100), text, font and starting location (0,0) and destination path are all hard-coded.

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