Posts Tagged ‘ image ’

Load image from Assembly

If you create a new CustomControl and want to load an image from the resources you can use this method.
Because in CustomContorls you can not load the image with the path, therefore you have to load it directyl from the assembly.

public static BitmapImage getImageFromAssembly(string filename)
{
  var path = string.Format("/MyAssembly;component/{0}", filename);
  var uri = new Uri(path, UriKind.Relative);
  var resourceStream = Application.GetResourceStream(uri);
  if (resourceStream == null)
    return null;

  var bitmap = new BitmapImage();
  bitmap.BeginInit();
  bitmap.StreamSource = resourceStream.Stream;
  bitmap.EndInit();
  return bitmap;
}