简体   繁体   中英

How to extract an image from embedded resources to temp folder then execute it?

I've an application which compiles another winform application from a source file and create a .resources file at runtime then add the .resources file as an embedded resource during compile time.. The code i'm using for creating a new .resources file is the below :

 // Creating .resources file from a file named Photo.jpg

        string myfile = "photo.jpg";
        using (ResourceWriter rw = new ResourceWriter(@".\photoRes.resources"))
         {
           rw.AddResource("MyPhoto",File.ReadAllBytes(myfile));
         }

Then this resource file will be added to the compiled program as Embedded Resource. The line below will do that job

        // -- -- -- inside the CompileExecutable() function -- -- --
         CompilerParameters cp = new CompilerParameters();
         ..
         ..
         if (provider.Supports(GeneratorSupport.Resources))
            {
                cp.EmbeddedResources.Add("bindedfile.resources");
            }
         .. 
         ..

Till now, everything works ok, the compiled program that we compile from source file will be ( Program Size + Picture Size ) .

A Picture is now an embedded resource of that file. So in order to access the picture, extract it and execute it, some lines of code need to be added to the source of that file, right ?

My question is what should be written inside the source.cs file to tell it to extract the image which is an embedded resource of it then execute it from a temp folder.

When i add the line below to source.cs

string[] arrayofstrings = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
        foreach(string s in arrayofstrings) {MessageBox.Show(s);}

It shows me one message box with this text : photoRes.resources

How to read the embedded resource and extract the image to temp folder then execute it? What lines to add to source.cs ?

此支持文章如何使用Visual C#嵌入和访问资源

_imageStream = _assembly.GetManifestResourceStream("MyNameSpace.MyImage.bmp");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM