在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例常用C#方法 → C# winform 图片上传 实例源码

C# winform 图片上传 实例源码

常用C#方法

下载此实例
  • 开发语言:C#
  • 实例大小:0.44M
  • 下载次数:112
  • 浏览次数:1206
  • 发布时间:2017-04-07
  • 实例类别:常用C#方法
  • 发 布 人:lgfalmh
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 上传 图片 C# c 图片上传

实例介绍

【实例简介】

【实例截图】

【核心代码】

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 图片上传 实例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警