Posts Tagged ‘ assembly ’

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.StreamSource = resourceStream.Stream;
  return bitmap;