Mohsin Mushtaq
Mohsin Mushtaq

Reputation: 133

Display Database value on an Image

I would like to know if there's a way to display database value on an image, like I have a image template of a certificate and i would like to display name from the database, cause I have like 1000 names and there Image have to be made, is it possible from me to make a c# app for that. Im sorry new to this I did searches but i can't find anything.

Thanks

Upvotes: 0

Views: 95

Answers (1)

Scott Yang
Scott Yang

Reputation: 567

You can do it by the GDI+ drawing in .NET. first, load your template image, then draw a string to it, then save it as a new image. load all the names from database and process them in a loop, that will be ok. what you must focus on is to find the proper drawing position and beautify the font, my sample is below:

private void btnDrawImage_Click(object sender, EventArgs e)
    {
        string templateFile = Application.StartupPath + Path.DirectorySeparatorChar + "template_picture.jpg";
        //customerize your dispaly string style
        Font ft = new System.Drawing.Font("SimSun", 24);
        Brush brush = Brushes.Red;
        //start position(x,y)
        Point startPt = new Point(100, 100);
        //names from database
        var nameList = new List<string>(){
            "scott yang",
            "Vivi",
            "maxleaf",
            "lenka"};
        //process image on every name
        foreach (string name in nameList)
        {
            string msg = "Welcome " + name;
            Image templateImage = Image.FromFile(templateFile);
            Graphics g = Graphics.FromImage(templateImage);
            g.DrawString(msg, ft, brush, startPt.X, startPt.Y);
            g.Dispose();
            string savePath = "c:\\" + name + ".jpg";
            templateImage.Save(savePath);
        }
    }

here is my result, I hope it is helpful to you. result of my test result of my test

Upvotes: 2

Related Questions