save png file in application image folder without

2019-08-01 09:22发布

问题:

I save a png file using savedialogfile. But I want to save it in application IMG folder. My code is as follows:

if (lastSnapshot != null)//writableBitmap object lastSnapshot
        {
            var dlg = new SaveFileDialog();
            dlg.DefaultExt = ".png";
            dlg.Filter = "PNG File|*.png";
            if (dlg.ShowDialog() == true)
            {
                using (var pngStream = GetPngStream(lastSnapshot))//return Stream type 
                using (var file = dlg.OpenFile())
                {
                    byte[] binaryData = new Byte[pngStream.Length];
                    long bytesRead = pngStream.Read(binaryData, 0, (int)pngStream.Length);
                    file.Write(binaryData, 0, (int)pngStream.Length);
                    file.Flush();
                    file.Close();
                }
            }
        }

How to do it? I'll be grateful to anyone who will help me. Thanks in advance.

Adjacent question of mine

回答1:

if (lastSnapshot != null)//writableBitmap object lastSnapshot
{
     using (var pngStream = GetPngStream(lastSnapshot))//return Stream type 
     using (var file = File.Create(Path.Combine("ImageFolder", "ImageName.png")))
     {
         byte[] binaryData = new Byte[pngStream.Length];
         long bytesRead = pngStream.Read(binaryData, 0, (int)pngStream.Length);
         file.Write(binaryData, 0, (int)pngStream.Length);
     }
}


回答2:

Assuming ASP.net...

You need to use HttpServerUtility.MapPath to compute location of the path on your server and potentially adjust permissions on that folder to allow IIS to write there.

var filePath = Server.MapPath("images\\myFile.png");
using (var file = File.Create(filePath))
{
  pngStream.Copy(file);
}