C#-向窗体中拖放图片并显示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;//添加的命名空间,对文件进行操作
using System.Threading;//线程序的命名空间

namespace DragImageToForm
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        public static bool Var_Style = true;
        public static string tempstr="";

        /// <summary>
        /// 在窗体背景中显示被拖放的图片
        /// </summary>
        /// <param Frm="Form">窗体</param>
        /// <param e="DragEventArgs">DragDrop、DragEnter 或 DragOver 事件提供数据</param>
        public void SetDragImageToFrm(Form Frm, DragEventArgs e)
        {
            if (Var_Style == true)
            {
                e.Effect = DragDropEffects.Copy;//设置拖放操作中目标放置类型为复制
                String[] str_Drop = (String[])e.Data.GetData(DataFormats.FileDrop, true);//检索数据格式相关联的数据
                string tempstr;
                Bitmap bkImage;//定义Bitmap变量
                tempstr = str_Drop[0];//获取拖放文件的目录
                try
                {
                    bkImage = new Bitmap(tempstr);//存储拖放的图片
                    //根据图片设置窗体的大小
                    Frm.Size = new System.Drawing.Size(bkImage.Width + 6, bkImage.Height + 33);
                    Frm.BackgroundImage = bkImage;//在窗体背景中显示图片
                }
                catch { }
            }
        }

        private void Frm_Main_DragEnter(object sender, DragEventArgs e)
        {
            SetDragImageToFrm(this, e);//在窗体中显示拖放到窗体上的图片
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值