这篇要说下生 成DLL链接库 窗体,由于只有一个DLL文件,于是这里的生成DLL,只是用了一个简单的COPY,把程序文件夹下的DLL放到了用户指定位置。

界面如图:

代码只是File类的Copy方法:

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ResourceFile
{
public partial class ProduceFile : Form
{

bool isEnglish;
Main main;
string[] chinese = { "生成DLL文件 - L.E.O", "Dll文件保存路径", "生 成", "取 消" };
string[] english = { "Produce DLL Library - L.E.O", "Dll File Save Path", "Produce", "Cancel" };

public ProduceFile(Main m,bool e)
{
isEnglish = e;
main = m;
InitializeComponent();
saveFileDialog1.Filter = "dll类库|*.dll|所有文件|*.*";
saveFileDialog1.FileName = "ResourceFile";
if (isEnglish)
{
this.Text = english[0];
label1.Text = english[1];
button1.Text = english[2];
button2.Text = english[3];
}
else
{
this.Text = chinese[0];
label1.Text = chinese[1];
button1.Text = chinese[2];
button2.Text = chinese[3];
}
}

private void textBox1_Click(object sender, EventArgs e)
{
if (DialogResult.OK == saveFileDialog1.ShowDialog())
{
textBox1.Text = saveFileDialog1.FileName;
}
}

private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
if (isEnglish)
MessageBox.Show("Please select a directory to save!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("请先选择保存目录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
File.Copy("ResourceFile", textBox1.Text, true);
if (DialogResult.Yes == (isEnglish ? MessageBox.Show("Generation is successful, do you want to continue?", "Prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Question) : MessageBox.Show("生成成功,是否继续?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)))
textBox1.Clear();
else
this.Close();
}
catch
{
MessageBox.Show("生成失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void ProduceFile_FormClosed(object sender, FormClosedEventArgs e)
{
main.Show();
}

}
}


现在说下读取资源图片的DLL的代码,做这个DLL目的是为了生成的资源文件便于在其他项目中使用,其中用构造函数的参数接受资源文件路径,调用GetImage()方法可以获得资源文件里的Image[];在其中的GetImage方法中依旧用到了StreamReader和MemoryStream,可以参考查看资源文件窗体的代码,全部代码为:

View Code
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace LoadResourceFile
{
public class LoadImage
{

int count;
int[] c;
byte[][] img;
Image[] image;
MemoryStream ms;
string path;

public LoadImage(string p)
{
path = p;
}

public Image[] GetImage()
{
try
{
StreamReader sr = new StreamReader(path);
string temp = sr.ReadLine();
if (temp.IndexOf("(<--|") != -1 && temp.IndexOf("|-->)") != -1)
{
temp = temp.Replace("(<--|", "");
temp = temp.Replace("|-->)", "");
count = Convert.ToInt32(temp);
}
img = new byte[count][];
string[] t = new string[count];
c = new int[count];
for (int i = 0; i < count; i++)
{
t[i] = sr.ReadLine();
if (t[i].IndexOf("(<--|") != -1 && t[i].IndexOf("|-->)") != -1)
{
t[i] = t[i].Replace("(<--|", "");
t[i] = t[i].Replace("|-->)", "");
c[i] = Convert.ToInt32(t[i]);
}
img[i] = new byte[c[i]];
for (int j = 0; j < c[i]; j++)
{
img[i][j] = Convert.ToByte(sr.ReadLine());
}
}
sr.Close();
image = new Image[count];
for (int i = 0; i < count; i++)
{
ms = new MemoryStream(img[i], 0, c[i]);
image[i] = Image.FromStream(ms);
}
return image;
}
catch
{
MessageBox.Show("获取资源文件出错!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}

}
}