Hello WPF lovers. With the invent of unified API language such as WPF, it has become very easy to enrich user interface. Creating rich user interface is just a thought away. All you need to have is a creative mind and latest technological blend. WPF and Expression Blend are very useful in making rich UI applications, crispy graphics and pretty good animation.
What I am going to post is about shaping a window rectangular form to the shape of an image. It can be any image, any shape, any size just that it should be PNG (Portable Network Graphics) image with no background (white background removed).
Using the Code
Well, achieving this is a piece of cake but still I will provide a complete walkthrough process.
- .NET FrameWork 3.0 or above
- Visual Studio 2008 or above
- Adobe Photoshop or an image editing tool
- An image file that you are going to use
- Knowledge of XAML
Preparing the Image File
- Select image file that you wish to use as Form shape.
- Making the image background free is a pretty easy process in most image editing tools, however, I will explain it for Adobe Photoshop (as I love that product :)).
- Open the image file in Adobe Photoshop.
- Select the entire image excluding the background.
- Copy the selection.
- Click on menu File>>New. This will show a dialog box. In the Content section of the dialog box, select Transparent and click ok.
- A transparent background canvas will open up, paste your image there and save it as PNG file.
Now our image file is ready, let's shape our WPF Form now. :)
Setting up WPF Application
- Open Visual Studio 2008 and create new project.
- Under Visual C# project templates, select Windows>>WPF Application.
- Use the following XAML:
<window title="ShapedWindow" windowstartuplocation="CenterScreen"
allowstransparency="True" opacitymask="White" windowstyle="None"
background="Transparent" width="620" height="267"
<img name="imgBackground" source="Shape.png" stretch="Fill" />
And that's it, it's done. :)
Let me explain the XAML:
allowstransparency="True" - This property makes the Form transparent.
background="Transparent" - This property specifies that the background color should be transparent.
windowstyle="None" - This property removes the title bar and makes the form borderless. Basically removes all the system controls from the form.
opacitymask="White" - This property makes the specified background color invisible from the form.
Done, tutorial is complete. By doing this, you can have an image as form shape. Give creative shape to your forms. Happy designing!
- 24th June, 2010: Initial post