c# - 将文字添加到图像并保存

标签 c# image-processing text save digital

在我的程序中,我允许用户输入一些文本,然后使用 graphics.DrawString() 方法将这些文本放在图像的顶部。当我然后去保存这张图片时,它保存时没有文本。

如何将两者保存为一张图片?

我看过几个例子,但没有一个有帮助。

private void txtToolStripMenuItem_Click(object sender, System.EventArgs e)
    {
        Rectangle r = new Rectangle(535, 50, original_image.Width, original_image.Height);
        Image img = Image.FromFile("C:\\PCB.bmp");

        Bitmap image = new Bitmap(img);

        StringFormat strFormat = new StringFormat();

        strFormat.Alignment = StringAlignment.Center;
        strFormat.LineAlignment = StringAlignment.Center;

        Graphics g = Graphics.FromImage(image);

        g.DrawString("Hellooooo", new Font("Tahoma", 40), Brushes.White,
                r, strFormat); 

        image.Save("file_PCB.Bmp", ImageFormat.Bmp);
    }

最佳答案

那是因为您正在创建一个没有 Canvas 的图形对象。您没有绘制任何内容,因此绘制文本不会改变任何内容。

首先创建图像的副本(或创建空白位图并在其上绘制图像),然后创建用于在该图像上绘制的图形对象:

Graphics g = Graphics.FromImage(image_save);

然后绘制文字并保存图像。

关于c# - 将文字添加到图像并保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13892308/

相关文章:

python - 具有分水岭的连通对象的图像分割

image - 仅比较纯图像数据和图像相似度的重复照片搜索?

c# - MySQL BLOB 图像数据逐渐丢失?

c - 奇怪的字符保存在我的文本文件中

C# 与 VB.Net - 隐式转换

C# Zedgraph 改变短划线长度

c# - 按名称访问动态对象属性

c# - 为什么这个方法重载异常?

css - 如何在它们之间添加一个空格并使文本保持在同一行

C 编程 — 获取字符串中的多个单词