C# 将文本添加到图像文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/709414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding text to an image file
提问by RV.
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.
我需要将文本添加到图像文件中。我需要读取一个图像文件(jpg、png、gif),并且需要向其中添加一行文本。
回答by nba bogdan
You can do this by using the Graphics object in C#. You can get a Graphics object from the picture ( image.CreateGraphics() - or something like this as I remember ) and the use some of the built in methods for adding text to it like : Graphycs.DrawString() or other related methods.
您可以通过使用 C# 中的 Graphics 对象来完成此操作。您可以从图片中获取 Graphics 对象( image.CreateGraphics() - 或者我记得的类似的东西),并使用一些内置方法向其中添加文本,例如:Graphycs.DrawString() 或其他相关方法。
回答by Mladen Mihajlovic
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:
那么在 GDI+ 中,您将使用 Image 类读入文件,然后使用 Graphics 类向其中添加文本。就像是:
Image image = Image.FromFile(@"c:\somepic.gif"); //or .jpg, etc...
Graphics graphics = Graphics.FromImage(image);
graphics.DrawString("Hello", this.Font, Brushes.Black, 0, 0);
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:
如果您想将文件保存在旧文件上,则代码必须稍作更改,因为 Image.FromFile() 方法会锁定文件直到它被释放。以下是我想出的:
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 :)
不过我会非常彻底地测试一下:)
回答by OmG
Specifically for gifs to have a gif result, you should write on each frame like the following:
特别是要让 gif 具有 gif 结果,您应该在每一帧上写下如下内容:
string originalImgPath = @"C:\test.gif";
Image IMG = Image.FromFile(originalImgPath);
FrameDimension dimension = new FrameDimension(IMG.FrameDimensionsList[0]);
int frameCount = IMG.GetFrameCount(dimension);
int Length = frameCount;
GifBitmapEncoder gEnc = new GifBitmapEncoder();
for (int i = 0; i < Length; i++)
{
// Get each frame
IMG.SelectActiveFrame(dimension, i);
var aFrame = new Bitmap(IMG);
// write one the selected frame
Graphics graphics = Graphics.FromImage(aFrame);
graphics.DrawString("Hello", new Font("Arial", 24, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 50, 50);
var bmp = aFrame.GetHbitmap();
var src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
// merge frames
gEnc.Frames.Add(BitmapFrame.Create(src));
}
string saveImgFile = @"C:\modified_test.gif"
using (FileStream fs2 = new FileStream(saveImgFile, FileMode.Create))
{
gEnc.Save(fs2);
}
I should have mentioned that getting gif frames from this post.
我应该提到从这篇文章中获取 gif 帧。