wpf 如何将图像从资源复制到磁盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24970319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:14:15  来源:igfitidea点击:

How to copy image from resources to disk

c#wpfwinforms

提问by GarryMoveOut

I added image to my solution. Changed compilation action property to resource and don't copy. Application in first run check is directories and files are existing if not they are created. My application needs default image of object. So thats why I added image to solution. Now how I can copy it to specific disk location.
I written this from samples which I founded.

我在我的解决方案中添加了图像。将编译操作属性更改为资源且不复制。应用程序在第一次运行检查中是否存在目录和文件,如果没有创建它们。我的应用程序需要对象的默认图像。所以这就是我在解决方案中添加图像的原因。现在我如何将它复制到特定的磁盘位置。
我是根据我创建的样本写的。

if (!File.Exists(path_exe + "\images\drinks\defaultIMG.jpg"))
{
    using (var resource =  Assembly.GetExecutingAssembly().GetManifestResourceStream("Data\defaultIMG.jpg"))
    {
         using (var file = new FileStream(path_exe + "\images\drinks\defaultIMG.jpg", FileMode.Create, FileAccess.Write))
         {
               resource.CopyTo(file);
         }
}

}

}

But this creates only empty file.

但这只会创建空文件。

回答by Microlang

I Think the easiest way is:

我认为最简单的方法是:

 Properties.Resources.defaultIMG.Save(path_exe + "\images\drinks\defaultIMG.jpg");

Where defaultIMG is your Image resource name.

其中 defaultIMG 是您的图像资源名称。

回答by GarryMoveOut

I found sample of code on http://www.c-sharpcorner.com/uploadfile/40e97e/saving-an-embedded-file-in-C-Sharp/It works good

我在http://www.c-sharpcorner.com/uploadfile/40e97e/saving-an-embedded-file-in-C-Sharp/上找到了代码示例,效果很好

Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Zeszycik.Data.defaultIMG.jpg");
FileStream fileStream = new FileStream("new.jpg", FileMode.CreateNew);
for (int i = 0; i < stream.Length; i++)
     fileStream.WriteByte((byte)stream.ReadByte());
fileStream.Close();

回答by raj

Is your GetManifestResourceStream () returning null? GetManifestResourceStream always accepts the resource name in the following format:

您的 GetManifestResourceStream () 是否返回 null?GetManifestResourceStream 始终接受以下格式的资源名称:

<namespace>.<path in project>.<filename>

In your case, if the namespace is Company.Application and the resource file path is "Resources/defaultIMG.jpg", you should call GetManifestResourceStream as

在你的情况下,如果命名空间是 Company.Application 并且资源文件路径是“Resources/defaultIMG.jpg”,你应该调用 GetManifestResourceStream 作为

Assembly.GetExecutingAssembly().GetManifestResourceStream("Company.Application.Resources.defaultIMG.jpg")

If you're not sure about the actual resource name within the assembly, view the list of resources embedded within the assembly with the following code:

如果您不确定程序集中的实际资源名称,请使用以下代码查看程序集中嵌入的资源列表:

foreach( string resourceName in Assembly.GetExecutingAssembly().GetManifestResourceNames() ) {
  MessageBox.Show(resourceName);
}

So, your code should be something like

所以,你的代码应该是这样的

if (!File.Exists(path_exe + "\images\drinks\defaultIMG.jpg"))
{
    using (var resource =  Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace.Resources.defaultIMG.jpg"))
    {
         using (var file = new FileStream(path_exe + "\images\drinks\defaultIMG.jpg", FileMode.Create, FileAccess.Write))
         {
               resource.CopyTo(file);
         }
}

I'm assuming you've set the "Bulid Action" for your image resource to "Embedded resource".

我假设您已将图像资源的“Bulid Action”设置为“Embedded resource”。

For more information, see Microsoft .NET Framework Resource Basics.

有关详细信息,请参阅 Microsoft .NET Framework 资源基础知识