|
Infralution Support Support groups for Infralution products
|
View previous topic :: View next topic |
Author |
Message |
hi_jack
Joined: 10 Mar 2016 Posts: 19
|
Posted: Thu Mar 10, 2016 12:30 pm Post subject: WPF loading ressource |
|
|
Hello,
I am getting an error message when trying to load a bitmap in a wpf application.
I use :
Black_Monitor = new BitmapImage ( new Uri ( @"pack://application:,,,/Resources/black_monitor.png", UriKind.Absolute ) );
The Build Action ismarked as Embedded Resource
This creates an error :
Error: Cannot locate resource 'resources/black_monitor.png'.
Stack: at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
at System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
at System.IO.Packaging.PackWebResponse.GetResponseStream()
at System.IO.Packaging.PackWebResponse.get_ContentType()
at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
at System.Windows.Media.Imaging.BitmapImage.EndInit()
at System.Windows.Media.Imaging.BitmapImage..ctor(Uri uriSource, RequestCachePolicy uriCachePolicy)
at System.Windows.Media.Imaging.BitmapImage..ctor(Uri uriSource)
at World_Terrain.MainWindow..ctor()
It works ok without encryption.
Any reccomendations in using resources?
Also I intend to use obfuscation (once I get this working)
Any recommendations regarding resources with obfuscation?
Thank you |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Thu Mar 10, 2016 9:32 pm Post subject: |
|
|
The one thing that .NET Encryptor does changes is the Application.ExecutablePath and Assembly methods that return a path to the executing assemblies will no longer work - because your entry assembly no longer has a path, because it is loaded from memory. I think that this is causing your problem. If you want to use external files relative to the application then you can use the code below to locate the executable path. This will work the same for both the encrypted and non-encrypted application:
Code: | [DllImport("kernel32.dll", SetLastError = true)]
[PreserveSig]
private static extern uint GetModuleFileName([In]IntPtr hModule,
[Out] StringBuilder lpFilename,
[In] [MarshalAs(UnmanagedType.U4)]
int nSize);
public static string ExecutablePath
{
get
{
const int maxFileName = 512;
StringBuilder fileName = new StringBuilder(maxFileName);
GetModuleFileName(IntPtr.Zero, fileName, maxFileName);
string result = fileName.ToString();
return result.Replace("vshost.exe", "exe");
}
}
public static string ExecutableDirectory
{
get
{
return Path.GetDirectoryName(ExecutablePath);
}
}
|
_________________ Infralution Support |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Fri Mar 11, 2016 10:09 pm Post subject: |
|
|
You can do the same thing without any interop calls like:
Code: | public static string ExecutablePath
{
get
{
return System.Reflection.Assembly.GetEntryAssembly().Location
}
}
public static string ExecutableDirectory
{
get
{
return Path.GetDirectoryName(ExecutablePath);
}
} |
_________________ Infralution Support |
|
Back to top |
|
|
hi_jack
Joined: 10 Mar 2016 Posts: 19
|
Posted: Mon Mar 14, 2016 4:54 pm Post subject: |
|
|
Hello,
Thank you for your answer but getting the executable path will not help.
I was not clear what I am trying to achieve is keep images in the executable as a ressource to avoid having to distribute them seperatly and manage all exceptions and errors that comes with it.
Images are packed in the executable with the function :
new Uri ( @"pack://application:,,,/Resources/black_monitor.png", UriKind.Absolute ) );
and the build action set to ressource or embeded ressource.
https://msdn.microsoft.com/library/aa970494%28v=vs.100%29.aspx
https://msdn.microsoft.com/en-us/library/aa970069%28v=vs.100%29.aspx
Is there a way to achieve this while using the encryption?
Thank you,
hi-jack |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Mon Mar 14, 2016 9:43 pm Post subject: |
|
|
Sorry I misunderstood what you were trying to do. The problem is that your URI refers to a resource in the application entry assembly - which is now the bootstrap assembly. So you could add the imageto bootstrap assembly as an embedded resource and it would work - however this is not a very good solution. Instead you need to change your URI to indicate that the resource is in the current assembly. To do this you need to include the short name of your assembly followed by the keyword component in the path. Like:
Code: | new Uri ( @"pack://application:,,,/MyAssembly;component/Resources/black_monitor.png", UriKind.Absolute ) );
|
Where MyAssembly is the shortname of your assembly. Personally I wouldn't use this technique to load images and would instead put images in the project Resources.resx file where you can then access them through the the designer generated type safe wrapper like:
Code: | Black_Monitor = Properties.Resources.black_monitor;
|
This will be more robust since it will give you compiled time warnings if there is a problem. It also makes it easy to localize the images if required. If you want to reference images and icons stored in embedded resx files from within your XAML markup you can do this using the Resx Extension which is also included as part of our Globalizer product _________________ Infralution Support |
|
Back to top |
|
|
hi_jack
Joined: 10 Mar 2016 Posts: 19
|
Posted: Thu Mar 17, 2016 10:54 am Post subject: |
|
|
Hello,
Worked fine mant thanks for the quick support.
hi-jack |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|