我想在.NET 4.0中编写一个小程序,它将打开一个.jpg(或.jpeg)文件,在图像上添加一行文本,然后将图像重新保存为.jpg。有谁知道最简单的方法吗?
感谢您的帮助。
答案 0 :(得分:12)
这样的事情:
var filePath = @"D:\Pictures\Backgrounds\abc.jpg";
Bitmap bitmap = null;
// Create from a stream so we don't keep a lock on the file.
using (var stream = File.OpenRead(filePath))
{
bitmap = (Bitmap)Bitmap.FromStream(stream);
}
using (bitmap)
using (var graphics = Graphics.FromImage(bitmap))
using (var font = new Font("Arial", 20, FontStyle.Regular))
{
// Do what you want using the Graphics object here.
graphics.DrawString("Hello World!", font, Brushes.Red, 0, 0);
// Important part!
bitmap.Save(filePath);
}
答案 1 :(得分:3)
var myBitmap = new Bitmap("C:\\myImage.jpg");
var g = Graphics.FromImage(myBitmap);
g.DrawString("My\nText", new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));