在C#中的位图图像上写文本

时间:2013-03-04 23:22:03

标签: c# .net image

好吧,我尝试用C#写一个图像,我的代码是:

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;
        }
    }

这种方法不起作用,因为没有写在图像上,请帮帮我。

PD:我很抱歉我的英语,但我是拉丁裔jeje,谢谢。

1 个答案:

答案 0 :(得分:4)

看起来你正在绘制错误的位图

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

应该是

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