二进制文件图形转换工具

好久没有写文章 ,今天刚好有点空就把我写的一个小工具分享一下吧,就是把数据库中的存储的二进制图形文件转换成直观的JPEG 图像,并有序的保存起来。

技术核心:

   1、使用多线程操作,委托;

   2、有完美的进度条和相关揭示;

   3、有序存储图片;

软件截图:

 

 

程序代码:

  一、数据库创建

 [UserImage] 表数据源表  用于存储图像(为二进制文件)

CREATE TABLE [dbo].[UserImage](
	[vcEmployeeID] [char](10) NOT NULL,
	[vcName] [varchar](200) NULL,
	[vcAccount] [varchar](30) NULL,
	[pWorkPicture] [image] NULL,
	[dOperatorDate] [datetime] NULL,
 CONSTRAINT [PK_UserImage] PRIMARY KEY CLUSTERED 
(
	[vcEmployeeID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

[Users] 用户信息表 

CREATE TABLE [dbo].[Users](
[Key] [
int] IDENTITY(1,1) primary key NOT NULL,
[Name] [nvarchar](
256) NOT NULL,
[Nickname] [nvarchar](
256) NOT NULL,
[Type] [
int] NOT NULL,
[RegisterTime] [datetime] NOT NULL,
[HomePage] [nvarchar](
256) NULL,
[HeadIMG] [nvarchar](
512) NOT NULL,
[Remark] [text] NOT NULL
)

二、需求

要把UserImage 表的中 [pWorkPicture] 的二进制 文件转换图片并保存在 Users 表中对应的 [HeadIMG] 注意: [HeadIMG] 只能存储的是图片路径

三、程序实现(步骤)

  1、用VS2010 创建新项目 选择 C# ----- WINDOWS 应用程序;

  2、画出上面的图形界面;

  3、写代码实例; 

四、核心代码

  1、进度条、状态信息功能相关代码,为了避免程序进度条卡死,所以我使用了委托线程,代码如下

1 private delegate void DisPrcent(string txtvalue);
2
3 private void showDisPrcent(string txtvalue)
4 {
5 if (prcent.InvokeRequired)
6 {
7 prcent.Invoke(new DisPrcent(showDisPrcent), txtvalue);
8 }
9 else
10 {
11 string[] txt = txtvalue.Split(',');
12 double current = int.Parse(txt[0].ToString());
13 double Totel = int.Parse(txt[1].ToString());
14 prcent.Text = (current / Totel).ToString("0%");
15
16 }
17 }
18
19 private delegate void DisCenterContent(string content);
20 private void ShowDisCenterContent(string content)
21 {
22 if (CenterContent.InvokeRequired)
23 {
24 CenterContent.Invoke(new DisCenterContent(ShowDisCenterContent), content);
25 }
26 else
27 {
28 CenterContent.Text = content;
29 }
30 }
31
32
33 private delegate void dispProgStatus(ProState state);
34
35 private void showProgStatus(ProState state)
36 {
37 if (progressBar1.InvokeRequired)
38 {
39 progressBar1.Invoke(new dispProgStatus(showProgStatus), state);
40 }
41 else
42 {
43 int current = 0;
44 Int32.TryParse(state.MinCount.ToString(), out current);
45 int total = 0;
46 Int32.TryParse(state.MaxCount.ToString(), out total);
47 progressBar1.Value = current;
48 progressBar1.Maximum = total;
49 }
50
51 if (label_pro.InvokeRequired)
52 {
53 label_pro.Invoke(new dispProgStatus(showProgStatus), state);
54 }
55 else
56 {
57 label_pro.Text = state.ValueText;
58 }
59 }
60
61 private delegate void Buttonen(bool state);
62
63 private void ShowButtonen(bool state)
64 {
65 if (button4.InvokeRequired)
66 {
67 button4.Invoke(new Buttonen(ShowButtonen), state);
68 }
69 else
70 {
71 if (state == true)
72 {
73 button4.Enabled = true;
74
75 }
76 else
77 {
78 button4.Enabled = false;
79 }
80 }
81
82 if (SelectFod.InvokeRequired)
83 {
84 button4.Invoke(new Buttonen(ShowButtonen), state);
85 }
86 else
87 {
88 if (state == true)
89 {
90 SelectFod.Enabled = true;
91 }
92 else
93 {
94 SelectFod.Enabled = false;
95 }
96 }
97
98 if (FileUrl.InvokeRequired)
99 {
100 FileUrl.Invoke(new Buttonen(ShowButtonen), state);
101
102 }
103 else
104 {
105 if (state == true)
106 {
107 FileUrl.Enabled = true;
108 }
109 else
110 {
111 FileUrl.Enabled = false;
112
113 }
114 }
115
116
117
118 }

   2、二进制转换为JPEG 图形方法 代码

private void ImageSave(byte[] barrImage, string userName, string Names)
{
MemoryStream ms
= new MemoryStream(barrImage);
Image image
= Image.FromStream(ms);
string Nikname = string.Format("{0}-{1}", userName, Names);
string imgurl = HeadimgeCreatUrl(userName);
image.Save(string.Format("{0}/{1}.jpeg", imgurl, Nikname), ImageFormat.Jpeg);
AccountImage ai
= new AccountImage();
ai.AccountName
= userName;
ai.UserImageUrl
= string.Format("/{0}/{1}.jpeg", imgurl.Substring(imgurl.IndexOf(userName)), Nikname);
ImageList.Add(ai);
}

3、加载数据代码 

public void LoadData(string state)
{
string type = state;
string Count = SqlHelper.ExecuteScalar(ConString, CommandType.Text, "select count(*) as Counts from UserImage").ToString();

int p = int.Parse(Count);
SqlDataReader rd
= SqlHelper.ExecuteReader(ConString, CommandType.Text, "select * from UserImage");
int i = 0;
while (rd.Read())
{
UserInfo ui
= new UserInfo();
ui.UserId
= rd[0].ToString();
ui.FName
= rd[1].ToString();
ui.UserName
= rd[2].ToString();
ui.ImageValue
= (byte[])rd[3];
userList.Add(ui);

i
++;
ProState ps
= new ProState();
ps.MinCount
= i;
ps.MaxCount
= p;
ps.ValueText
= type + i.ToString() + "/" + Count.ToString();
string tt = string.Format("正在取数据 {0} ……", rd[2].ToString());
if (i == p)
{
ps.ValueText
= "数据加载已完,正在分析数据中,请稍后……";
tt
= "进程一处理已完,正等待下一进程中……";
}

showProgStatus(ps);
showDisPrcent(
string.Format("{0},{1}", i, p));
ShowDisCenterContent(tt);
}

}

 4、处理阶段代码  

public string HeadimgeCreatUrl(string UserName)
{
string UrlFile = string.Format("{0}/{1}/pub", Furl, UserName);
return new UserData().CreatDir(UrlFile);
}

public List<AccountImage> ImageList = new List<AccountImage>();
private void StartConvert(object type)
{
int s = 0;
type
= type.ToString();
foreach (var img in userList)
{
object rs = img.ImageValue;
byte[] bary = (byte[])rs;
ImageSave(bary, img.UserName, img.FName);
s
++;
ProState ps
= new ProState();
ps.MinCount
= s;
ps.MaxCount
= userList.Count;
ps.ValueText
= type + s.ToString() + "/" + userList.Count.ToString();
string cc = string.Format("正在转换 {0} ……", img.UserName);
if (s == userList.Count)
{
ps.ValueText
= "图片已转换完成,正在分析头像信息,请稍后……";
cc
= "进程处理二已完成,正等待下一进程中……";
}
string Nikname = string.Format("{0}-{1}", img.UserName, img.FName);
ShowDisCenterContent(cc);
showProgStatus(ps);
showDisPrcent(
string.Format("{0},{1}", s, userList.Count));

}

}

private void UpDataUser(object type)
{
type
= type.ToString();
int t = 0;
foreach (var us in ImageList)
{
SqlHelper.ExecuteNonQuery(ConString, CommandType.StoredProcedure,
"UPdataUserImage", new SqlParameter("@Name", us.AccountName), new SqlParameter("@HeadIMG", us.UserImageUrl));
t
++;
ProState ps
= new ProState();
ps.MinCount
= t;
ps.MaxCount
= userList.Count;
ps.ValueText
= type + t.ToString() + "/" + ImageList.Count.ToString();
string pp = string.Format("正在更新 {0} ……", us.AccountName);
if (t == ImageList.Count)
{
ps.ValueText
= "数据头像更新已完成!";
pp
= "进程处理已全部完成!";
}
ShowDisCenterContent(pp);
showProgStatus(ps);
showDisPrcent(
string.Format("{0},{1}", t, ImageList.Count));

}
MessageBox.Show(
"头像数据处理已完!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

5、开始执行代码 

private void button4_Click(object sender, EventArgs e)
{

object state = "正在读取数据中……";

ThreadPool.QueueUserWorkItem(dispRentReport, state);

}

private void GetUserImage()
{
object state = "正在转换图片中,请稍后……";
StartConvert(state);

}

private void StartUpdata()
{
object state = "正在更新用户数据表,请稍后……";
UpDataUser(state);
}
private string Furl = string.Empty;

private void dispRentReport(object state)
{
Furl
= FileUrl.Text;
if (Furl != "" & Furl != "")
{
ShowButtonen(
false);
LoadData(state.ToString());
Thread.Sleep(
4000);
GetUserImage();
Thread.Sleep(
4000);
StartUpdata();
ShowButtonen(
true);
}
else
{
MessageBox.Show(
"文件存放路径没有选择", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


}

6、为了方便大家学习与使用,本人出提供源代码下载;

  https://files.cnblogs.com/waren168/ConvertIMage.rar 点击下载

posted on 2011-06-14 17:03  willis_wu  阅读(11979)  评论(4编辑  收藏  举报