user1680185
user1680185

Reputation: 33

How to save a Bitmap using filestream

I have created a bitmap and I want to save it in my upload folder. 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\\";

Upvotes: 3

Views: 8574

Answers (2)

Jakob Christensen
Jakob Christensen

Reputation: 14956

The Bitmap class has several overloads of the Save method. One of them takes a file name as parameter which means that you can use

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

Upvotes: 3

Random Dev
Random Dev

Reputation: 52280

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:

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.

Upvotes: 1

Related Questions