简体   繁体   English

拖放时拖动PictureBox-图片的大小

[英]Drag and Drop move PictureBox- size of image when I'm dragging

I have created a method which move the PictureBox when I Drag and Drop. 我创建了一个在拖放时移动PictureBox的方法。 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 但是,当我拖动PictureBox时,图像具有图像的实际大小,我想图像具有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);

Use 采用

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

instead of 代替

var dragImage = (Bitmap)picBox.Image;

(and maybe call Dispose on the temporary image later, but the GC will deal with it if you don't) (也许稍后再调用该临时映像上的Dispose,但如果您不这样做,则GC会对其进行处理)

This is because the image in your picture box is the full size image. 这是因为图片框中的图像是完整尺寸的图像。 The picture box just scales it for display purposes but the Image property has the original-sized image. 图片框只是为了显示目的对其进行缩放,但是Image属性具有原始尺寸的图像。

So, in your MouseDown event handler you want to resize the image before using it. 因此,在MouseDown事件处理程序中,您想在使用前调整图像的大小。

Rather than: 而不是:

var dragImage = (Bitmap)picBox.Image;

Try: 尝试:

 var dragImage = ResizeImage(picBox.Image, new Size(picBox.Width, PicBox.Height));

Use something like this method to resize the image for you: 使用类似这种方法来为您调整图像大小:

public static Image ResizeImage(Image image, Size size, 
    bool preserveAspectRatio = true)
{
    int newWidth;
    int newHeight;
    if (preserveAspectRatio)
    {
        int originalWidth = image.Width;
        int originalHeight = image.Height;
        float percentWidth = (float)size.Width / (float)originalWidth;
        float percentHeight = (float)size.Height / (float)originalHeight;
        float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
        newWidth = (int)(originalWidth * percent);
        newHeight = (int)(originalHeight * percent);
    }
    else
    {
        newWidth = size.Width;
        newHeight = size.Height;
    }
    Image newImage = new Bitmap(newWidth, newHeight);
    using (Graphics graphicsHandle = Graphics.FromImage(newImage))
    {
        graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
    }
    return newImage;
}

* Image resize code from here: http://www.codeproject.com/Articles/191424/Resizing-an-Image-On-The-Fly-using-NET * 图片大小调整代码,可从此处获取: http//www.codeproject.com/Articles/191424/Resizing-an-Image-On-The-Fly-using-NET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM