2012-04-25 5 views
0

ピクチャエディットボックスのサイズよりも大きな画像があります。マウスで四角形を作成し、画像上の正しいポイントが選択されていない場合です。それはPictureEditの点を画像ではなく、それを選択しているようです。私は画像をトリミングし、切り抜いた画像を新しいPictureeditに配置しようとしています。以下のコード例。切り抜きピクチャの切り抜きの問題

private void originalPictureEdit_MouseDown(object sender, MouseEventArgs e) 
{ 
    //Point TextStartLocation = e.Location; 
    Cursor = Cursors.IBeam; 

    if (_drawStarted == false) 
    { 
     _drawStarted = true; 

     if (e.Button == System.Windows.Forms.MouseButtons.Left) 
     { 
      Cursor = Cursors.Cross; 

      _cropX = e.X; 
      _cropY = e.Y; 
      _selection = new Rectangle(new Point(e.X, e.Y), new Size()); 
      _cropPen = new Pen(Color.Black, 1); 
      _cropPen.DashStyle = DashStyle.DashDotDot; 
     } 
    } 

    originalPictureEdit.Refresh(); 
} 

private void originalPictureEdit_MouseMove(object sender, MouseEventArgs e) 
{ 

    if (originalPictureEdit.Image == null) 
     return; 

    if (e.Button == System.Windows.Forms.MouseButtons.Left) 
    { 
     originalPictureEdit.Refresh(); 
     _selection.Width = e.X - _selection.X; 
     _selection.Height = e.Y - _selection.Y; 
     originalPictureEdit.CreateGraphics().DrawRectangle(_cropPen, _cropX, _cropY, _selection.Width, _selection.Height); 

    }   
} 

private void originalPictureEdit_MouseUp(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left && _drawStarted && _selection.Size != new Size()) 
    { 
     _croppedImage = (_displayedImage as Bitmap).Clone(_selection, _displayedImage.PixelFormat); 

     modifiedPictureEdit.Image = _croppedImage; 
     modifiedPictureEdit.Width = _croppedImage.Width; 
     modifiedPictureEdit.Height = _croppedImage.Height; 
    } 

    Cursor = Cursors.Default; 

    _drawStarted = false; 
} 

答えて

1

私はスクロールバーの位置を見つける必要がありました。私は、次のアプローチがあなたを助けて信じていますMouseUpの作業コードは次のとおりです。

private void pictureEdit1_MouseUp(object sender, MouseEventArgs e) 
{ 
     if (e.Button == MouseButtons.Left && _drawStarted && _selection.Size != new Size()) 
     { 
      PictureEditViewInfo viewInfo = originalPictureEdit.GetViewInfo() as PictureEditViewInfo; 
      PropertyInfo pr = viewInfo.GetType().GetProperty("HScrollBarPosition", BindingFlags.Instance | BindingFlags.NonPublic); 
      int fHScrollBarPosition = (int)pr.GetValue(viewInfo, null); 
      pr = viewInfo.GetType().GetProperty("VScrollBarPosition", BindingFlags.Instance | BindingFlags.NonPublic); 
      int fVScrollBarPosition = (int)pr.GetValue(viewInfo, null); 
      _selection.X += fHScrollBarPosition; 
      _selection.Y += fVScrollBarPosition; 

      Crop(); 
     } 

     Cursor = Cursors.Default; 

     _drawStarted = false; 
} 
0

PictureEdit制御は、オフセットやズームで画像を表示することができますので、あなたの計算でこれらの要素を受け入れる必要があります。

PictureEditViewInfo viewInfo = GetViewInfo(originalPictureEdit); 
Rectangle cropRect = new Rectangle(
    _selection.X - viewInfo.PictureStartX, 
    _selection.Y - viewInfo.PictureStartY, 
    _selection.Width, 
    _selection.Height); 
_croppedImage = (originalPictureEdit.Image as Bitmap).Clone(cropRect, 
    originalPictureEdit.Image.PixelFormat); 
//... 
static PictureEditViewInfo GetViewInfo(PictureEdit edit) { 
    PropertyInfo pInfo = typeof(BaseEdit).GetProperty("ViewInfo", 
     BindingFlags.Instance | BindingFlags.NonPublic); 
    return (PictureEditViewInfo)pInfo.GetValue(edit, new object[] { }); 
} 

また、私はあなたが以下でpictureeditのgraphichに直接描画を交換をお勧めします:

//... 
Point frameLocation = originalPictureEdit.PointToScreen(_selection.Location); 
ControlPaint.DrawReversibleFrame(
    new Rectangle(frameLocation, _selection.Size), Color.Black, FrameStyle.Dashed); 
関連する問題