Vince MacKenzie
Vince MacKenzie

Reputation: 27

How to edit image in C#

Heey. I wanna write a program for a report system. I wanna Show up the result in a .jpg image. I have many variable for name, age, sex etc etc etc.

Here comes the question..

Why not do in Word?

Ya. This result should be in .jpg not .docx for because the image will be post on websites.

How should "put" variables onto image?

Heres a example for what i want: http://prntscr.com/s8bgze

Upvotes: 0

Views: 3793

Answers (1)

Alexander Petrov
Alexander Petrov

Reputation: 14231

You can do it like this:

// load your photo
var photo = new Bitmap("photo.jpg");
// create an image of the desired size
var bitmap = new Bitmap(200, 300);

using (var graphics = Graphics.FromImage(bitmap))
{
    // specify the desired quality of the render and text, if you wish
    //graphics.CompositingQuality = CompositingQuality.HighQuality;
    //graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

    // set background color
    graphics.Clear(Color.White);
    // place photo on image in desired location
    graphics.DrawImageUnscaled(photo, 0, 0);

    using (var font = new Font("Arial", 12))
    {
        // draw some text on image
        graphics.DrawString("Name: X Y", font, Brushes.Black, 0, 200);
        graphics.DrawString("Age: 19", font, Brushes.Black, 0, 230);
        // etc
    }
}

// save image to file or stream
bitmap.Save("edited.phg", ImageFormat.Png);

Instead of Graphics.DrawString method you can use TextRenderer.DrawText (they have small differences in the drawing of the text).

Also, do not use jpg format for images containing text. Instead, take png.

Upvotes: 2

Related Questions