Get TypeInitializationException with ImageTools

Nov 5, 2009 at 11:43 PM

I created a simple project to test this with and am getting an TypeInitializationException when I try to save a Canvas.

I hope I am just doing something silly as this looks very intresting.

Below is my code and steps i took.

I added References

  • ImageTools
  • ImageTools.Util
  • ImageTools.IO.Png

MainPage.cs 

using System.IO;
using System.Windows;
using System.Windows.Controls;
using ImageTools;
using Image = ImageTools.Image;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Image myImage = this.MyCanvas.ToImage();
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Image Files (*.png)|*.png";

            if (sfd.ShowDialog() == true)
            {
                using (Stream stream = sfd.OpenFile())
                {
                    myImage.WriteToStream(stream);
                }
            }
        }
    }
}
MainPage.xaml
<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 
    mc:Ignorable="d" d:DesignWidth="50" Height="80">
	<Canvas>
		<Canvas x:Name="MyCanvas" Height="50" Width="50" Background="Red"/>
		<Button x:Name="btnSave" Height="25" Width="50" Canvas.Top="55" Content="Save" Click="Button_Click"/>
	</Canvas>
</UserControl>
Exception details
System.TypeInitializationException was unhandled by user code
  Message="The type initializer for 'ImageTools.ImageExtensions' threw an exception."
  TypeName="ImageTools.ImageExtensions"
  StackTrace:
       at ImageTools.ImageExtensions.ToImage(Canvas canvas)
       at SilverlightApplication1.MainPage.Button_Click(Object sender, RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
  InnerException: System.MissingMethodException
       Message="Method not found: 'Void ImageTools.IO.Encoders.AddEncoder()'."
       StackTrace:
            at ImageTools.ImageExtensions..cctor()
       InnerException: