Blog posts about wpf

  • Convert to BitmapImage

    March 30, 2011 | Tags: WPF, Image Library, Open source, Snippet
    Want to convert your KalikoImage (if your using C# Image Library) into a System.Windows.Media.Imaging.BitmapImage for use with for instance WPF, then here's your code (kalikoImage being your KalikoImage object):
    MemoryStream ms = new MemoryStream();
    kalikoImage.SaveBmp(ms);
    ms.Position = 0;
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = ms;
    bi.EndInit();

    If you instead want to convert a System.Drawing.Bitmap to a System.Windows.Media.Imaging.BitmapImage, then this is your code (myBmp being your Bitmap object):
    MemoryStream ms = new MemoryStream();
    myBmp.Save(ms, ImageFormat.Bmp);
    ms.Position = 0;
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = ms;
    bi.EndInit();

    Once you have your BitmapImage, you can pass that into a System.Windows.Controls.Image:
    System.Windows.Controls.Image img = new System.Windows.Controls.Image();
    img.Source = bi;

  • Get height of TextBlock in WPF

    May 21, 2010 | Tags: WPF, XAML, CodeProject, C#, Snippet
    Sometimes it's quite nice to get the height of a textblock at the same time it's created. In WPF however there's a couple of events that handle the layout functions when an element is rendered, and before these have been fired, you won't get proper values from the DesiredSize property. The good news is that these can be triggered by the Arrange function. You call the Arrange function with a Rect (rectangle) representing the column width and max height (example below use 1000px, but you might need more for longer texts) of your text.

    TextBlock t = new TextBlock();
    int columnWidth = 310;
    t.FontFamily = new FontFamily("Arial");
    t.FontSize = 20;
    t.Text = "Here goes the text..";
    t.Width = columnWidth;
    t.TextWrapping = TextWrapping.Wrap;
    t.Arrange(new Rect(0,0,columnWidth,1000));
    int textHeight = (int)t.DesiredSize.Height;

  • No Xaml-editor in Visual Studio 2008 problem

    May 07, 2010 | Tags: WPF, XAML, C#, Troubleshooting
    I recently started working on a WPF project in Visual Studio 2008, but didn't get the design mode for the Xaml files to work properly. Trying to turn on the design mode I was faced with the following message:

    There is no editor available for ".xaml". Make sure the application for file type(.xaml) is installed.

    Tried reinstalling service packs and all, but no luch. Until I ran into this thread. The answer given there worked for me too. And it's to close down Visual Studio and then run the following command:

    devenv /resetskippkgs

    Voilá, there was the designer in working order! :)

  • Using relative path in WPF/Xaml

    May 05, 2010 | Tags: WPF, XAML, C#, Snippet
    Creating a new Uri for a relative file path (like "myfolder\myfile.txt") in WPF will result in an UriFormatException being thrown. This is because the parser has no idea what type of path it is. Uri's is far more than "c:\myfile.txt" and you can read all about it over at Microsoft. But to keep it simple, this is what a relative Uri should be written like:

    Uri myRelativeUri = new Uri("pack://siteoforigin:,,,/myfolder/myfile.txt");

  • Adobe Illustrator to XAML

    March 30, 2010 | Tags: WPF, XAML, C#
    This is a great plugin for exporting from Adobe Illustrator to XAML for use in WPF or Silverlight projects. The plugin is freely available for both Mac and Windows over at:
    http://www.mikeswanson.com/xamlexport/