这是我的代码:
Assembly_Create(Application.StartupPath + @"\Out.exe");
private static void Assembly_Create(string fileDestination)
{
CompilerParameters settings = new CompilerParameters(); //the heart of CodeDOM
string compilerOptions = "/t:winexe /optimize";
string icon_name = "my_icon.ico";
string icon_location = Path.Combine(Application.StartupPath.Replace("\\bin\\Release", string.Empty), "images", icon_name);
compilerOptions += " /win32icon:\"" + icon_location + "\"";
settings.CompilerOptions = compilerOptions;
settings.ReferencedAssemblies.AddRange(new string[] { "Ionic.Zip.dll", "Microsoft.CSharp.dll", ... });
settings.GenerateExecutable = true;
settings.WarningLevel = 4;
settings.OutputAssembly = fileDestination;
//settings.EmbeddedResources.Add();
string sourceCode = Properties.Resources.Source;
string assemblyInfo = Properties.Resources.AssemblyInfo;
CompilerResults results = new CSharpCodeProvider().CompileAssemblyFromSource(settings, sourceCode, assemblyInfo);
if (results.Errors.HasErrors)
{
}
else
{
}
}
<小时/> 这里是资源区域中的 Source.txt :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ionic.Zip;
using System.Reflection;
using System.Resources;
namespace Game_Setup
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form2 frm = new Form2();
frm.Show();
}
}
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(98, 211);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
}
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 174);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "salam";
//
// pictureBox1
//
this.pictureBox1.Image = global::Game_Setup.Properties.Resources.Logo;
this.pictureBox1.Location = new System.Drawing.Point(0, 105);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(250, 143);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.textBox1);
this.Name = "Form2";
this.Text = "Form2";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.PictureBox pictureBox1;
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
<小时/> 在 Source.txt 中,我添加了
Form1.cs
&amp; Form1.Designer.cs
&amp; Form2.cs
&amp; Form2.Designer.cs
&amp; Program.cs
档案。
this.pictureBox1.Image = global::Game_Setup.Properties.Resources.Logo;
<小时/> 错误是:
类型或命名空间名称“属性”不存在于 命名空间'Game_Setup'(您是否缺少程序集参考?)
如何修复此错误?
我想我应该使用这一行:
//settings.EmbeddedResources.Add();
但是如何将Logo图像嵌入到新的程序集中,如何将其作为pictureBox1图像引入?
<小时/> 我正在使用visual studio 2012,这是Solution Explorer的图片:
答案 0 :(得分:0)
您正在使用Form2
汇编CodeDom
。并且你想要显示图片,它是应用程序的资源,它执行编译。
显然最简单的方法是将该图像作为参数传递。
例如,它可以是static
的{{1}}属性:
Form2
它应该编译没有问题。当您要使用该程序集时,只需将public partial class Form2 : Form
{
public static Image Image {get; set;}
public Form2()
{
InitializeComponent();
pictureBox1.Image = Image;
}
}
设置为您的资源(不确定,所以伪代码)
Image
答案 1 :(得分:0)
我找到了这个解决方案:
我将这些代码添加到Assembly_Create()方法:
IResourceWriter writer = new ResourceWriter("myResources.resources");
writer.AddResource("Logo", new Bitmap(Properties.Resources.Logo));
writer.Generate();
writer.Close();
settings.EmbeddedResources.Add("myResources.resources");
并更改了Source.txt文件,如下所示:
ResourceManager RM = new ResourceManager("myResources", Assembly.GetExecutingAssembly());
this.pictureBox1.Image = (Image)RM.GetObject("Logo");
现在一切正常,我可以使用我的Out.exe文件 请完成我的答案并告诉我们其他方法。