Click here to Skip to main content
Licence CPOL
First Posted 24 Jun 2010
Views 21,784
Downloads 1,903
Bookmarked 42 times

Shaped WPF Form

By | 24 Jun 2010 | Article
Shaping WPF Form using an Image

Introduction

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.

Background

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.

Prerequisites

  1. .NET FrameWork 3.0 or above
  2. Visual Studio 2008 or above
  3. Adobe Photoshop or an image editing tool
  4. An image file that you are going to use
  5. Knowledge of XAML

Preparing the Image File

  1. Select image file that you wish to use as Form shape.
  2. 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 :)).
  3. Open the image file in Adobe Photoshop.
  4. Select the entire image excluding the background.
  5. Copy the selection.
  6. Click on menu File>>New. This will show a dialog box. In the Content section of the dialog box, select Transparent and click ok.
  7. A transparent background canvas will open up, paste your image there and save it as PNG file.

Selection.png

Now our image file is ready, let's shape our WPF Form now. :)

Setting up WPF Application

  1. Open Visual Studio 2008 and create new project.
  2. Under Visual C# project templates, select Windows>>WPF Application.
  3. Use the following XAML:
<window title="ShapedWindow" windowstartuplocation="CenterScreen" 
    allowstransparency="True" opacitymask="White" windowstyle="None" 
    background="Transparent" width="620" height="267" 
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml 
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation 
    x:class="ShapedWPFForm.ShapedWindow">
    <grid>
        <img name="imgBackground" source="Shape.png" stretch="Fill" />
    </grid>
</window>

And that's it, it's done. :)

Let me explain the XAML:

  1. allowstransparency="True" - This property makes the Form transparent.
  2. background="Transparent" - This property specifies that the background color should be transparent.
  3. windowstyle="None" - This property removes the title bar and makes the form borderless. Basically removes all the system controls from the form.
  4. 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!

History

  • 24th June, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Amit Kumar Tiwari

Software Developer (Senior)

India India

Member

I am a Software Developer with 5 years experience in Microsoft Technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey23:50 4 Apr '12  
QuestionIs it possible to change the Shape.PNG to Shape1.PNG PinmemberMember 832682111:05 6 Dec '11  
AnswerRe: Is it possible to change the Shape.PNG to Shape1.PNG Pinmemberemarti12:53 28 Dec '11  
GeneralMy vote of 5 Pinmembercsharpbd20:30 16 Nov '11  
GeneralMy vote of 5 Pinmemberayad0net11:17 9 Aug '11  
GeneralGood trick man... PinmvpAbhishek Sur22:27 30 Jun '10  
GeneralRe: Good trick man... PinmemberAmit Kumar Tiwari17:35 5 Jul '10  
GeneralVery practical PinmemberRabinDl10:37 24 Jun '10  
GeneralRe: Very practical PinmemberAmit Kumar Tiwari17:34 5 Jul '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 24 Jun 2010
Article Copyright 2010 by Amit Kumar Tiwari
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid