遗憾的是,TransformedBitmap
它不支持以 90 度的任何倍数以外的角度旋转(这意味着我们只能旋转 90、180、270,...)。考虑我们可以将哪些对象放入位图中并应用一些Transform
?好吧,我们有DrawingGroup
, DrawingVisual
,和 via 。ImageBrush
UIElement
DrawingContext
使用,我们必须通过 的属性DrawingGroup
放入ImageDrawing
并应用变换。然后我们要使用 a ,将属性设置为。Transform
DrawingGroup
DrawingImage
Drawing
DrawingGroup
使用DrawingVisual
,我们必须打开一个DrawingContext
,使用DrawImage
推一些变换后的方法。然后我们可能不得不RenderTargetBitmap
通过DrawingVisual
在Render
方法中传递来使用。
使用UIElement
(就像您关于使用控件作为媒介的想法Image
),我们必须在UIElement
. 所以Image
控制最适合这个。每个UIElement
都有一个Transform
属性,允许我们添加任何变换。最后,我们还必须RenderTargetBitmap
通过UIElement
在Render
方法中传递 a 来使用。
使用ImageBrush
,我们必须将ImageSource
属性设置为BitmapImage
。然后我们可以使用Transform
orRelativeTransform
属性来应用一些变换。之后我们必须使用DrawingImage
. 创建一个简单GeometryDrawing
的使用ImageBrush
作为它的画笔,和一个RectangleGeometry
作为它的几何。最后,我们只需要将其设置GeometryDrawing
为. 输出是. 我想用这种方法在这里写代码:Drawing
DrawingImage
DrawingImage
Dim InImage As New BitmapImage(New Uri("My Image Path"))
Dim ImgBrush As New ImageBrush(InImage)
ImgBrush.Viewport = New Rect(0.1,0.1,0.8,0.8)
ImgBrush.ViewportUnits = BrushMappingMode.RelativeToBoundingBox
Dim Rotating As New RotateTransform(190)
Rotating.CenterX = 0.5
Rotating.CenterY = 0.5
ImgBrush.RelativeTransform = Rotating
Dim ImgSize As New Rect(0,0,300,400)
Dim DrawImage As New DrawingImage()
DrawImage.Drawing = New GeometryDrawing(ImgBrush, null, ImgSize)
关于"My Image Path"
. 我发现Relative
如果您没有与构建的 exe 文件处于同一级别的任何 Image 文件夹,则使用图像将不起作用。如果您不想将某些图像文件夹与您的应用程序一起部署,您可以将图像添加为Resource。要在后面的代码中引用作为资源添加的图像,您必须使用一种特殊的路径:
pack://application:,,,/Your_Relative_Image_Path
请注意,要确保您的图像被添加为资源,请尝试右键单击图像(在 Projects 树视图下),在弹出菜单中选择Properties ,然后查看Build Action字段,它应该是Resource。
还要注意ImgBrush.Viewport
, 适当地设置它会防止图像被切断(那是因为转换后的图像被旋转了)。这取决于ImgSize
您旋转的度数和度数。