private void GetCutImage() { int tempLeft = (int)Canvas.GetLeft(rectangle); int tempTop = (int)Canvas.GetTop(rectangle); int tempWidth = (int)rectangle.Width; int tempHeight = (int)rectangle.Height; System.Drawing.Image currentImage; System.Drawing.Image processImage; //currentImage = System.Drawing.Image.FromFile(@"D:\公司项目\测试的代码\CutImageTest\CutImageTest\10.jpg"); currentImage = ImageWork.ImageSourceToBitmap(sourceImg.Source);//获取原图片 System.Drawing.Rectangle SelectedRect = new Rectangle(tempLeft, tempTop, tempWidth, tempHeight); System.Drawing.Rectangle _selectedRect = MapSelectedRegion(sourceImg, SelectedRect, currentImage); //System.Drawing.Rectangle _selectedRect = new System.Drawing.Rectangle(tempLeft, tempTop, tempWidth, tempHeight); processImage = ImageWork.ClipImage(currentImage, _selectedRect, (int)targetImg.Width, (int)targetImg.Height, System.Drawing.Color.Black ); Bitmap bmp = new Bitmap(processImage); // get bitmap BitmapImage bmpResource = ImageWork.BitmapToBitmapImage(bmp); targetImg.Source = bmpResource; }
/// <summary> /// 计算选择区域的缩放比例 /// </summary> /// <param name="MapPbx"></param> /// <param name="MapRect"></param> /// <param name="SourceImage"></param> /// <returns></returns> private System.Drawing.Rectangle MapSelectedRegion(FrameworkElement MapPbx, System.Drawing.Rectangle MapRect, System.Drawing.Image SourceImage) { #region Determine the originally used mapping offsets and dimensions int targetMapOffsetX = 0; int targetMapOffsetY = 0; int targetMapWidth = 0; int targetMapHeight = 0; float dummyMapRatio = 0f; int tempWidth = (int)MapPbx.ActualWidth; int tempHeight = (int)MapPbx.ActualHeight; ImageWork.FixedSize_ResizeRatios(SourceImage.Width, SourceImage.Height, tempWidth, tempHeight, ref targetMapOffsetX, ref targetMapOffsetY, ref targetMapWidth, ref targetMapHeight, ref dummyMapRatio); #endregion #region Compute mapped .Left and .Width double dMapPbxWidth = (double)targetMapWidth; double xLeft = (double)MapRect.Left; double xWidth = (double)MapRect.Width; if (targetMapOffsetX > 0) { double dOffsetX = (double)targetMapOffsetX; double dOffsetWidth = (double)targetMapWidth; if (xLeft < dOffsetX) { xLeft = 0; } else { xLeft -= dOffsetX; } double xRight = (double)MapRect.Right - dOffsetX; if (xRight > dOffsetWidth) { xRight = dOffsetWidth; } xWidth = xRight - xLeft; if (xWidth > dMapPbxWidth) { xWidth = dMapPbxWidth; } } int _mappedRect_Left = (int)((double)SourceImage.Width * (xLeft / dMapPbxWidth)); int _mappedRect_Width = (int)((double)SourceImage.Width * (xWidth / dMapPbxWidth)); #endregion #region Compute mapped .Top and .Height double dMapPbxHeight = (double)targetMapHeight; double yTop = (double)MapRect.Top; double yHeight = (double)MapRect.Height; if (targetMapOffsetY > 0) { double dOffsetY = (double)targetMapOffsetY; double dOffsetHeight = (double)targetMapHeight; if (yTop < dOffsetY) { yTop = 0; } else { yTop -= dOffsetY; } double yBottom = (double)MapRect.Bottom - dOffsetY; if (yBottom > dOffsetHeight) { yBottom = dOffsetHeight; } yHeight = yBottom - yTop; if (yHeight > dMapPbxHeight) { yHeight = dMapPbxHeight; } } int _mappedRect_Top = (int)((double)SourceImage.Height * (yTop / dMapPbxHeight)); int _mappedRect_Height = (int)((double)SourceImage.Height * (yHeight / dMapPbxHeight)); #endregion return(new System.Drawing.Rectangle(_mappedRect_Left, _mappedRect_Top, _mappedRect_Width, _mappedRect_Height)); }