Save image from resource file to computer - c# [closed]

杀马特。学长 韩版系。学妹 提交于 2019-12-08 03:15:13

问题


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

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