问题
I was just browsing some questions when I stumbled upon this one. I am not experienced in System.Drawing, so I'm just wondering, how would you draw a timestamp onto an image? Also, as I'm very interested in System.Drawing, what are the best resources for learning how to use it well, and its application in various situations? Thank you!
回答1:
It sounds like it would be as simple as using GDI to DrawText() on a Bitmap object. As long as you make sure the z-order is correct (i.e. at the back) it should work. I haven't tried it but that seems like it would work well.
回答2:
I am using the code below to do this. It has a format parameter which you can use to specify how you want the timestamp to look. It also puts a black rectangle behind the timestamp for readability.
protected void Stamp(Bitmap b, DateTime dt, string format)
{
string stampString;
if (!string.IsNullOrEmpty(format)) { stampString = dt.ToString(format); }
else { stampString = dt.ToString(); }
Graphics g = Graphics.FromImage(b);
g.FillRectangle(Brushes.Black, 0, 0, b.Width, 20);
g.DrawString(stampString, new Font("Arial", 12f), Brushes.White, 2, 2);
}
来源:https://stackoverflow.com/questions/1230570/c-drawing-a-timestamp-onto-an-image