拖放移动图片框-拖动时图像的大小 [英] Drag and Drop move PictureBox- size of image when I'm dragging

查看:33
本文介绍了拖放移动图片框-拖动时图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一种在拖放时移动图片框的方法.但是当我拖动图片框时,图像具有图像的实际大小,我希望图像具有图片框的大小

I have created a method which move the PictureBox when I Drag and Drop. But when I'm dragging the PictureBox, the image has the real size of image and I wanna that the image has the size of PictureBox

 private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            picBox = (PictureBox)sender;
            var dragImage = (Bitmap)picBox.Image;
            IntPtr icon = dragImage.GetHicon();
            Cursor.Current = new Cursor(icon);
            DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
            DestroyIcon(icon);
        }
    }

protected override void OnGiveFeedback(GiveFeedbackEventArgs e)
    {
        e.UseDefaultCursors = false;
    }
    protected override void OnDragEnter(DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(Bitmap))) e.Effect = DragDropEffects.Copy;
    }
    protected override void OnDragDrop(DragEventArgs e)
    {

        picBox.Location = this.PointToClient(new Point(e.X - picBox.Width / 2, e.Y - picBox.Height / 2));
    }

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    extern static bool DestroyIcon(IntPtr handle);

推荐答案

使用

var dragImage = new Bitmap((Bitmap)picBox.Image, picBox.Size);

代替

var dragImage = (Bitmap)picBox.Image;

(也许稍后会在临时映像上调用 Dispose,但如果您不这样做,GC 会处理它)

(and maybe call Dispose on the temporary image later, but the GC will deal with it if you don't)

这篇关于拖放移动图片框-拖动时图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