技术核心:
1、使用多线程操作,委托;
2、有完美的进度条和相关揭示;
3、有序存储图片;
软件截图:
![二进制文件图形转换工具二进制文件转换](/Files/20116/c7383040-18a3-49c7-bd39-b47b2de32a5d.png)
![二进制文件图形转换工具二进制文件转换](/Files/20116/955000a0-5557-427e-ae2c-23a03f44566d.png)
![二进制文件图形转换工具二进制文件转换](/Files/20116/e26df5ed-204f-4702-9f98-b3d1a66f6361.png)
![二进制文件图形转换工具二进制文件转换](/Files/20116/557fb424-2698-456f-8906-8f368b47d37e.png)
![二进制文件图形转换工具二进制文件转换](/Files/20116/08d72e83-ec8f-47e9-b8e5-56c2bb9a214a.png)
程序代码:
一、数据库创建
[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 = _disibledevent=>
[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
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、为了方便大家学习与使用,本人出提供源代码下载;
http://files.cnblogs.com/waren168/ConvertIMage.rar 点击下载
最新评论