Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 您可以在.NET中打开JPEG、添加文本并重新保存为JPEG吗?_C#_.net_Image_Image Manipulation - Fatal编程技术网

C# 您可以在.NET中打开JPEG、添加文本并重新保存为JPEG吗?

C# 您可以在.NET中打开JPEG、添加文本并重新保存为JPEG吗?,c#,.net,image,image-manipulation,C#,.net,Image,Image Manipulation,我想在.NET4.0中编写一个小程序,它将打开一个.jpg(或.jpeg)文件,在图像中添加一行文本,然后将图像重新保存为.jpg。有人知道最简单的方法吗 谢谢您的帮助。类似这样的内容: 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 = F

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

谢谢您的帮助。

类似这样的内容:

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

请记住,重新保存JPEG(尤其是您修改过的JPEG)会再次压缩它,每次压缩都会抛出一些细节。执行此操作时,如果必须,请尽可能少地执行。@cHao-另一个选项是动态生成/输出图像,同时保持原始图像不带水印。您可以不保存而执行此操作吗?@DB是的,您可以使用重载写入流(例如
MemoryStream
)。
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));