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 ImageUpLoad{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } //添加附件的单击事件 private void btnSearch_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog();//显示选择文件对话框 ofd.ShowDialog(); //得到上传文件的完整名 string loadFullName = ofd.FileName.ToString(); this.picShow.ImageLocation = loadFullName; //上传文件的文件名 string loadName = loadFullName.Substring(loadFullName.LastIndexOf("\\") 1); //上传文件的类型 string loadType = loadFullName.Substring(loadFullName.LastIndexOf(".") 1).ToLower(); //判断文件类型 if (!loadType.Equals("jpg") && !loadType.Equals("gif") && !loadType.Equals("png")) { MessageBox.Show("文件不合法!仅限于JPG,GIF,PNG格式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //将文件名显示到标签上 this.lblMessage.Text = "\n" loadFullName; //调用添加到TreeView显示的方法 AddFileToTreeView(loadFullName, loadName); } //上传按钮的事件 private void btnUpLoad_Click(object sender, EventArgs e) { TreeNodeCollection tnNodeColl = tvInfo.Nodes;//获取TreeView的节点 int nodeCount = tnNodeColl.Count;//获取节点的总数 int successCount = 0;//成功上传文件的变量 if (nodeCount > 0) { //选择保存文件的路径 //folderBrowserDialog1.ShowDialog(); //string loadPath = folderBrowserDialog1.SelectedPath; string tempPath = Application.StartupPath;//获取系统启动路径 string temp = tempPath.Substring(0, tempPath.LastIndexOf("bin") - 1) "/Images";//设置保存图片的文件夹 string loadPath = temp; string nodeText = string.Empty; string nodeTipText = string.Empty; //循环将TreeView中的数据获取 foreach (TreeNode node in tnNodeColl) { if (node.Checked) { nodeText = node.Text.ToString(); nodeTipText = node.ToolTipText; string loadFile = loadPath "\\" nodeTipText; bool isExists = JudgeFileExists(loadFile, nodeTipText); if (isExists) { byte[] btFile = FileToBinary(nodeText);//调用读取方法保存图片 if (BinaryToFile(loadFile, btFile)) { node.Checked = false; successCount ; continue; } else { break; } } else { continue; } } else { continue; } } //在标签给予提示 string strCue = "成功上传" successCount.ToString() "个文件。\n"; this.tvInfo.Nodes.Clear(); int failCount = nodeCount - successCount; strCue = "上传失败" failCount.ToString() "个文件。\n"; this.lblMessage.Text = strCue; } else { MessageBox.Show("请选择要上传的文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } /// <summary> /// 将文件转换为二进制流进行读取 /// </summary> /// <param name="fileName">文件完整名</param> /// <returns>二进制流</returns> private byte[] FileToBinary(string fileName) { try { FileStream fsRead = new FileStream(fileName, FileMode.Open, FileAccess.Read); if (fsRead.CanRead) { int fsSize = Convert.ToInt32(fsRead.Length); byte[] btRead = new byte[fsSize]; fsRead.Read(btRead, 0, fsSize); return btRead; } else { MessageBox.Show("文件读取错误!"); return null; } } catch (Exception ce) { MessageBox.Show(ce.Message); return null; } } /// <summary> /// 将二进制流转换为对应的文件进行存储 /// </summary> /// <param name="filePath">接收的文件</param> /// <param name="btBinary">二进制流</param> /// <returns>转换结果</returns> private bool BinaryToFile(string fileName, byte[] btBinary) { bool result = false; try { FileStream fsWrite = new FileStream(fileName, FileMode.Create, FileAccess.Write); if (fsWrite.CanWrite) { fsWrite.Write(btBinary, 0, btBinary.Length); result = true; } else { result = false; } } catch (Exception ce) { MessageBox.Show(ce.Message); result = false; } return result; } /// <summary> /// 判断文件是否存在 /// </summary> /// <param name="fileName">文件完整的路径名</param> /// <param name="nodeTipText">文件名</param> /// <returns>判断结果</returns> private bool JudgeFileExists(string fileName, string nodeTipText) { if (File.Exists(fileName)) { StringBuilder sbError = new StringBuilder(); sbError.Append(nodeTipText "已存在于:\n"); sbError.Append(fileName.Substring(0, fileName.LastIndexOf("\\")) "\n"); sbError.Append("中,是否覆盖原文件?"); string strSearch = MessageBox.Show(sbError.ToString(), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk).ToString(); if (strSearch == "Yes") { return true; } else { return false; } } else { return true; } } /// <summary> /// 为TreeView单赋值,即待上传文件的完整名 /// </summary> /// <param name="fullName"></param> /// <param name="simpleName"></param> private void AddFileToTreeView(string fullName, string simpleName) { TreeNodeCollection nodeCollection = this.tvInfo.Nodes; TreeNode node = new TreeNode(); node.Text = fullName; node.ToolTipText = simpleName; node.Checked = true; nodeCollection.Add(node); if (nodeCollection.Count == 1) { Button btnUpLoad = new Button(); btnUpLoad.Text = "上传"; btnUpLoad.Click = new EventHandler(btnUpLoad_Click); //panel1.Controls.Add(btnUpLoad); } } }}
下载C# winform 图片上传 实例源码用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论