save png file in application image folder without

2019-08-01 09:45发布

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

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-01 10:03
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);
     }
}
查看更多
再贱就再见
3楼-- · 2019-08-01 10:22

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);
}
查看更多
登录 后发表回答