Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write text on a bitmap image in C#

Tags:

c#

.net

image

Well, I try to write on an image in C#, my code is:

public string WriteOnImage(Bitmap Image, string NameImage, string TextFileName)

    {
        string Message = "OK";
        try
        {
            Bitmap bitMapImage = new Bitmap(Image);

            using (Graphics graphImage = Graphics.FromImage(Image))

            {

                graphImage.SmoothingMode = SmoothingMode.AntiAlias;

                string line;

                // Read the file and display it line by line.
                StreamReader file = new StreamReader(Resources.C_PATH_DESTINO_IMG + TextFileName);
                while ((line = file.ReadLine()) != null)
                {
                    graphImage.DrawString(line, new Font("Courier New", 15, FontStyle.Bold), SystemBrushes.WindowText, new Point(0, 0));
                    HttpContext.Current.Response.ContentType = "image/jpeg";
                    bitMapImage.Save(Resources.C_PATH_DESTINO_IMG + NameImage, ImageFormat.Jpeg);
                    graphImage.Dispose();
                    bitMapImage.Dispose();
                }

                file.Close();
            }
            return Message;
        }
        catch (Exception ex)
        {
            EventLogWrite("Error: " + ex.Message);
            return Message = ex.Message;
        }
    }

this method doesn't work because doesn't write on the image, please help me.

PD: I'm sorry for my english but I'm Latino jeje, thanks.

like image 731
Elmer A. Chacon Avatar asked Jan 19 '26 11:01

Elmer A. Chacon


1 Answers

It looks like you are drawing on the wrong bitmap

 Bitmap bitMapImage = new Bitmap(Image);
 using (Graphics graphImage = Graphics.FromImage(Image))

should be

 Bitmap bitMapImage = new Bitmap(Image);
 using (Graphics graphImage = Graphics.FromImage(bitMapImage))
like image 178
sa_ddam213 Avatar answered Jan 21 '26 00:01

sa_ddam213



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!