C#实现外部图片的拖拽到应用程序的简单功能,附全部源码,供有需要的参考

简介:

通用权限管理系统组件源码里,有职员管理的功能,实现了直接可以把照片拖拽过来的功能,用起来会很方便。

管理软件能支持拖拽功能,会好用很多,用户体验也会改善很多。想做好一个组件需要把放放面面都彻底做好才可以。

 

想要控件支持拖拽,需要设置 AllowDrop 属性。

 

还需要写下面的2个事件。

 

参考代码如下,有需要的可以参考一下,把有需要的代码参考一下就可以了。

 

代码
// -----------------------------------------------------------
//  All Rights Reserved , Copyright (C) 2010 ,Jirisoft , Ltd .
// -----------------------------------------------------------

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Drawing;
using  System.Data;
using  System.Text;
using  System.Windows.Forms;
using  System.IO;

namespace  DotNet.WinForm.File
{
    
using  DotNet.Model;
    
using  DotNet.Utilities;
    
using  DotNet.Service;

    
///   <summary>
    
///  UCPicture
    
///  照片显示控件
    
///  
    
///  修改纪录
    
///
    
///         2010.12.08 版本:2.0 JiRiGaLa 更新员工更新照片的错误。
    
///         2008.04.29 版本:1.0 JiRiGaLa 创建。
    
///         
    
///  版本:2.0
    
///
    
///   <author>
    
///          <name> JiRiGaLa </name>
    
///          <date> 2010.12.08 </date>
    
///   </author>  
    
///   </summary>
     public   partial   class  UCPicture : UserControl
    {
        
public  UCPicture()
        {
            InitializeComponent();
        }

        
private  BaseUserInfo userInfo  =   new  BaseUserInfo();
        
///   <summary>
        
///  当前操作员信息
        
///   </summary>
         public  BaseUserInfo UserInfo
        {
            
get
            {
                userInfo 
=   new  BaseUserInfo();
                userInfo.GetUserInfo();
                
return  userInfo;
            }
            
set
            {
                userInfo 
=  value;
            }
        }

        
///   <summary>
        
///  保存到数据库
        
///   </summary>
         public   bool  FromDatabase  =   true ;

        
private   string  fileId  =   string .Empty;
        
public   string  FileId
        {
            
get
            {
                
return   this .fileId;
            }
            
set
            {
                
this .fileId  =  value;
                
this .ShowPicture();
            }
        }

        
private   string  folderId  =   string .Empty;
        
public   string  FolderID
        {
            
get
            {
                
return   this .folderId;
            }
            
set
            {
                
this .folderId  =  value;
            }
        }
        
        
private   void  UCPicture_Load( object  sender, EventArgs e)
        {
            
//  设置按钮状态
            
//  this.SetControlState();
        }

        
private   void  UCPicture_DragOver( object  sender, DragEventArgs e)
        {
            
if  (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect 
=  DragDropEffects.Move;
            }
        }

        
private   void  UCPicture_DragDrop( object  sender, DragEventArgs e)
        {
            
if  (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                
string [] file  =  ( string [])e.Data.GetData(DataFormats.FileDrop);
                
for  ( int  i  =   0 ; i  <=  file.Length  -   1 ; i ++ )
                {
                    
if  (System.IO.File.Exists(file[i]))
                    {
                        
this .fileId  =   string .Empty;
                        
//  设置显示的图片
                         this .pic.ImageLocation  =  file[i];
                        
//  设置按钮状态
                         this .SetControlState();
                        
break ;
                    }
                }
            }
        }

        
///   <summary>
        
///  显示图片
        
///   </summary>
         public   void  ShowPicture()
        {
            
if  ( ! String.IsNullOrEmpty( this .FileId))
            {
                
//  显示图片
                 this .ShowPicture( this .FileId);
            }
            
else
            {
                
//  清除图片
                 this .ClearPicture();
            }
        }

        
///   <summary>
        
///  显示图片
        
///   </summary>
        
///   <param name="id"> 主键 </param>
         private   void  ShowPicture( string  id)
        {
            
if  ( ! this .FromDatabase)
            {
                
this .pic.ImageLocation  =  BaseSystemInfo.StartupPath  +  id;
            }
            
else
            {
                
byte [] fileContent  =   null ;
                fileContent 
=   this .Download(id);
                
if  (fileContent  !=   null )
                {
                    
//  this.pic.Image = this.ByteToImage(fileContent);
                    MemoryStream memoryStream  =   new  MemoryStream(fileContent);
                    Bitmap bitmap 
=   new  Bitmap(memoryStream);
                    
this .pic.Image  =  bitmap;
                }
                
else
                {
                    
this .FileId  =   string .Empty;
                    
this .ClearPicture();
                }
            }
            
//  设置按钮状态
             this .SetControlState();
        }

        
///   <summary>
        
///  从数据库中读取文件
        
///   </summary>
        
///   <param name="id"> 文件主键 </param>
        
///   <returns> 文件 </returns>
         public   byte [] Download( string  id)
        {
            
return  ServiceManager.Instance.FileService.Download(UserInfo, id);
            
//  OleDbHelper dbHelper = new SqlHelper();
            
//  dbHelper.Open();
            
//  byte[] fileContent = null;
            
//  string sqlQuery = " SELECT " + BaseFileTable.FieldFileContent
            
//                 + "   FROM " + BaseFileTable.TableName
            
//                 + "  WHERE " + BaseFileTable.FieldId + " = '" + Id + "'";
            
//  OleDbDataReader dataReader = (OleDbDataReader)dbHelper.ExecuteReader(sqlQuery);
            
//  if (dataReader.Read())
            
//  {
            
//     fileContent = (byte[])dataReader[BaseFileTable.FieldFileContent];
            
//  }
            
//  dbHelper.Close();
            
//  return fileContent;
        }

        
public   string  Upload( string  folderId,  string  categoryId)
        {
            
this .FolderID  =  folderId;
            
string  returnValue  =   string .Empty;
            
if  ( ! String.IsNullOrEmpty( this .pic.ImageLocation))
            {
                
//  保存到数据库
                 if  ( this .FromDatabase)
                {
                    
if  (String.IsNullOrEmpty( this .FileId))
                    {
                        returnValue 
=  ServiceManager.Instance.FileService.Upload(UserInfo, folderId, Path.GetFileName( this .pic.ImageLocation), FileUtil.GetFile( this .pic.ImageLocation),  true );
                    }
                    
else
                    {
                        
string  statusCode  =   string .Empty;
                        
string  statusMessage  =   string .Empty;
                        ServiceManager.Instance.FileService.UpdateFile(UserInfo, 
this .FileId, Path.GetFileName( this .pic.ImageLocation), FileUtil.GetFile( this .pic.ImageLocation),  out  statusCode,  out  statusMessage);
                        returnValue 
=   this .FileId;
                    }
                }
                
else
                {
                    
//  复制文件到指定的目录里
                     if  ( ! this .pic.ImageLocation.Equals(BaseSystemInfo.StartupPath  +   this .FileId))
                    {
                        
string  destDirectory  =  BaseSystemInfo.StartupPath  +   " \\UploadFiles "   +   " \\ "   +  folderId  +   " \\ "   +  categoryId;
                        System.IO.Directory.CreateDirectory(destDirectory);
                        
string  destFileName  =  destDirectory  +   " \\ "   +  Path.GetFileName( this .pic.ImageLocation);
                        System.IO.File.Copy(
this .pic.ImageLocation, destFileName);
                        returnValue 
=   " \\UploadFiles "   +   " \\ "   +  folderId  +   " \\ "   +  categoryId  +   " \\ "   +  Path.GetFileName( this .pic.ImageLocation);
                    }
                }
                
//  OleDbHelper dbHelper = new SqlHelper();
                
//  dbHelper.Open();
                
//  string sequence = BaseSequenceManager.Instance.GetSequence(DbHelper, BaseFileTable.TableName);
                
//  OleDbSQLBuilder sqlBuilder = new OleDbSQLBuilder();
                
//  sqlBuilder.BeginInsert(DbHelper, BaseFileTable.TableName);
                
//  sqlBuilder.SetValue(BaseFileTable.FieldId, sequence);
                
//  sqlBuilder.SetValue(BaseFileTable.FieldFolderId, folderId);
                
//  sqlBuilder.SetValue(BaseFileTable.FieldFileName, Path.GetFileName(this.pic.ImageLocation));
                
//   //  byte[] File = this.ImageToByte(this.pic.Image);
                
//  byte[] File = this.GetFile(this.pic.ImageLocation);
                
//  sqlBuilder.SetValue(BaseFileTable.FieldFileContent, File);
                
//  sqlBuilder.SetValue(BaseFileTable.FieldFileSize, File.Length);
                
//  sqlBuilder.SetValue(BaseFileTable.FieldCreateUserId, UserInfo.Id);
                
//  sqlBuilder.SetDBNow(BaseFileTable.FieldCreateDate);
                
//  sqlBuilder.EndInsert();
                
//  dbHelper.Close();
                
//  returnValue = sequence;
            }
            
return  returnValue;
        }

        
private   void  SetControlState( bool  enabled)
        {
            
this .btnSelect.Enabled   =  enabled;
            
this .btnClear.Enabled    =  enabled;
            
this .btnDelete.Enabled   =  enabled;
        }

        
#region  private void SetControlState() 设置按钮状态
        
///   <summary>
        
///  设置按钮状态
        
///   </summary>
         private   void  SetControlState()
        {
            
this .btnSelect.Enabled  =   true ;
            
//  是从数据库里面读取出来的
             if  ( ! String.IsNullOrEmpty( this .FileId)  &&  (String.IsNullOrEmpty( this .pic.ImageLocation)  ||   ! this .FromDatabase))
            {
                
this .btnDelete.Enabled  =   true ;
            }
            
else
            {
                
this .btnDelete.Enabled  =   false ;
            }
            
//  清除按钮
             this .btnClear.Enabled  =   false ;
            
if  ( ! String.IsNullOrEmpty( this .pic.ImageLocation))
            {
                
if  ( this .FromDatabase)
                {
                    
this .btnClear.Enabled  =   true ;
                    
this .btnDelete.Enabled  =   false ;
                }
                
else
                {
                    
if  ( ! this .pic.ImageLocation.Equals(BaseSystemInfo.StartupPath  +   this .FileId))
                    {
                        
this .btnClear.Enabled  =   true ;
                        
this .btnDelete.Enabled  =   false ;
                    }
                }
            }
        }
        
#endregion

        
private   void  pic_DoubleClick( object  sender, EventArgs e)
        {
            
if  ( this .pic.Image  !=   null )
            {
                FrmPicture frmPicture 
=   new  FrmPicture( this .FileId);
                frmPicture.SetImage(
this .pic.Image);
                frmPicture.Show();
            }
        }        

        
private   void  btnSelect_Click( object  sender, EventArgs e)
        {
            OpenFileDialog openFileDialog 
=   new  OpenFileDialog();
            
//  OpenFileDialog.InitialDirectory = Application.StartupPath;
            openFileDialog.Filter  =   " 位图文件(*.bmp)|*.bmp|JPEG(*.JPG;*.JPEG;*.JPE;*.JFIF)|*.JPG;*.JPEG;*.JPE;*.JFIF|GIF(*.GIF)|*.GIF|TIFF(*.TIF;*.TIIF)|*.TIF;*.TIIF|PNG(*.PNG)|*.PNG|ICO(*.ICO)|*.ICO|所有图片文件|(*.bmp;*.JPG;*.JPEG;*.JPE;*.JFIF;*.GIF;*.TIF;*.TIIF;*.PNG;*.ICO)|所有文件|*.* " ;
            openFileDialog.FilterIndex 
=   7 ;
            openFileDialog.RestoreDirectory 
=   true ;
            openFileDialog.Title 
=   " 打开图片文件 " ;
            
if  (openFileDialog.ShowDialog()  ==  DialogResult.OK)
            {
                
this .fileId  =   string .Empty;
                
this .SetControlState( false );
                
this .pic.ImageLocation  =  openFileDialog.FileName;
                
//  设置按钮状态
                 this .SetControlState();
            }
        }

        
///   <summary>
        
///  清除图片
        
///   </summary>
         private   void  ClearPicture()
        {
            
this .pic.ImageLocation  =   string .Empty;
            
this .pic.Image  =   null ;
        }

        
private   void  btnClear_Click( object  sender, EventArgs e)
        {
            
this .ClearPicture();
            
this .ShowPicture();
            
//  设置按钮状态
             this .SetControlState();
        }

        
///   <summary>
        
///  删除文件
        
///   </summary>
        
///   <param name="id"> 主键 </param>
        
///   <returns> 影响的行数 </returns>
         public   int  DeleteFile( string  id)
        {
            
int  returnValue  =   0 ;
            
if  ( this .FromDatabase)
            {
                
//  从数据库服务器删除文件
                returnValue  =  ServiceManager.Instance.FileService.Delete(UserInfo, id);
            }
            
else
            {
                
//  清除图片
                 this .ClearPicture();
                
//  删除文件
                System.IO.File.Delete(BaseSystemInfo.StartupPath  +   this .FileId);
            }
            
return  returnValue;
        }

        
private   void  btnDelete_Click( object  sender, EventArgs e)
        {
            
if  (MessageBox.Show(AppMessage.MSG0207, AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)  ==  DialogResult.OK)
            {
                
this .SetControlState( false );
                
this .DeleteFile( this .FileId);
                
this .FileId  =   string .Empty;
                
this .ShowPicture();
                
//  设置按钮状态
                 this .SetControlState();
            }
        }    
    }
}

 

 

 本文转自jirigala_bao 51CTO博客,原文链接:http://blog.51cto.com/jirigala/809334

