'How save image by using bound of stroke in WPF?
I made a paint application. It can create line, rectangle and can add annotation on image. All element is Stroke which be drawn in InkCanvas. This canvas will be expand to fit screen. But if I want to save image, I don't want to save full size of inkcanvas. But I want to save by using bounds of each stroke. (min left, min top, max right,max bottom)
Private Function GetMaxBound() As Windows.Rect
Dim maxLeft As Integer = 10000
Dim maxTop As Integer = 10000
Dim maxRight As Integer = 0
Dim maxBottom As Integer = 0
For Each s As Windows.Ink.Stroke In m_inkCanvas.Strokes
Dim bound As Windows.Rect = s.GetBounds
If bound.Left < maxLeft Then
maxLeft = bound.Left
End If
If bound.Top < maxTop Then
maxTop = bound.Top
End If
If bound.Right > maxRight Then
maxRight = bound.Right
End If
If bound.Bottom > maxBottom Then
maxBottom = bound.Bottom
End If
Next
Dim width As Integer = Math.Abs(maxRight - maxLeft)
Dim height As Integer = Math.Abs(maxTop - maxBottom)
Return New Windows.Rect(maxLeft, maxTop, width, height)
End Function
I try to use these function
Private Function SaveImage(ByVal targetPath As String) As Boolean
'Dim rect As Windows.Rect = GetMaxBound()
'Dim width As Double = rect.Width
'Dim height As Double = rect.Height
Dim width As Double = m_inkCanvas.ActualWidth
Dim height As Double = m_inkCanvas.ActualHeight
Dim bmpCopied As Windows.Media.Imaging.RenderTargetBitmap =
New Windows.Media.Imaging.RenderTargetBitmap(CInt(Math.Round(width)), CInt(Math.Round(height)), 96, 96, Windows.Media.PixelFormats.[Default])
Dim dv As Windows.Media.DrawingVisual = New Windows.Media.DrawingVisual()
Using dc As Windows.Media.DrawingContext = dv.RenderOpen()
Dim vb As Windows.Media.VisualBrush = New Windows.Media.VisualBrush(m_inkCanvas)
dc.DrawRectangle(vb, Nothing, New Windows.Rect(New System.Windows.Point(), New System.Windows.Size(width, height)))
End Using
bmpCopied.Render(dv)
Dim bitmap As System.Drawing.Bitmap
Using outStream As MemoryStream = New MemoryStream()
Dim enc As Windows.Media.Imaging.BitmapEncoder = New Windows.Media.Imaging.BmpBitmapEncoder()
enc.Frames.Add(Windows.Media.Imaging.BitmapFrame.Create(bmpCopied))
enc.Save(outStream)
bitmap = New System.Drawing.Bitmap(outStream)
End Using
Dim qualityParam As EncoderParameter = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L)
Dim jpegCodec As ImageCodecInfo = GetEncoderInfo("image/jpeg")
If jpegCodec Is Nothing Then
Return False
End If
Dim encoderParams As EncoderParameters = New EncoderParameters(1)
encoderParams.Param(0) = qualityParam
Dim btm As Bitmap = New Bitmap(bitmap)
bitmap.Dispose()
btm.Save(targetPath, jpegCodec, encoderParams)
btm.Dispose()
Return True
End Function
But this function save by inkcanvas size. How to save image by using min left and max right of strokes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


