clipping an image region and saving as new image
-
Help wanted clipping an image region and saving as new image.... Can someone please help me. What I what to do is display a picture (image) and drawer a clip region using the mouse on a selected area on the image, this could be a retangle, square or a freehand shape. I can display the image and clipped region using WPF as below. But what I cannot work out is how to save the clipped region of the image as a new bitmap image. The same problem if I create a image using pathgeometry, I cannot work out how to save the cropped part of the image. If there is an alternative method instead of using WPF, please advice me. c# or vb I don't mind, I just need a solution. Thanks // Sample 4 // Create the image to clip. Image myImage = new Image(); Uri imageUri = new Uri(@"C:\\Documents and Settings\\All Users\\Documents\My Pictures\\Sample Pictures\\Water lilies.jpg", UriKind.Relative); myImage.Source = new BitmapImage(imageUri); myImage.Width = 200; myImage.Height = 150; myImage.HorizontalAlignment = HorizontalAlignment.Left; // Use an EllipseGeometry to define the clip region. EllipseGeometry myEllipseGeometry2 = new EllipseGeometry(); myEllipseGeometry2.Center = new Point(100, 75); myEllipseGeometry2.RadiusX = 100; myEllipseGeometry2.RadiusY = 75; myImage.Clip = myEllipseGeometry2;
-
Help wanted clipping an image region and saving as new image.... Can someone please help me. What I what to do is display a picture (image) and drawer a clip region using the mouse on a selected area on the image, this could be a retangle, square or a freehand shape. I can display the image and clipped region using WPF as below. But what I cannot work out is how to save the clipped region of the image as a new bitmap image. The same problem if I create a image using pathgeometry, I cannot work out how to save the cropped part of the image. If there is an alternative method instead of using WPF, please advice me. c# or vb I don't mind, I just need a solution. Thanks // Sample 4 // Create the image to clip. Image myImage = new Image(); Uri imageUri = new Uri(@"C:\\Documents and Settings\\All Users\\Documents\My Pictures\\Sample Pictures\\Water lilies.jpg", UriKind.Relative); myImage.Source = new BitmapImage(imageUri); myImage.Width = 200; myImage.Height = 150; myImage.HorizontalAlignment = HorizontalAlignment.Left; // Use an EllipseGeometry to define the clip region. EllipseGeometry myEllipseGeometry2 = new EllipseGeometry(); myEllipseGeometry2.Center = new Point(100, 75); myEllipseGeometry2.RadiusX = 100; myEllipseGeometry2.RadiusY = 75; myImage.Clip = myEllipseGeometry2;
RenderTargetBitmap[^] is the key here. Also check out this blog post by Rick Strahl which I found helpful: Rendering a WPF Container to Bitmap[^]