Embedding Bitmaps inside XAML

October 8, 2007

I needed a type of BitmapSource that allows me to embed an image inside a XAML file. WPF has a number of derived BitmapSource classes, each one for its specialized task, one specific, BitmapImage, allows you to load images using XAML, but BitmapImage refer to external images in XAML files and does not embed images in a XAML file.

So I decided to create my own class to achieve the task.

What did I do?

  1. A BitmapSource derived class that expose a property with the bitmap info and can act as a reference to another BitmapSource.
  2. A container struct, BitmapInfo, to hold all the information needed to reconstruct the image, pixel array, size, format, etc.
  3. A TypeConverter that convert BitmapInfo to a serialized byte64 string and vice versa.

Using the EmbeddedBitmapSource is seamless as using any other derived BitmapSource class. For example:

Image myImage = new Image();

BitmapImage bmpImage = new BitmapImage( new Uri( “someimage.jpg” ) );

myImage.Source = new EmbeddedBitmapSource( bmpImage );

I included the sources with a sample application that let you save the image into a XAML file (the XAML file can be found after saving where the assembly is) and later to load it.

EmbeddedSample

On the upper right side is the original image and on the lower part of the window is the embedded image that was loaded from the XAML file.

Source code (VS2008 project) can be downloaded from here (Remember to change the .DOC extension to .ZIP)

By the way, I took the picture some year ago in Chile near Puerto Tranquilo. In Lake General Carrera are marble caves formations, called the Capilla de marmol and Catedral de marmol that were carved by water over time. One of those from a specific angle a dog’s head can be seen.


Huge News!! Microsoft is releasing part of the .NET Framework source code

October 3, 2007

Huge news!! Microsoft is releasing the Source Code for the .NET Framework Libraries, which includes ASP.NET, Windows Forms, WPF and much more.


RadioButton IsChecked Binding Problem – Conclusions

October 2, 2007

After I posted my previous post on this subject, I done some research on this issue and I found the following:

  1. The binding between the IsChecked property and a property in the “code behind” works in only if the binding is declared inside a Control Template, see code below.
  2. When binding between xaml elements the problem does not occur.

<UserControl

    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation

    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

    x:Class=RadioButtonTest.TemplatedControl

    Height=300 Width=300 Template={DynamicResource UserControlControlTemplate1}>

  <UserControl.Resources>

    <BooleanToVisibilityConverter x:Key=BooleanToVisibilityConverter/>

    <ControlTemplate x:Key=UserControlControlTemplate1 TargetType={x:Type UserControl}>

      <Grid>

        <StackPanel VerticalAlignment=Top>

          <StackPanel VerticalAlignment=Top>

 

            <RadioButton

                Height=16

                HorizontalAlignment=Left

                Name=isEllipse

 

                IsChecked={Binding Path=IsEllipse,

                                        RelativeSource={RelativeSource TemplatedParent},

                                        Mode=TwoWay,

                                        UpdateSourceTrigger=PropertyChanged}

                Content=Ellipse/>

 

            <RadioButton

                Height=16

                HorizontalAlignment=Left

                Name=isRectangle

                Content=Rectangle/>

 

          </StackPanel>

          <TextBlock Text=Binding Visibilty to Radio Button IsChecked />

          <TextBlock Text=property with BooleanToVisibilityConverter:/>

          <Ellipse x:Name=EllipseTip Fill=#FF000000

                  Width=30 Height=30  StrokeThickness=1

                  Visibility={Binding Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=isEllipse, Mode=Default}/>

          <Rectangle x:Name=RectangleTip Fill=#FF000000

                    Width=30 Height=30 StrokeThickness=1

                      Visibility={Binding Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=isRectangle, Mode=Default}/>

          <TextBlock Text=Binding to SelectionText Property />

          <TextBlock Text=and IsEnabled to rectangle radio button:/>

          <TextBox Text={Binding Path=SelectionText, RelativeSource={RelativeSource TemplatedParent}, Mode=Default}

          IsEnabled={Binding Path=IsChecked, ElementName=isRectangle, Mode=Default}/>

        </StackPanel>

      </Grid>

    </ControlTemplate>

  </UserControl.Resources>

</UserControl>

I did a sample which uses two similar controls with the difference that one uses a control template and the other do not, and demonstrates the differences while binding to properties on the UserControls (TemplatedControl and NoTemplateControl).

radioBtnTest

You can download a sample code (VS2008 Beta2 project) here, Remember to change the .DOC extension to .ZIP.