使用RenderTargetBitmap和DrawingVisual [英] Using RenderTargetBitmap and DrawingVisual
问题描述
这儿去了:
我想使用ImageBrush在Canvas上设置背景图像.当我想调整画布大小并保持背景图片完好无损时,麻烦就开始了.这意味着它将在左上角保持相同的大小.
这是我尝试过的:
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?
推荐答案
我更改了代码以使其正常工作.我生成了自己的图片类:
I changed the code to make it work. I generated an own picture class:
Public Class PictureElement
Inherits FrameworkElement
Private _children As VisualCollection
Public Sub New(p_position As Point, ByVal Url As String)
'You have to set the position before creating the Visual collection
'Other wise the object will be placed on point (0,0)
PositionOnCanvas = p_position
_children = New VisualCollection(Me)
_children.Add(CreateDrawingVisualImage(Url))
End Sub
Private Function CreateDrawingVisualImage(ByVal img As String) As DrawingVisual
Dim imgConv = New ImageSourceConverter()
Dim imageSource As ImageSource
imageSource = DirectCast(imgConv.ConvertFromString(img), ImageSource)
Dim imgHeight = imageSource.Height
Dim imgWidth = imageSource.Width
' Create a circle and draw it in the DrawingContext.
Dim drawingVisual As New DrawingVisual()
Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
drawingContext.DrawImage(imageSource, New Rect(0, 0, imgWidth, imgHeight))
drawingContext.Close()
' Persist the drawing content.
' drawingContext.Close()
Return drawingVisual
End Function
......
不过,我很想知道如何使用我的原始方法来做....
Would love to know how to do it with my original method though....
这篇关于使用RenderTargetBitmap和DrawingVisual的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!