Wpf Resource Resources and Visual Studio 2010 Resource Editor

My motivation for this question is simply to specify the image that will be used in the user control through the dependency property for ImageSource. I click on some pain points related to management, access and unit testing for this.

  • Is a resource editor a good tool for using images for an application?
  • What is the best way to translate a bitmap from an editor to ImageSource?
  • How can I grab the resource name in the editor?
+4
source share
1 answer

No, the resource editor is not a good tool for this.

In a WPF application, the best way is to put all of your images in the Images directory and mark each one as a Resource. You can then refer to them directly in image controls and elsewhere.

Here are the exact steps:

  • Crop and otherwise adjust images using your favorite raster image editing program (Paint.NET, Photoshop, etc.).
  • Save them as .png files (or .jpg or .gif if you want)
  • Create the Images folder inside your Visual Studio solution (or several folders, however you want to organize it).
  • Drag and drop the images from the hard drive into the "Images" folder (or right-click on the project, select "Create" - "Existing item" and select the images).

Now you can easily reference your images in XAML:

<Image Source="Images/MyImage.png" /> 

Or in the code:

 var source = (BitmapSource)Application.LoadComponent( new Uri("Images/MyImage.png", UriKind.Relative)); 

You can also reference images in external assemblies:

 <Image Source="ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png" /> 

Which code would look like this:

 var source = (BitmapSource)Application.LoadComponent( new Uri("ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png", UriKind.Relative)); 
+10
source

Source: https://habr.com/ru/post/1312613/


All Articles