简体   繁体   English

如何使用filestream保存位图

[英]How to save a Bitmap using filestream

I have created a bitmap and I want to save it in my upload folder. 我创建了一个bitmap ,我想将它保存在我的上传文件夹中。 How can I do this? 我怎样才能做到这一点?

My Code: 我的代码:

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

The path I will use: 我将使用的路径:

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

The Bitmap class has several overloads of the Save method. Bitmap类有几个Save方法的重载。 One of them takes a file name as parameter which means that you can use 其中一个将文件名作为参数,这意味着您可以使用

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

the question seems to be in conflict with your code (there you load a file from your images and return it) - anyway: there is a "Save" method on your Image-classes so just use this with the same Server.MapPath trick ... just make sure that the ASP.NET account has access/write rights for your target-folder: 这个问题似乎与你的代码冲突(你从你的图像中加载一个文件并将其返回) - 无论如何:你的Image-class上有一个“Save”方法,所以只需使用相同的Server.MapPath技巧。 ..只需确保ASP.NET帐户具有目标文件夹的访问/写入权限:

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

remark: this is using your path but I don't know if this is really the right choice for your problem - as I said: MapPath should be the saver bet. 评论:这是使用你的路径,但我不知道这是否真的是你的问题的正确选择 - 正如我所说:MapPath应该是更安全的赌注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM