C#做的资源图片打包工具(二)

今天又搞好了几个窗体,其中的制作资源文件窗体效果如图

这个窗体较为重点弄了下接受用户拖拽文件,主要应用了Form的两个事件,并加入了对接受的文件类型判断筛选。

this.DragEnter += new DragEventHandler(MakeFile_DragEnter);
this.DragDrop += new DragEventHandler(MakeFile_DragDrop);
public void MakeFile_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        public void MakeFile_DragDrop(object sender, DragEventArgs e)
        {
            string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            foreach (string File in FileList)
            {
                if (File.EndsWith(".bmp") || File.EndsWith(".gif") || File.EndsWith(".jpeg") || File.EndsWith(".jpg") || File.EndsWith(".png") || File.EndsWith(".ico"))
                    pathListView.Items.Add(File);
                else
                {
                    if (isEnglish)
                        MessageBox.Show("The file format is not supported!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        MessageBox.Show("文件格式不支持!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            if (isEnglish)
                countLabel.Text = "Has Add Images : " + pathListView.Items.Count.ToString() + " Piece";
            else
                countLabel.Text = "已添加图片 : " + pathListView.Items.Count.ToString() + " 张";
        }

 图像保存采用的是利用FileStream实例对象的Read方法读取Image到byte[],然后利用StreamWrite实例对象的WriteLine方法写入到生成的资源文件中。

全部代码如下:

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 MakeFile : Form
{

bool isEnglish;
string[] chinese = { "制作资源文件 - L.E.O", "图片路径", "已添加图片 : 0 张", "添加图片", "删除图片", "保存路劲", "开始制作" };
string[] english = { "Making Resource File - L.E.O", "Images Path", "Has Add Images : 0 Piece", "Add", "Delete", "Save Path", "Start" };
Main main;

public MakeFile(Main m,bool e)
{
main = m;
isEnglish = e;
InitializeComponent();
openFileDialog1.Filter = "BMP图片|*.bmp|GIF图片|*.gif|JPEG图片|*.jpeg;*.jpg|PNG图片|*.png|ICO图标|*.ico|所有图像|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.ico";
saveFileDialog1.Filter = "leo资源文件|*.leo|所有文件|*.*";
this.AllowDrop = true;
//窗体接受拖拽文件事件
this.DragEnter += new DragEventHandler(MakeFile_DragEnter);
this.DragDrop += new DragEventHandler(MakeFile_DragDrop);
if (isEnglish)
{
this.Text = english[0];
columnHeader1.Text = english[1];
countLabel.Text = english[2];
addButton.Text = english[3];
deleteButton.Text = english[4];
textLabel.Text = english[5];
startButton.Text = english[6];
}
else
{
this.Text = chinese[0];
columnHeader1.Text = chinese[1];
countLabel.Text = chinese[2];
addButton.Text = chinese[3];
deleteButton.Text = chinese[4];
textLabel.Text = chinese[5];
startButton.Text = chinese[6];
}
}

public void MakeFile_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}

public void MakeFile_DragDrop(object sender, DragEventArgs e)
{
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (string File in FileList)
{
if (File.EndsWith(".bmp") || File.EndsWith(".gif") || File.EndsWith(".jpeg") || File.EndsWith(".jpg") || File.EndsWith(".png") || File.EndsWith(".ico"))
pathListView.Items.Add(File);
else
{
if (isEnglish)
MessageBox.Show("The file format is not supported!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("文件格式不支持!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
if (isEnglish)
countLabel.Text = "Has Add Images : " + pathListView.Items.Count.ToString() + " Piece";
else
countLabel.Text = "已添加图片 : " + pathListView.Items.Count.ToString() + "";
}

private void addButton_Click(object sender, EventArgs e)
{
if (DialogResult.OK == openFileDialog1.ShowDialog())
{
string[] temppath = openFileDialog1.FileNames;
if (temppath.Length != 0)
{
foreach (string s in temppath)
{
pathListView.Items.Add(s);
}
if (isEnglish)
countLabel.Text = "Has Add Images : " + pathListView.Items.Count.ToString() + " Piece";
else
countLabel.Text = "已添加图片 : " + pathListView.Items.Count.ToString() + "";
}
}
}

private void deleteButton_Click(object sender, EventArgs e)
{
if (pathListView.SelectedItems.Count > 0)
{
while (pathListView.SelectedItems.Count != 0)
{
pathListView.Items.Remove(pathListView.SelectedItems[0]);
}
if (isEnglish)
countLabel.Text = "Has Add Images : " + pathListView.Items.Count.ToString() + " Piece";
else
countLabel.Text = "已添加图片 : " + pathListView.Items.Count.ToString() + "";
}
}

private void startButton_Click(object sender, EventArgs e)
{
//判断是否添加图片
if (pathListView.Items.Count == 0)
{
if (isEnglish)
MessageBox.Show("Please add at least one file!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("请至少添加一个文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//判断是否选择路径
if (pathTextBox.Text == "")
{
if (isEnglish)
MessageBox.Show("Please select a file path!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("请选择保存文件路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
int count = pathListView.Items.Count;
byte[][] img = new byte[count][];
FileStream fs;
//保存每张Image为byte[]
for (int i = 0; i < count; i++)
{
fs = new FileStream(pathListView.Items[i].Text, FileMode.Open);
img[i] = new byte[fs.Length];
fs.Read(img[i], 0, img[i].Length);
fs.Close();
fs.Dispose();
}
StreamWriter sw = new StreamWriter(pathTextBox.Text);
//写入总Image张数
sw.WriteLine("(<--|" + count.ToString() + "|-->)");
for (int i = 0; i < count; i++)
{
//写入当前Image长度
sw.WriteLine("(<--|" + img[i].Length.ToString() + "|-->)");
//写入byte[];
for (int j = 0; j < img[i].Length; j++)
{
sw.WriteLine(img[i][j]);
}
}
sw.Close();
sw.Dispose();
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)))
{
pathListView.Items.Clear();
pathTextBox.Clear();
if (isEnglish)
textLabel.Text = english[5];
else
textLabel.Text = chinese[5];
}
else
{
this.Close();
}
}

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

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

}
}

 

转载于:https://www.cnblogs.com/CeLeo17227720/archive/2011/11/06/CeLeo2011110602.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值