相关文章
|
12天前
|
数据采集 安全 JavaScript
C#医院手术麻醉信息管理系统源码 对接院内HIS、LIS、PACS
手麻系统的功能涵盖了麻醉临床业务管理、麻醉运营业务管理以及手术进程管理等,实现了将多种麻醉病历文书与医院HIS系统的有效关联,让手术室人员、设备资源和信息资源高度共享;实现了手术安排、各种统计报表等科室管理和科研工作的需求,可借其收集临床数据、进行整合分析,为围术期临床信息、管理、科研提供整体解决方案;该系统的运行,为医护人员提供了流程化、信息化、自动化、智能化的临床业务综合管理。
25 5
|
3月前
|
监控 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK获取每张图像的微秒时间和FrameID功能(C#)
48 0
|
3月前
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用ForceIP强制修改网口IP功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用ForceIP强制修改网口IP功能(C#)
26 0
|
3月前
|
安全 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用短曝光功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用短曝光功能(C#)
34 0
|
3月前
|
编解码 监控 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用Binning像素合并功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK使用Binning像素合并功能(C#)
17 0
|
3月前
|
存储 数据管理 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK设置相机本身的数据保存(CustomData)功能(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK设置相机本身的数据保存(CustomData)功能(C#)
26 0
|
1天前
|
C#
【C#】 如何实现文本框历史记录提示功能
【C#】 如何实现文本框历史记录提示功能
11 0
|
8天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
|
25天前
|
安全 JavaScript 前端开发
C#医院手麻系统源码,手术麻醉管理系统源码
手术麻醉管理系统贯穿患者入院至出院全程,整合术前术后流程,如手术排班、麻醉计划、术中监护及术后恢复。采用C#.net6.0、Vue、Ant-Design等技术,与sqlserver2019数据库结合。系统提供麻醉记录、手术安全核查、费用管理等功能,确保信息数字化和流程规范化。通过自动化记录和数据分析,增强手术安全,优化资源分配,并支持医疗质控与研究。
C#医院手麻系统源码,手术麻醉管理系统源码
|
27天前
|
存储 监控 安全
C#手术麻醉系统源码 大型医院手麻系统4大需求是什么?
C#编写的大型医院手术麻醉系统旨在满足四大核心需求:智慧医院建设,要求实时患者信息共享与监控;医院等级评级,强调安全评估与术后管理;电子病历评级,规定手术预约、麻醉信息的标准化;科室需求,包括系统互联、流程信息化、数据追溯、操作简便、文书完整、快速响应、生命体征动态采集及质量控制。系统提升手麻工作的效率与安全性,确保手术顺利进行。