基本信息
源码名称:C# winform 图片上传 实例源码
源码大小:0.44M
文件格式:.rar
开发语言:C#
更新时间:2017-04-07
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

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);
            }
        }

    }

}