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

第二个子窗体 查看资源文件 ,效果如图:

其中,读取Image主要用的StreamReader、MemoryStream类,将资源文件中的byte逐行读取到byte[],再转化为Image类型。

然后,又做了抽取图片文件的功能,主要分两种方法做的,对于BMP,PNG,JPRG,GIF用的是Image的Save(Stream,ImageCodeInfo,EncoderParameters)方法,icon文件用Bitmap的GetHicon获取到Intptr,利用Intptr获得Icon对象,再通过FileStream保存为图标文件,目前这种方法得到的Icon质量不高,正在寻找新方法解决。

全部代码如下:

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;
using System.Drawing.Imaging;
using System.Reflection;

namespace ResourceFile
{
public partial class ViewFile : Form
{

bool isEnglish;
bool isExtractionAll;
int imageIndex;
string[] chinese = { "查看资源文件 - L.E.O", "资源文件路径", "打开文件", "图片序号", "抽取已选", "抽取全部", "保存路径", "保存名称", "保存类型", "确定", "取消" };
string[] english = { "View Resource File - L.E.O", "Resource File Path", "Open", "Image Number", "Extraction", "ExtractionAll", "Save path", "Save the name", "Save as type", "OK", "Cancel" };
Main main;
int count;
int[] c;
byte[][] img;
Image[] image;
MemoryStream ms;

public ViewFile(Main m, bool e)
{
main = m;
isEnglish = e;
InitializeComponent();
extractionPanel.Visible = false;
openFileDialog1.Filter = "leo资源文件|*.leo|所有文件|*.*";
if (isEnglish)
{
this.Text = english[0];
pathLabel1.Text = english[1];
openButton.Text = english[2];
columnHeader1.Text = english[3];
extractionButton.Text = english[4];
extractionAllButton.Text = english[5];
pathLabel2.Text = english[6];
nameLabel.Text = english[7];
formatLabel.Text = english[8];
okButton.Text = english[9];
cancelButton.Text = english[10];
}
else
{
this.Text = chinese[0];
pathLabel1.Text = chinese[1];
openButton.Text = chinese[2];
columnHeader1.Text = chinese[3];
extractionButton.Text = chinese[4];
extractionAllButton.Text = chinese[5];
pathLabel2.Text = chinese[6];
nameLabel.Text = chinese[7];
formatLabel.Text = chinese[8];
okButton.Text = chinese[9];
cancelButton.Text = chinese[10];
}
formatComboBox.KeyPress+=new KeyPressEventHandler(formatComboBox_KeyPress);
}

private void formatComboBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}

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

private void openButton_Click(object sender, EventArgs e)
{
if (pathTextBox1.Text == "")
{
if (isEnglish)
MessageBox.Show("Please choose to view the resource file!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("请选择要查看的资源文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
imageListView.SelectedIndexChanged -= new EventHandler(imageListView_SelectedIndexChanged);
LoadByte();
LoadImage();
imageListView.SelectedIndexChanged += new EventHandler(imageListView_SelectedIndexChanged);
}

public void LoadByte()
{
try
{
StreamReader sr = new StreamReader(pathTextBox1.Text);
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();
sr.Dispose();
}
catch
{
if (isEnglish)
MessageBox.Show("Program error, please try again!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
MessageBox.Show("程序出错,请重试!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
}

public void LoadImage()
{
image = new Image[count];
imageListView.Items.Clear();
for (int i = 0; i < count; i++)
{
ms = new MemoryStream(img[i], 0, c[i]);
image[i] = Image.FromStream(ms);
image[i] = ZoomAuto(image[i], imageLabel.Width, imageLabel.Height);
if (isEnglish)
imageListView.Items.Add("Resource Image " + i.ToString());
else
imageListView.Items.Add("资源图片 " + i.ToString());
}
imageLabel.Image = image[0];
imageListView.Items[0].Selected = true;
ms.Close();
ms.Dispose();
}

#region 等比缩放

///<summary>
/// 图片缩放
///</summary>
///<remarks>L.E.O 2011-11-05</remarks>
///<param name="Image">原图</param>
///<param name="targetWidth">指定的最大宽度</param>
///<param name="targetHeight">指定的最大高度</param>

public Image ZoomAuto(Image sImage, Double targetWidth, Double targetHeight)
{

//原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
Image initImage = sImage;

//原图宽高均小于模版,不作处理,直接保存
if (initImage.Width <= targetWidth && initImage.Height <= targetHeight)
{
return initImage;
}

else
{

//缩略图宽、高计算
double newWidth = initImage.Width;
double newHeight = initImage.Height;

//宽大于高或宽等于高(横图或正方)
if (initImage.Width > initImage.Height || initImage.Width == initImage.Height)
{
//如果宽大于模版
if (initImage.Width > targetWidth)
{
//宽按模版,高按比例缩放
newWidth = targetWidth;
newHeight = initImage.Height * (targetWidth / initImage.Width);
}
}

//高大于宽(竖图)
else
{
//如果高大于模版
if (initImage.Height > targetHeight)
{
//高按模版,宽按比例缩放
newHeight = targetHeight;
newWidth = initImage.Width * (targetHeight / initImage.Height);
}
}

//生成新图
//新建一个bmp图片
Image newImage = new Bitmap((int)newWidth, (int)newHeight);

//新建一个画板
Graphics newG = Graphics.FromImage(newImage);

//设置质量
newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//置背景色
newG.Clear(Color.White);

//画图
newG.DrawImage(initImage, new Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), GraphicsUnit.Pixel);

//释放资源
newG.Dispose();
initImage.Dispose();

//保存缩略图
return newImage;

}
}

#endregion

private void imageListView_SelectedIndexChanged(object sender, EventArgs e)
{
if (imageListView.SelectedItems.Count == 1)
imageLabel.Image = image[imageListView.Items.IndexOf(imageListView.FocusedItem)];
}

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

private void extractionButton_Click(object sender, EventArgs e)
{
if (imageListView.SelectedItems.Count == 1)
{
extractionPanel.Visible = true;
isExtractionAll = false;
imageIndex = imageListView.Items.IndexOf(imageListView.FocusedItem);
}
else
{
if (isEnglish)
MessageBox.Show("Please select a file to extract!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("请选择一个要提取的文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void extractionAllButton_Click(object sender, EventArgs e)
{
if (imageListView.Items.Count > 0)
{
extractionPanel.Visible = true;
isExtractionAll = true;
}
else
{
if (isEnglish)
MessageBox.Show("No resource file is opened!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("没有资源文件被打开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void pathTextBox2_Click(object sender, EventArgs e)
{
if (DialogResult.OK == folderBrowserDialog1.ShowDialog())
pathTextBox2.Text = folderBrowserDialog1.SelectedPath + "\\";
}

private void okButton_Click(object sender, EventArgs e)
{
string itype = "";
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/png");
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myEncoder = System.Drawing.Imaging.Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
if (pathTextBox2.Text == "")
{
if (isEnglish)
MessageBox.Show("Please input the saved file path!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("请输入保存文件的路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (nameTextBox.Text == "")
{
if (isEnglish)
MessageBox.Show("Please input the saved file name!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("请输入保存文件的名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
switch (formatComboBox.Text)
{
case "*.png":
myImageCodecInfo = GetEncoderInfo("image/png");
itype = ".png";
break;
case "*.bmp":
myImageCodecInfo = GetEncoderInfo("image/bmp");
itype = ".png";
break;
case "*.gif":
myImageCodecInfo = GetEncoderInfo("image/gif");
itype = ".png";
break;
case "*.jpeg;*.jpg":
myImageCodecInfo = GetEncoderInfo("image/jpeg");
itype = ".png";
break;
case "*.ico":
itype = ".ico";
break;
}
if (isExtractionAll)
{
if (formatComboBox.Text == "*.ico")
{
for (int i = 0; i < count; i++)
{
Bitmap myBitmap = new Bitmap(image[i], image[i].Width, image[i].Height);
IntPtr Hicon = myBitmap.GetHicon();
Icon newIcon = Icon.FromHandle(Hicon);
FileStream outStream = new FileStream(pathTextBox2.Text + nameTextBox.Text + i.ToString() + itype, FileMode.OpenOrCreate);
newIcon.Save(outStream);
outStream.Flush();
outStream.Close();
}
}
else
{
for (int i = 0; i < count; i++)
{
image[i].Save(pathTextBox2.Text + nameTextBox.Text + i.ToString() + itype, myImageCodecInfo, myEncoderParameters);
}
}
}
else
{
if (formatComboBox.Text == "*.ico")
{
Bitmap myBitmap = new Bitmap(image[imageIndex], image[imageIndex].Width, image[imageIndex].Height);
IntPtr Hicon = myBitmap.GetHicon();
Icon newIcon = Icon.FromHandle(Hicon);
FileStream outStream = new FileStream(pathTextBox2.Text + nameTextBox.Text + itype, FileMode.OpenOrCreate);
newIcon.Save(outStream);
outStream.Flush();
outStream.Close();
}
else
{
image[imageIndex].Save(pathTextBox2.Text + nameTextBox.Text + itype, myImageCodecInfo, myEncoderParameters);
}
}
GoOn();
}

private void cancelButton_Click(object sender, EventArgs e)
{
extractionPanel.Visible = false;
pathTextBox2.Clear();
nameTextBox.Clear();
formatComboBox.Text = "*.png";
}

private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}

public void GoOn()
{
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)))
{
extractionPanel.Visible = false;
pathTextBox2.Clear();
nameTextBox.Clear();
formatComboBox.Text = "*.png";
}
else
{
this.Close();
}
}

}
}

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值