Here goes:
I want to set a background image on a Canvas, using ImageBrush. The trouble starts when I want to resize the Canvas and leave the background picture intact. Meaning it will stay the same size in the topleft corner.
This is what I tried:
Private Function GenerateImageBrush(ByVal image As ImageSource, Optional ByVal SizeNew As Boolean = False) As ImageBrush
Dim result As New ImageBrush
Dim drawingVisual As New DrawingVisual()
Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
drawingContext.DrawRectangle(Brushes.White, New Pen(Brushes.Black, 1), New Rect(New Size(Me.Height, Me.Width)))
drawingContext.DrawImage(image, New Rect(0, 0, imgWidth, imgHeight))
drawingContext.Close()
Dim bmp As New RenderTargetBitmap(Me.Height, Me.Width, 96, 96, PixelFormats.Pbgra32)
bmp.Render(drawingVisual)
result.ImageSource = bmp
Return result
End Function
Dim imgWidth, imgHeight as Double
Public Sub AddPicture(ByVal pic As String)
Dim imgConv = New ImageSourceConverter()
Dim imageSource As ImageSource
imageSource = DirectCast(imgConv.ConvertFromString(pic), ImageSource)
imgWidth = imageSource.Width
imgHeight = imageSource.Height
Me.Width = imageSource.Width
Me.Height = imageSource.Height
Me.Background = GenerateImageBrush(imageSource)
End Sub
The idea is to return an Image that has preserved its original size, and filled the rest of the space with white color.
But When I try to resize the canvas, the picture starts to deform. It seems to want to preserve the original ratio Length/Width and adusts the image accordingly. But I cant seem to find out how to do this, does anybody know how to get this to work?