C#.图片拖动处理(摘抄)

摘要:
本文摘自博客园牛人吉日嘎啦。通用权限管理系统组件源码里,有职员管理的功能,实现了直接可以把照片拖拽过来的功能,用起来会很方便。想要控件支持拖拽,需要设置AllowDrop属性。还需要写下面的2个事件。//-----------------------------------------------------------//AllRightsReserved,Copyright2010,Jirisoft,Ltd.//-----------------------------------------------------------usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;namespaceDotNet.WinForm.File{usingDotNet.Model;usingDotNet.Utilities;usingDotNet.Service;//////UCPicture///照片显示控件//////修改纪录//////2010.12.08版本:2.0JiRiGaLa更新员工更新照片的错误。///2008.04.29版本:1.0JiRiGaLa创建。//////版本:2.0/////////JiRiGaLa///2010.12.08//////publicpartialclassUCPicture:UserControl{publicUCPicture(){InitializeComponent();}privateBaseUserInfouserInfo=newBaseUserInfo();//////当前操作员信息///publicBaseUserInfoUserInfo{get{userInfo=newBaseUserInfo();userInfo.GetUserInfo();returnuserInfo;}set{userInfo=value;}}//////保存到数据库///publicboolFromDatabase=true;privatestringfileId=string.Empty;publicstringFileId{get{returnthis.fileId;}set{this.fileId=value;this.ShowPicture();}}privatestringfolderId=string.Empty;publicstringFolderID{get{returnthis.folderId;}set{this.folderId=value;}}privatevoidUCPicture_Load{//设置按钮状态//this.SetControlState();}privatevoidUCPicture_DragOver{if{e.Effect=DragDropEffects.Move;}}privatevoidUCPicture_DragDrop{if{string[]file=e.Data.GetData;for{if{this.fileId=string.Empty;//设置显示的图片this.pic.ImageLocation=file[i];//设置按钮状态this.SetControlState();break;}}}}//////显示图片///publicvoidShowPicture(){if(!String.IsNullOrEmpty){//显示图片this.ShowPicture;}else{//清除图片this.ClearPicture();}}//////显示图片//////主键privatevoidShowPicture{if(!

本文摘自博客园牛人吉日嘎啦。

通用权限管理系统组件源码里,有职员管理的功能,实现了直接可以把照片拖拽过来的功能,用起来会很方便。C#.图片拖动处理(摘抄)第1张

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

C#.图片拖动处理(摘抄)第2张

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

C#.图片拖动处理(摘抄)第3张

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

