使用Drawcontext进行图像裁剪

时间:2018-06-19 10:45:22

标签: c# wpf image-processing

我正在尝试使用WPF DrawContext.DrawGeometry裁剪图像。在此项目中,首先使用下面的代码选择裁剪区域。在鼠标左键单击获取当前位置并存储在静态列表中。选择路径后,再启用保存按钮。

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        { 
              System.Windows.Point currentPoint = e.GetPosition(this);
                    if (CurrentGeometryGroup == null)
                    {
                        CurrentGeometryGroup = new List<System.Windows.Point>();
                    }
                    CurrentGeometryGroup.Add(currentPoint);
                    if (CurrentGeometry == null)
                    {
                        _startPoint = currentPoint;
                        _lastPoint = currentPoint;
                        CurrentGeometry = new GeometryGroup();
                        CurrentGeometry.Children.Add(new EllipseGeometry(currentPoint, 3, 3));
                        GetPixel(currentPoint, _lastPoint);
                    }
                    else
                    {
                        if (AreClose(_startPoint, currentPoint))
                        {
                            CurrentGeometry.Children.Add(new LineGeometry(_lastPoint, _startPoint));
                            CurrentGeometry.Freeze();
                            CurrentGeometry = null;
                            GetPixel(currentPoint, _lastPoint);
                            System.Windows.Point Defaly = currentPoint;
                            RaiseEvent(new RoutedEventArgs(this.CropImageEvent, this));
                        }
                        else
                        {
                            CurrentGeometry.Children.Add(new LineGeometry(_lastPoint, currentPoint));
                            CurrentGeometry.Children.Add(new EllipseGeometry(currentPoint, 3, 3));
                            GetPixel(currentPoint, _lastPoint);
                            _lastPoint = currentPoint;
                        }
                    }
          }

点击保存按钮后,我在代码下面调用以裁剪图像。在此代码中使用Bitmap获取源图像。然后使用源宽度和高度创建RenderTargetBitmap。一旦完成,然后使用点创建PathGeometry。之后使用DrawingContext.DrawGeometry裁剪图像。

                using (System.Drawing.Bitmap source = new System.Drawing.Bitmap(ImgUrl))
                {
                    RenderTargetBitmap bmp = new RenderTargetBitmap(source.Width, source.Height,
                                source.HorizontalResolution,
                                source.VerticalResolution,
                                PixelFormats.Pbgra32);
                    float hScale = 1.0f / (float)zoomFactor;
                    PathFigure pathFigure = new PathFigure();
                    pathFigure.StartPoint = new System.Windows.Point(CurrentGeometryGroup[0].X / zoomFactor, CurrentGeometryGroup[0].Y / zoomFactor);
                    for (int x = 1; x < CurrentGeometryGroup.Count; x++)
                    {
                        LineSegment lineSegment = new LineSegment();
                        lineSegment.Point = new System.Windows.Point(CurrentGeometryGroup[x].X / zoomFactor, CurrentGeometryGroup[x].Y / zoomFactor);
                        pathFigure.Segments.Add(lineSegment);
                    }
                    pathFigure.IsClosed = true;
                    PathGeometry pathGeometry = new PathGeometry();
                    pathGeometry.Figures.Add(pathFigure);
                    DrawingVisual visual = new DrawingVisual();
                    using (DrawingContext context = visual.RenderOpen())
                    {

                        ImageBrush imgBrush = new ImageBrush();
                        imgBrush.ImageSource = new BitmapImage(new Uri(ImageUrl, UriKind.RelativeOrAbsolute));
                        imgBrush.Stretch = Stretch.None;
                        imgBrush.Freeze();
                        context.DrawGeometry(imgBrush, new System.Windows.Media.Pen(System.Windows.Media.Brushes.White, 0), pathGeometry);
                    }
                    bmp.Render(visual);
                    MemoryStream stream = new MemoryStream();
                    BitmapEncoder encoder = new BmpBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bmp));
                    encoder.Save(stream);
                    Bitmap bitmap = new Bitmap(stream);
                    tempFileName = System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) + @"\assets\";
                    if (fixedTempIdx > 2)
                        fixedTempIdx = 0;
                    else
                        ++fixedTempIdx;
                    CleanUp(tempFileName, fixedTempName, fixedTempIdx);
                    tempFileName = tempFileName + fixedTempName + fixedTempIdx.ToString() + ".jpg";
                    bitmap.Save(tempFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                    cmDragCanvas.RemoveHandler(MenuItem.ClickEvent, cmDragCanvasRoutedEventHandler);
                    dragCanvasForImg.ContextMenu = null;
                    cmDragCanvas = null;
                    BitmapImage bmpPopup = new BitmapImage(new Uri(tempFileName));
                    popUpImage.Source = bmpPopup;
                    popUpImage.Height = 200;
                    popUpImage.Width = 200;
                    grdCroppedImage.Visibility = Visibility.Visible;
                }

毕竟裁剪图像与选择图像不同。检查下面的链接图像。请建议避免这个问题。

Orginal

Selection

Crop Image

0 个答案:

没有答案