将图像从资源文件保存到计算机 – c#

Okei,我有一个带有资源文件的C#项目。 资源文件包含图像(.png)。 我希望将png文件保存/解压缩到我的计算机上的指定文件夹中。 我该怎么做呢?

static void ExtractFileResource(string resource_name, string file_name) { try { if (File.Exists(file_name)) File.Delete(file_name); if (!Directory.Exists(Path.GetDirectoryName(file_name))) Directory.CreateDirectory(Path.GetDirectoryName(file_name)); using (Stream sfile = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource_name)) { byte[] buf = new byte[sfile.Length]; sfile.Read(buf, 0, Convert.ToInt32(sfile.Length)); using (FileStream fs = File.Create(file_name)) { fs.Write(buf, 0, Convert.ToInt32(sfile.Length)); fs.Close(); } } } catch (Exception ex) { throw new Exception(string.Format("Can't extract resource '{0}' to file '{1}': {2}", resource_name, file_name, ex.Message), ex); } } 

看到这个页面它可以帮助: MSDN保存图像

你有没有尝试过:

从组件或组件中提取嵌入式图像

如何灿我提取物-一个文件从-一个嵌入资源并节省,它到磁盘

这个?