What is this transparent color glass
Simply a Glass which shows a black and white image in color.The application here contains a black and white image along with a elliptical glass on it.We can see the same image in color through the glass and the glass is moving with mouse.
Basic idea of creating transparent glass
Of course there are 2 images one is color and another is black and white image of the same.In the application basically there are 3 layers inside a Canvas.One layer is again a Canvas and rest are Images.Bindings are used to make sure that all are in same size.
The canvas is at bottom ,with an ellipse inside.The second layer is the black and white image.At the top we have placed the actual color image and its opacity mask is set to make it viewable only through an elliptical portion.
The parent canvas is enabled with FollowMouseAction.It is a trigger action which enables the elements in Canvas to follow the mouse.The FollowMouseAction is bound to the MouseMove trigger.Hence it updates it’s position on mouse move.
We have to create a VisualBrush of the Canvas which is at the bottom layer to apply as OpacityMask of top layer image.Keep this as Resource.Set Stretch property of VisualBrush to None.Then set the OpacityMask of the top image to this resource.
Yes its done.Now run the project and see the glass in action.
Below are the step by step instructions to create this application.Later I will be writing about how to create color glass using Expression Blend 3
- Create a WPF application.
- Add 2 photos into application.One is the original which is in color and another is ,black and white version of the same.
- Add a Canvas to the Grid in Window and name it as ‘rootcanvas’ .Set its Height and Width.
- Add another Canvas in to rootcanvas and name it as ‘childcanvas’.Bind it’s Width and height to ActualHeight and ActualWidth of rootcanvas
- Set the background property of childcanvas to any color with alpha value of 00.Eg :#00ffffff
- In the child canvas add an Ellipse named ‘ellipse’.Set it’s color to Red.Set Height and Width.
- Add 2 images into parentcanvas.Bind Width and Height in to ActualWidth and ActualHeight of rootcanvas.
- Set black and white photo to the first image(middle layer)and color photo to the top most layer.
- Create a Visual brush of childcanvas and store as Resource named ‘CanvasVisualBrush’.Set its stretch to None.
- Apply ‘CanvasVisualBrush’ as OpacityMask of Image which is in top layer.(which has the color photo)
- Add Reference to the dll MyActionsLibrary.(See my previous post to get MyActionsLibrary dll)
- Add FollowMouseBehavior into the parentcanvas.Set it’s Event to MouseMove.
- Set the Element property of FollowMouseAction to ‘ellipse’.
- Run and enjoy.