Solved

Put text in a picture ASP.NET C#

Posted on 2016-10-07
2
67 Views
Last Modified: 2016-10-08
We have a file called BLUE.PNG and is located in c:\pictures
This is that file example:

BLUE.PNG
Now in ASP.NET C# we want to process information and some variables and after that put some text over the picture and save like BLUEMOD.PNG in same c:\pictures directory. For example the next picture is text over the first picture:

BLUETEXT
That text will be generated via string variables or whatever. How can we do then to put text over a picture in ASP.NET C# and save the results in other file?

Any ideas?
0
Comment
Question by:Alex E.
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 41833899
You need to bring in the Drawing library for this purpose. You can make use of a Graphics object to draw on the image.

e.g.

static byte[] DrawOnImage(byte[] imageData, string text)
{
    using (MemoryStream ms = new MemoryStream(imageData))
    {
        using (Image img = Image.FromStream(ms))
        {
            using (Graphics g = Graphics.FromImage(img))
            {
                g.DrawString(text, new Font("Arial", 12F), Brushes.Black, new PointF(50, 40));
            }

            ms.Position = 0;    // Important!

            img.Save(ms, ImageFormat.Png);
        }

        return ms.ToArray();
    }
}

Open in new window


Usage
byte[] imageBytes = File.ReadAllBytes("blue.png");
byte[] modifiedBytes = DrawOnImage(imageBytes, "Example of text");

File.WriteAllBytes("blue2.png", modifiedBytes);

Open in new window

0
 

Author Closing Comment

by:Alex E.
ID: 41835066
Thank you
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question