2013-01-11 6 views
1

끌어서 놓을 때 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); 
+0

이미지가 그림 상자에 맞게 축소되도록 하시겠습니까? –

+0

http://www.dotnetcurry.com/ShowArticle.aspx?ID=179 – MethodMan

+0

@glace 예, 그림 상자에 수축이 필요합니다. – Ladessa

답변

1

사용

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

대신

var dragImage = (Bitmap)picBox.Image; 
을 PictureBox를의 크기를 가지고 싶어

(나중에 임시 이미지에서 Dispose를 호출 할 수도 있지만 그렇지 않은 경우 GC가 처리합니다)

+0

많이 들려주세요! 그것은 작동합니다! – Ladessa

+0

이미지의 종횡비가 그림 상자의 종횡비와 일치하지 않으면 올바른 크기를 직접 계산해야합니다. jmrnet이 이미 답변을 게시 했으므로 내 답변에 추가하지 않겠습니다. 답변을 받아 들일 때까지 자유롭게 전환하십시오. – Zarat

0

그림 상자의 이미지가 전체 크기 이미지이기 때문입니다. 그림 상자는 표시 목적으로 만 크기를 조정하지만 Image 속성에는 원래 크기의 이미지가 있습니다.

따라서 이벤트 핸들러 MouseDown에서 이미지를 사용하기 전에 크기를 조정하려고합니다.

보다는 :

var dragImage = (Bitmap)picBox.Image; 

시도 :이 방법 같은

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

사용 무언가가 당신을 위해 이미지 크기를 조정하기 :

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; 
} 

* 이미지는 여기에서 코드의 크기를 조정 : http://www.codeproject.com/Articles/191424/Resizing-an-Image-On-The-Fly-using-NET