问题
Okei, I have a C# project with a resource file. The resource file contains a image (.png). I want the png file to be saved/extracted to a specified folder on my computer. How do I do this?
回答1:
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);
}
}
回答2:
See this Page It could help : MSDN Save Image
回答3:
Have you tried:
Extracting-Embedded-Images-From-An-Assembly or
how-can-i-extract-a-file-from-an-embedded-resource-and-save-it-to-disk
This?
来源:https://stackoverflow.com/questions/17936679/save-image-from-resource-file-to-computer-c-sharp