Assign Bitmapimage from Resources.Resx to Image.Source

Assign BitmapImage from Resources.resx to Image.Source?

In order to make that Uri work, the file logo.png must be contained in a folder named "Resources" in your VS project (see first image), and its Build Action must be set to Resource (see second image).

VS Project

Build Action

This Resources folder is completely unrelated to Resources.resx. You may rename it to whatever you like.

How to use Resources.resx to link images

you can't do that. that worked only in winforms

see this post for more info

Different way how add image to resources

use the method shown in this post

WPF image resources

instead

quote:

If you will use the image in multiple places, then it's worth loading the image data only once into memory and then sharing it between all Image elements.

To do this, create a BitmapSource as a resource somewhere:

<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />

Then, in your code, use something like:

<Image Source="{StaticResource MyImageSource}" />

In my case, I found that I had to set the Image.png file to have a build action of Resource rather than just Content. This causes the image to be carried within your compiled assembly.

Pack URI to image embedded in a resx file

I don't think it's possible using the "pack" protocol scheme. This protocol is related to normalized Open Packaging Conventions specs (http://tools.ietf.org/id/draft-shur-pack-uri-scheme-05.txt for pointers). So the pack uri points to the application package's resources (or parts in OPC terms), not to .NET embedded resources.

However, you can define your own scheme, for example "resx" and use it in WPF component uris. New Uri schemes for such usages can be defined using WebRequest.RegisterPrefix.

Here is an example based on a small Wpf application project named "WpfApplication1". This application has a Resource1.resx file defined (and possibly other localized corresponding Resource1 files, like Resource1.fr-FR.resx for french for example). Each of these ResX files define an Image resource named "img" (note this name is not the same as the image file name the resource is based on).

Here is the MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Image Source="resx:///WpfApplication1.Resource1/img" />
</Window>

The uri format is this:

resx://assembly name/resource set name/resource name

and assembly name is optional, so

resx:///resource set name/resource name

is also valid and point to resources in the main assembly (my sample uses this)

This is the code that supports it, in App.xaml.cs or somewhere else, you need to register the new scheme:

public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
ResXWebRequestFactory.Register();
base.OnStartup(e);
}
}

And the scheme implementation:

public sealed class ResXWebRequestFactory : IWebRequestCreate
{
public const string Scheme = "resx";
private static ResXWebRequestFactory _factory = new ResXWebRequestFactory();

private ResXWebRequestFactory()
{
}

// call this before anything else
public static void Register()
{
WebRequest.RegisterPrefix(Scheme, _factory);
}

WebRequest IWebRequestCreate.Create(Uri uri)
{
return new ResXWebRequest(uri);
}

private class ResXWebRequest : WebRequest
{
public ResXWebRequest(Uri uri)
{
Uri = uri;
}

public Uri Uri { get; set; }

public override WebResponse GetResponse()
{
return new ResXWebResponse(Uri);
}
}

private class ResXWebResponse : WebResponse
{
public ResXWebResponse(Uri uri)
{
Uri = uri;
}

public Uri Uri { get; set; }

public override Stream GetResponseStream()
{
Assembly asm;
if (string.IsNullOrEmpty(Uri.Host))
{
asm = Assembly.GetEntryAssembly();
}
else
{
asm = Assembly.Load(Uri.Host);
}

int filePos = Uri.LocalPath.LastIndexOf('/');
string baseName = Uri.LocalPath.Substring(1, filePos - 1);
string name = Uri.LocalPath.Substring(filePos + 1);

ResourceManager rm = new ResourceManager(baseName, asm);
object obj = rm.GetObject(name);

Stream stream = obj as Stream;
if (stream != null)
return stream;

Bitmap bmp = obj as Bitmap; // System.Drawing.Bitmap
if (bmp != null)
{
stream = new MemoryStream();
bmp.Save(stream, bmp.RawFormat);
bmp.Dispose();
stream.Position = 0;
return stream;
}

// TODO: add other formats
return null;
}
}
}

Loading image from resource to image control

In code behind you'll have to use a fully qualified Resource File Pack Uri

themeImage.UriSource = new Uri(
"pack://application:,,,/ZApp;component/Resources/Images/Themes/" + filename);

You may also avoid the BeginInit/EndInit calls by using the BitmapImage constructor that takes an Uri argument:

imgThemeStyle.Source = new BitmapImage(new Uri(
"pack://application:,,,/ZApp;component/Resources/Images/Themes/" + filename));

Casting BitmapImage to Image.Source

Windows.Media.Imaging.BitmapImage is a WPF class. Windows.UI.XAML.media.ImageSource is UWP. You found solutions for WPF, but your UWP Image class wants a UWP bitmap object. You need something using Windows.UI.Xaml.Media.Imaging.BitmapImage.

I'm unable to test UWP at the moment, but from reading the documentation, I think you want something like this:

image.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(imageUrl, UriKind.Absolute));

It appears that you have using Windows.Media.Imaging; in your C# file. It would be wise to replace that with using Windows.UI.Xaml.Media.Imaging;

Then you could just use this:

image.Source = new BitmapImage(new Uri(imageUrl, UriKind.Absolute));


Related Topics



Leave a reply



Submit