Search code examples
c#wpfimage-processinginkcanvas

Crop stroked region as Bitmap from InkCanvas Container


I am just started working in WPF application. Here, I need to get the signature from user. I am using Inkcanvas control to implement this feature.

I am curious on knowing is there a way in Inkcanvas control to crop the stroked region alone with some outer margin. I don't want the empty space of the Inkcanvas container in the output bitmap image. if the signature in too small, I want to crop the region and stretch that into particular size(300x200).


Solution

  • OK, first you need to determine the rectangle to crop (the "stroked region"), then save it to an image.

    InkCanvas has a Strokes property that is the collection of ink strokes, you can get the bounds of each Stroke by calling Stroke.GetBound method. Then you can get the Left property of the "stroked region", which is the Left property of the leftmost bounds. And you also get the Right, Top and Bottom in the same way. I hope you can understand my explanation.

    Resizing the cropped image to 300x200 should be quite easy, you can find plenty of answers on Stackoverflow.