简体   繁体   中英

Merge an ImageBrush and a SolidColorBrush ? (as a Background of a Canvas)

I have an ImageBrush and a SolidColorBrush. The thing is that I, now, want to set my canvas background with these properties.

I can easily set my background to either my ImageBrush or my SolidColorBrush but can I do a sort of merge ? The thing is that my ImageBrush.Stretch is set to Uniform so I can see the background color behind ! (It wouldn't be the case with "Fill")

Now, Brush is an abstract class so the solution I image is to extend it but that really is a lot of work for a thing that sounds simple and common to me...

Isn't there any other solution ?

What about this, although it has the drawback that the image's absolute size and, more important, the aspect ratio is ignored. But it may give you a hint.

<Canvas>
    <Canvas.Background>
        <DrawingBrush>
            <DrawingBrush.Drawing>
                <DrawingGroup>
                    <GeometryDrawing Brush="Orange">
                        <GeometryDrawing.Geometry>
                            <RectangleGeometry Rect="0,0,1,1"/>
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                    <ImageDrawing ImageSource="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg" Rect="0.2,0.2,0.6,0.6" />
                </DrawingGroup>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Canvas.Background>
</Canvas>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM