你可以在.NET中打开JPEG,添加文本并重新保存为JPEG吗?

Sak*_*o73 5 .net c# image-manipulation image

我想在.NET 4.0中编写一个小程序,它将打开一个.jpg(或.jpeg)文件,在图像上添加一行文本,然后将图像重新保存为.jpg.有谁知道最简单的方法吗?

谢谢你的帮助.

The*_*Sky 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);
}
Run Code Online (Sandbox Code Playgroud)