save png file in application image folder without using savedialogfile

久未见 提交于 2019-12-11 12:09:34

问题


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


来源:https://stackoverflow.com/questions/13599599/save-png-file-in-application-image-folder-without-using-savedialogfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!