将文本添加到图像文件 [英] Adding text to an image file

查看:117
本文介绍了将文本添加到图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文本添加到图像文件。我需要读取一个图像文件(JPG,PNG,GIF),我需要一行文本添加到它。

I need to add text to an image file. I need to read one image file (jpg,png,gif) and I need add one line text to it.

推荐答案

好在GDI +中,你会读使用Image类的文件中,然后使用Graphics类文本添加到它。是这样的:

Well in GDI+ you would read in the file using a Image class and then use the Graphics class to add text to it. Something like:

  Image image = Image.FromFile(@"c:\somepic.gif"); //or .jpg, etc...
  Graphics graphics = Graphics.FromImage(image);
  graphics.DrawString("Hello", this.Font, Brushes.Black, 0, 0);

如果您想保存文件覆盖旧的,代码更改位作为直到它的处置Image.FromFile()方法锁定文件。以下是我想出了:

If you want to save the file over the old one, the code has to change a bit as the Image.FromFile() method locks the file until it's disposed. The following is what I came up with:

  FileStream fs = new FileStream(@"c:\somepic.gif", FileMode.Open, FileAccess.Read);
  Image image = Image.FromStream(fs);
  fs.Close();

  Bitmap b = new Bitmap(image);
  Graphics graphics = Graphics.FromImage(b);
  graphics.DrawString("Hello", this.Font, Brushes.Black, 0, 0);

  b.Save(@"c:\somepic.gif", image.RawFormat);

  image.Dispose();
  b.Dispose();



我会很彻底,虽然测试:)

I would test this quite thoroughly though :)

这篇关于将文本添加到图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