Click here to Skip to main content
15,302,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to crop the image using WPF DrawContext.DrawGeometry.In this Project first select the cropping area using below code. In mouse left click got the current position and stored in static list. Once select the path then Enabling save button.

What I have tried:

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

Once I am clicking the save button, I am calling below code to crop the Image. In this code getting source image using Bitmap. Then creating RenderTargetBitmap using source width and height. Once it's done, then creating PathGeometry using points. After that cropping the image using DrawingContext.DrawGeometry.

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

After all thing cropping image different than selection image. Check below link image. Please suggest to avoid this problem.

Original
Selection
Crop
Posted
Updated 27-Oct-21 1:12am
v2
Comments
Gerry Schmitz 30-Oct-21 17:28pm
   
Use BitmapSource.CopyPixels and BitmapSource.Create (instead of the other stuff).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900