在asp mvc中,我向图像添加文本并返回MemoryStream,下载文件显示未知文件格式

时间:2019-03-22 03:58:08

标签: asp.net-mvc-4

在asp mvc中,我将文本添加到图像中,然后返回MemoryStream, 但是当我打开下载的图像时,文件显示未知文件格式,图像大小仅为1.33kb。

如果我使用Image.Save(filePath),它将很好地工作。

public ActionResult ReturnImage()
{
    Image img = Image.FromFile("image.jpg");
    string firstText = "Hello";
    string secondText = "World";

    PointF firstLocation = new PointF(10f, 10f);
    PointF secondLocation = new PointF(10f, 50f);

    using (MemoryStream ms = new MemoryStream())
    using (Graphics graphics = Graphics.FromImage(img))
    using (Font arialFont = new Font("Arial", 10))
    {
        graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
        graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
        img.Save(ms, ImageFormat.Jpeg);
        //img.Save(@"C:\Users\Administrator\Desktop\007.jpg");
        ms.Position = 0;
        return File(ms, "APPLICATION/OCTET-STREAM");
    }
}
[enter image description here][1]

0 个答案:

没有答案