C#: Drawing a timestamp onto an image

这一生的挚爱 提交于 2021-01-29 03:15:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!