Solved

Put text in a picture ASP.NET C#

Posted on 2016-10-07
2
56 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

770 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