I have a problem. I am trying to load an image, so I use this code:
string resourceID = "MyApp.Templates.Good_Question.png";
Assembly assembly = GetType().GetTypeInfo().Assembly;
using (Stream stream = assembly.GetManifestResourceStream(resourceID))
{
bitmap = SKBitmap.Decode(stream);
}
But it gives an error on stream, because stream is null. Now I created a folder in the root of the app called Templates and placed an image called Good_Question.png in the folder.
Why is my stream null?
I found it, I had to set the Build Action to Embedded Resource, not Resource
Related
I've been working on a file that was set-up to load data from a file, turn it into a MemoryMappedFile and then use the mmf.
However, deploying this to Android (through Unity) means we can no longer access files like this. Instead, we're trying to embed the file (.bin) into the project and final .DLL.
I've got the stream of the embedded resource, but I don't know if/how I can turn it into a MemoryMappedFile. Casting it as a fileStream doesn't work and simply continuing as before doesn't work:
Assembly assembly = Assembly.GetExecutingAssembly();
string resourcePath = file;
resourcePath = assembly.GetManifestResourceNames()
.Single(str => str.EndsWith(file));
Stream stream = assembly.GetManifestResourceStream(resourcePath);
this.fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
this.mmf = MemoryMappedFile.CreateFromFile(fileStream,
null,
0L,
MemoryMappedFileAccess.Read,
HandleInheritability.None,
false);
Before this, I've never used embedded resources or MemoryMappedFiles before, so I'm trying to understand if there is any way to make them talk to each other - or if a complete refactor is needed.
I'm trying to get an image in the resources as a byte[] for insertion into a database. The resource is at Resources/CatSeal and is a file called index.jpg.
I've looked at this question, but I'm still having trouble. I'm getting a NullReferenceException on the indicated line. My namespace is DatabaseConnectionTests. According to this documentation, under "Access Resources" it should follow this format, which I believe I'm doing:
MyNameSpace.MyImage.bmp
Here's my code:
Stream sourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DatabaseConnectionTests.index.jpg");
using (var memoryStream = new MemoryStream())
{
sourceStream.CopyTo(memoryStream); // NullReferenceException here
seal.SealerImage = memoryStream.ToArray();
}
sealDatabaseOperations.Insert(seal);
How can I resolve this so that my resource image is loaded to a byte[]? Thanks in advance.
Looks like it's not finding your resource.
Try: "DatabaseConnectionTests.Resources.index.jpg"
Set a breakpoint in a class in the same assembly and evaluate this:
this.GetType().Assembly.GetManifestResourceNames()
That will list all resource names avail for that assembly.
I'm having some really weird problems trying to load a file pragmatically into my project to save to a local folder.
I can load the file fine in my .xaml code, as so:
<BitmapImage x:Key="Image" UriSource ="/Assets/Submandibular_oedema.jpg" />
And I can display that image on my page. However, when I try to load the image and use it in my xaml.cs code like this
uriMyFile = new Uri("/Assets/Submandibular_oedema.jpg", UriKind.RelativeOrAbsolute);
It cannot find the file and as a result, won't let me do anything with the URI.
For background, my aim is to get the image stream then save it to a local folder.
I know it'll be a stupid little problem, but I can't find any solutions to it.
Thanks!
Try full path (specify your project assembly)
uriMyFile = new Uri("/YourAssemblyName;component/Assets/Submandibular_oedema.jpg",UriKind.RelativeOrAbsolute);
Update after Starktastics comment:
I finally understood your problem: you need the Application.GetResourceStream method to retrieve a stream of your resource, read from that stream and write it to a file stream.
var streamResourceInfo = Application.GetResourceStream(uri);
var stream = streamResourceInfo.Stream;
var byteBuffer = new byte[stream.Length];
using (stream)
{
stream.Read(byteBuffer, 0, byteBuffer.Length);
}
using (var fileStream = new FileStream("photo.jpg", FileMode.Create))
{
fileStream.Write(byteBuffer, 0, byteBuffer.Length);
}
I've updated the solution that you can download here.
Original answer:
If you use a WPF application, make sure that the asset you added to your project is to Build Action 'Resource'. You can check that in the properties pane after you clicked the file.
In any case, your syntax for the URI is correct. I checked it in a small project myself and it works. You can download it here (DropBox link)
Feel free to leave a comment.
Try to use
uriMyFile=new Uri("ms-appx:///Assets/YourImage.png"));
This works for me while trying to show a diffrent map icon for each place.
i am trying to make a simple application in which i have an image i have copied it in Assets folder of my project. The image i got from the web, and it is in the png format.
can some body give me an idea that how i can copy my images to my project so that when i deploy the project on device i will able to load them.
Current i what i am trying is.
var streamResource = App.GetResourceStream(new Uri("/Assets/Tiles/gradiant-mask.png", UriKind.Relative));
using (Stream stream = streamResource.Stream) {
var maskData = new byte[stream.Length];
stream.Read(maskData, 0, maskData.Length);
}
But i always get the streamResource object as null and the may be the reason is it didn't find the file on the device. can some body guide me that how i can load the image on the device in my wp8 application.
Make sure the Build action is set to Content on the properties of the image file in Visual Studio.
If you want the Build action to be set to Resource, use the following URI syntax:
new Uri("/YOUR_PROJECT_NAME;component/Assets/Tiles/gradiant-mask.png", UriKind.Relative)
Using the Content build action is recommended.
you can directly load image by :
in XAML
Source="/Assets/Tiles/gradiant-mask.png"
or in code behind by
imagename.Source = new Uri("/Assets/Tiles/gradiant-mask.png",UriKind.Relative);
set the build action as content .
I'm trying to build a wp7 application that must allow user to read ebooks in epub format. Since there isn't any available library to read epub file on a windows phone, I'm trying to create one. So I must unzip the file and then parse it.
The problem is that I can't unzip the epub file. I'm using SharpZipLib.WindowsPhone7.dll but I get an exception:
Attempt to access the method failed: System.IO.File.OpenRead(System.String)
on this line:
ZipInputStream s = new ZipInputStream(File.OpenRead(path_epubfile));
Can any one help me, please?
It's going to depend on how the content is obtained. There's three possible options here;
Option 1: If the content is added to your project with a Build Action of "Content" you can obtain a stream by using the StreamResourceInfo class (In the System.Windows.Resources namespace)
StreamResourceInfo info = Application.GetResourceStream(new Uri("MyContent.txt", UriKind.Relative));
using (info.Stream) {
// Make use of the stream as you will
}
Option 2: If you've added it to your project and set the Build Action to "Embedded Resource" then you'll need to use GetManifestResourceStream()
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ProjectName.MyContent.txt")) {
// Make use of stream as you will
}
Note: You'll need to replace "ProjectName" with the name of your project. So if your project was "EPubReader" and the embedded resource was "Example.txt" you'd need to pass "EPubReader.Example.txt" to GetManifestResourceStream(). You can use GetManifestResourceNames() to see what resources are available.
Option 3: If you've obtained the content at run time, it'll be stored in IsolatedStorage.
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) {
using (IsolatedStorageFileStream stream = store.OpenFile("MyContent.txt", FileMode.Open)) {
// Make use of stream as you will
}
}