How to reference image resources in XAML?

后端 未结 4 1113
轮回少年
轮回少年 2020-11-28 22:08

I put an Image control on a Window and I would like to display an image that is stored in a project resource file named \"Resources.resx\". The name of the imag

4条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 22:52

    If you've got an image in the Icons folder of your project and its build action is "Resource", you can refer to it like this:

    
    

    That's the simplest way to do it. This is the only way I could figure doing it purely from the resource standpoint and no project files:

    var resourceManager = new ResourceManager(typeof (Resources));
    var bitmap = resourceManager.GetObject("Search") as System.Drawing.Bitmap;
    
    var memoryStream = new MemoryStream();
    bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
    memoryStream.Position = 0;
    
    var bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = memoryStream;
    bitmapImage.EndInit();
    
    this.image1.Source = bitmapImage;
    

提交回复
热议问题