不使用savedialogfile将png文件保存在应用程序映像文件夹中

时间:2012-11-28 07:05:11

标签: c# asp.net silverlight-4.0

我使用savedialogfile保存了一个png文件。但我想将它保存在应用程序IMG文件夹中。我的代码如下:

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

怎么做?我会感激任何能帮助我的人。提前谢谢。

Adjacent question of mine

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

假设ASP.net ......

您需要使用HttpServerUtility.MapPath来计算服务器上路径的位置,并可能调整该文件夹的权限以允许IIS在那里写入。

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