如何使用文件流保存位图

3

我创建了一个位图,希望将其保存在我的上传文件夹中。我该怎么做?

我的代码:

public FileResult image(string image)
{
    var bitMapImage = new Bitmap(Server.MapPath("/Content/themes/base/images/photo.png"));
    Graphics graphicImage = Graphics.FromImage(bitMapImage);
    graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
    graphicImage.DrawString(image, new Font("Trebuchet MS,Trebuchet,Arial,sans-serif", 10, FontStyle.Bold), SystemBrushes.WindowFrame, new Point(15, 5));
    graphicImage.DrawArc(new Pen(Color.Red, 3), 90, 235, 150, 50, 0, 360);
    MemoryStream str = new MemoryStream();
    bitMapImage.Save(str, ImageFormat.Png);
    return File(str.ToArray(), "image/png");
}

我将使用的路径是:


string mynewpath = Request.PhysicalApplicationPath + "Upload\\";

我想将代码中创建的图像保存在上传文件夹中。 - user1680185
1
这不是一个问题,这是一个要求。你有错误吗?它能编译吗?等等... 都是问题。 - Steve B
2个回答

3

Bitmap类有几个重载的Save方法。其中一个方法带有文件名参数,这意味着您可以使用它来将位图保存在磁盘上。

bitMapImage.Save(mynewpath + "somefilename.png", ImageFormat.Png);

1

这个问题似乎与您的代码冲突(您从图像中加载文件并返回它),但无论如何:您的Image类上有一个“Save”方法,所以只需使用相同的Server.MapPath技巧即可...只需确保ASP.NET帐户对您的目标文件夹具有访问/写入权限:

string mynewpath = Request.PhysicalApplicationPath + "Upload\\myFile.png";
bitMapImage.Save(mynewpath);

备注:这是使用您的路径,但我不确定这是否真的是解决您问题的正确选择 - 正如我所说:MapPath 应该是更保险的选择。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接