2012-03-29 57 views
1

我想創建一個程序來生成可執行的幻燈片。創建一個生成自定義EXE的.NET程序

因此,我需要它輸出一個EXE的一些必需的代碼和某些嵌入式資源(圖片)。

.NET是否提供這種功能?

+1

MSDN:'System.Reflection.Emit' – 2012-03-29 14:28:48

+0

你確實需要嵌入圖片嗎?如果沒有,最好製作一個程序從一個文件夾中加載圖片並顯示(全部),您只需要在文件夾中放入新圖片以創建新的幻燈片,或爲其他文件夾提供程序位置 – ata 2012-03-29 14:32:01

+0

你爲什麼要這樣做?爲什麼你不能只使用powerpoint和/或查看器與一些[命令行選項](http://office.microsoft.com/en-us/powerpoint-help/command-line-switches-for-powerpoint-2007-和最的PowerPoint查看器-2007-HA010153889.aspx)? – mtijn 2012-03-29 14:43:09

回答

1

您可以使用CSharpCodeProvider類在運行時編譯代碼並添加嵌入的資源。看看這篇文章,我解釋如何做到這一點:SlideShow Builder

0

這會爲你生成一個進程具有指定名稱(你仍然需要爲圖片添加代碼):

public static Process GenerateRuntimeProcess(string processName, int aliveDuration, bool throwOnException = true) 
    { 
     Process result = null; 
     try 
     { 
      AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName() { Name = processName }, AssemblyBuilderAccess.Save); 
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(processName, processName + ".EXE"); 
      TypeBuilder typeBuilder = moduleBuilder.DefineType("Program", TypeAttributes.Public); 
      MethodBuilder methodBuilder = typeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, null, null); 
      ILGenerator il = methodBuilder.GetILGenerator(); 
      il.UsingNamespace("System.Threading"); 
      il.EmitWriteLine("Hello World"); 
      il.Emit(OpCodes.Ldc_I4, aliveDuration); 
      il.Emit(OpCodes.Call, typeof(Thread).GetMethod("Sleep", new Type[] { typeof(int) })); 
      il.Emit(OpCodes.Ret); 
      typeBuilder.CreateType(); 
      assemblyBuilder.SetEntryPoint(methodBuilder.GetBaseDefinition(), PEFileKinds.ConsoleApplication); 
      assemblyBuilder.Save(processName + ".EXE", PortableExecutableKinds.Required32Bit, ImageFileMachine.I386); 
      result = Process.Start(new ProcessStartInfo(processName + ".EXE") 
      { 
       WindowStyle = ProcessWindowStyle.Hidden 
      }); 
     } 
     catch 
     { 
      if (throwOnException) 
      { 
       throw; 
      } 
      result = null; 
     } 
     return result; 
    } 

可以findmore上System.Reflection.Emit信息在MSDN here或教程herehere

如果我是你,我也會考慮使用powerpoint和/或查看器應用程序和一些命令行選項作爲詳細here。也許你不需要「製作一個應用程序,使另一個應用程序是幻燈片放映」。

1

這很容易完成。

您可以將圖片添加爲嵌入資源,然後使用Reflection技術來發現和檢索嵌入的圖片。

因此,您編寫的程序與圖片列表無關,圖片列表只是嵌入的資源。您可以使用Visual Studio將圖片嵌入爲資源,或者創建一個自定義程序來執行此操作。

你可以在http://msdn.microsoft.com/en-us/library/aa287676(v=VS.71).aspxhttp://www.java2s.com/Code/CSharp/Development-Class/Saveandloadimagefromresourcefile.htm找到一些例子。

祝你好運!

相關問題