//-----------------------------------------------------------
//AllRightsReserved,Copyright(C)2010,Jirisoft,Ltd.
//-----------------------------------------------------------
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Drawing;
usingSystem.Data;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.IO;
namespaceDotNet.WinForm.File
{
usingDotNet.Model;
usingDotNet.Utilities;
usingDotNet.Service;
///<summary>///UCPicture
///照片显示控件
//////修改纪录
//////2010.12.08版本:2.0JiRiGaLa更新员工更新照片的错误。
///2008.04.29版本:1.0JiRiGaLa创建。
//////版本:2.0
//////<author>///<name>JiRiGaLa</name>///<date>2010.12.08</date>///</author>///</summary>publicpartialclassUCPicture:UserControl
{
publicUCPicture()
{
InitializeComponent();
}
privateBaseUserInfouserInfo=newBaseUserInfo();
///<summary>///当前操作员信息
///</summary>publicBaseUserInfoUserInfo
{
get
{
userInfo=newBaseUserInfo();
userInfo.GetUserInfo();
returnuserInfo;
}
set
{
userInfo=value;
}
}
///<summary>///保存到数据库
///</summary>publicboolFromDatabase=true;
privatestringfileId=string.Empty;
publicstringFileId
{
get
{
returnthis.fileId;
}
set
{
this.fileId=value;
this.ShowPicture();
}
}
privatestringfolderId=string.Empty;
publicstringFolderID
{
get
{
returnthis.folderId;
}
set
{
this.folderId=value;
}
}
privatevoidUCPicture_Load(objectsender,EventArgse)
{
//设置按钮状态
//this.SetControlState();}
privatevoidUCPicture_DragOver(objectsender,DragEventArgse)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect=DragDropEffects.Move;
}
}
privatevoidUCPicture_DragDrop(objectsender,DragEventArgse)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[]file=(string[])e.Data.GetData(DataFormats.FileDrop);
for(inti=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>publicvoidShowPicture()
{
if(!String.IsNullOrEmpty(this.FileId))
{
//显示图片this.ShowPicture(this.FileId);
}
else
{
//清除图片this.ClearPicture();
}
}
///<summary>///显示图片
///</summary>///<paramname="id">主键</param>privatevoidShowPicture(stringid)
{
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);MemoryStreammemoryStream=newMemoryStream(fileContent);
Bitmapbitmap=newBitmap(memoryStream);
this.pic.Image=bitmap;
}
else
{
this.FileId=string.Empty;
this.ClearPicture();
}
}
//设置按钮状态this.SetControlState();
}
///<summary>///从数据库中读取文件
///</summary>///<paramname="id">文件主键</param>///<returns>文件</returns>publicbyte[]Download(stringid)
{
returnServiceManager.Instance.FileService.Download(UserInfo,id);
//OleDbHelperdbHelper=newSqlHelper();
//dbHelper.Open();
//byte[]fileContent=null;
//stringsqlQuery="SELECT"+BaseFileTable.FieldFileContent
//+"FROM"+BaseFileTable.TableName
//+"WHERE"+BaseFileTable.FieldId+"='"+Id+"'";
//OleDbDataReaderdataReader=(OleDbDataReader)dbHelper.ExecuteReader(sqlQuery);
//if(dataReader.Read())
//{
//fileContent=(byte[])dataReader[BaseFileTable.FieldFileContent];
//}
//dbHelper.Close();
//returnfileContent;}
publicstringUpload(stringfolderId,stringcategoryId)
{
this.FolderID=folderId;
stringreturnValue=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
{
stringstatusCode=string.Empty;
stringstatusMessage=string.Empty;
ServiceManager.Instance.FileService.UpdateFile(UserInfo,this.FileId,Path.GetFileName(this.pic.ImageLocation),FileUtil.GetFile(this.pic.ImageLocation),outstatusCode,outstatusMessage);
returnValue=this.FileId;
}
}
else
{
//复制文件到指定的目录里if(!this.pic.ImageLocation.Equals(BaseSystemInfo.StartupPath+this.FileId))
{
stringdestDirectory=BaseSystemInfo.StartupPath+"\\UploadFiles"+"\\"+folderId+"\\"+categoryId;
System.IO.Directory.CreateDirectory(destDirectory);
stringdestFileName=destDirectory+"\\"+Path.GetFileName(this.pic.ImageLocation);
System.IO.File.Copy(this.pic.ImageLocation,destFileName);
returnValue="\\UploadFiles"+"\\"+folderId+"\\"+categoryId+"\\"+Path.GetFileName(this.pic.ImageLocation);
}
}
//OleDbHelperdbHelper=newSqlHelper();
//dbHelper.Open();
//stringsequence=BaseSequenceManager.Instance.GetSequence(DbHelper,BaseFileTable.TableName);
//OleDbSQLBuildersqlBuilder=newOleDbSQLBuilder();
//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;}
returnreturnValue;
}
privatevoidSetControlState(boolenabled)
{
this.btnSelect.Enabled=enabled;
this.btnClear.Enabled=enabled;
this.btnDelete.Enabled=enabled;
}
#regionprivatevoidSetControlState()设置按钮状态
///<summary>///设置按钮状态
///</summary>privatevoidSetControlState()
{
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
privatevoidpic_DoubleClick(objectsender,EventArgse)
{
if(this.pic.Image!=null)
{
FrmPicturefrmPicture=newFrmPicture(this.FileId);
frmPicture.SetImage(this.pic.Image);
frmPicture.Show();
}
}
privatevoidbtnSelect_Click(objectsender,EventArgse)
{
OpenFileDialogopenFileDialog=newOpenFileDialog();
//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>privatevoidClearPicture()
{
this.pic.ImageLocation=string.Empty;
this.pic.Image=null;
}
privatevoidbtnClear_Click(objectsender,EventArgse)
{
this.ClearPicture();
this.ShowPicture();
//设置按钮状态this.SetControlState();
}
///<summary>///删除文件
///</summary>///<paramname="id">主键</param>///<returns>影响的行数</returns>publicintDeleteFile(stringid)
{
intreturnValue=0;
if(this.FromDatabase)
{
//从数据库服务器删除文件returnValue=ServiceManager.Instance.FileService.Delete(UserInfo,id);
}
else
{
//清除图片this.ClearPicture();
//删除文件System.IO.File.Delete(BaseSystemInfo.StartupPath+this.FileId);
}
returnreturnValue;
}
privatevoidbtnDelete_Click(objectsender,EventArgse)
{
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();
}
}
}
}

免责声明:文章转载自《C#.图片拖动处理(摘抄)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Assimp里的一些知识(1)MySQL清除表数据而保留表结构TRUNCATE TABLE下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Oracle中对列加密的方法

Oracle中对列加密的方法 2011-12-22 17:21:13 分类: Linux Oracle支持多种列加密方式: 1,透明数据加密(TDE):create table encrypt_col(id int,txt varchar2(100) encrypt using '算法名称' identified by '密钥' no salt);优...

android中的资源访问

一.android中的资源是在代码中使用的外部文件。图片,音频,动画和字符串等叫做android中的资源文件。 二.Android工程 资源类型布局表    与src源文件夹并列的两个文件夹assets和res用来保存资源文件。      1.assets文件夹中放原声文件如MP3文件,通过AssetManager类以二进制流的形式访问      2.re...

微信关注后自动回复,根据关键字回复

packageme.chunyu.apps.weixin.controller; importjava.io.IOException; importjava.net.URLEncoder; importjava.text.SimpleDateFormat; importjava.util.ArrayList; importjava.util.Ha...

大文件上传 之 改版了的SlickUpload.HttpUploadModule(Krystalware.SlickUpload.dll)

以下代码中所注释的部分是所改版的地方。:)Krystalware.SlickUpload.dll/Files/bigmouthz/SlickUpload.rar------------------------------------------------------using System;using System.Collections;using S...

Recordset属性与方法

Recordset属性与方法 关于分页:   首先,我们可以为 PageSize 属性设置一个值,从而指定从记录组中取出的构成一个页的行数;然后通过RecordCount 属性来确定记录的总数;再用记录总数除以 PageSize 就可得到所显示的页面总数;最后通过 AbsolutePage 属性就能完成对指定页的访问    ----------------...

Java基础知识15commonslang3 第3方开源库的具体使用01(StringUtils、RandomUtils类、RandomStringUtils类)

1.commons-lang3 概述 apache提供的众多commons工具包,号称Java第二API,而common里面lang3包更是被我们使用得最多的。因此本文主要详细讲解lang3包里面几乎每个类的使用,希望以后大家使用此工具包,写出优雅的代码。 maven依赖: <dependency> <groupId>org...