将文本添加到图像并保存

Adding text to image and save(将文本添加到图像并保存)
本文介绍了将文本添加到图像并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我允许用户输入一些文本,然后使用 graphics.DrawString() 方法将这些文本放在图像之上.当我然后去保存这个图像时,它会保存它而不保存文本.

In my program i allow the user to enter some text which then gets put on top of an image using the graphics.DrawString() method. When i then go to save this image, it saves it without the text.

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

How can i save both as one image?

我看过一些例子,但都没有帮助.

I have seen a few examples but none of which have helped.

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);
    }

推荐答案

那是因为您正在创建一个没有画布的图形对象.你什么都没有画,所以你画的文字不会改变任何东西.

That's because you are creating a graphics object without a canvas. You are drawing on nothing, so there is nothing that is changed by your drawing the text.

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

First create a copy of the image (or create a blank bitmap and draw the image on it), then create a graphics object for drawing on that image:

Graphics g = Graphics.FromImage(image_save);

然后绘制文本并保存图像.

Then draw the text and save the image.

这篇关于将文本添加到图像并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)