【问题标题】:How to save a Bitmap using filestream如何使用文件流保存位图
【发布时间】:2012-09-18 12:03:49
【问题描述】:

我创建了一个bitmap,我想将它保存在我的upload 文件夹中。我怎样才能做到这一点?

我的代码:

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\\";

【问题讨论】:

  • 我想将我在代码中创建的图像保存在上传文件夹中
  • 这不是问题,这是要求。你有错误吗?它编译吗?等等...个问题。

标签: c# asp.net-mvc asp.net-mvc-3 c#-4.0


【解决方案1】:

Bitmap 类有几个 Save 方法的重载。其中一个以文件名作为参数,这意味着您可以使用

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

【讨论】:

    【解决方案2】:

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

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

    备注:这是使用您的路径,但我不知道这是否真的是您的问题的正确选择 - 正如我所说:MapPath 应该是节省的赌注。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 2012-12-04
      • 1970-01-01
      相关资源
      最近更新 更多