Greetings all,
Thanks for reading and sharing any insights. I've created a new simple Windows Phone 7 application. I have two Image objects on the form I would like to update with various .png files. I have included the .png files into my project, and I believe I am building proper URi resources pointing to these files.
Uri myImage1URi = new Uri( strImage1, UriKind.RelativeOrAbsolute );
Uri myImage2URi = new Uri( strImage2, UriKind.RelativeOrAbsolute );
System.Windows.Media.Imaging.BitmapImage bi1 = new System.Windows.Media.Imaging.BitmapImage(myImage1URi);
System.Windows.Media.Imaging.BitmapImage bi2 = new System.Windows.Media.Imaging.BitmapImage(myImage2URi);
image1.Source = bi1;
image1.Stretch = Stretch.Fill;
image2.Source = bi2;
image2.Stretch = Stretch.Fill;
This alone is not accomplishing what I want (to update the images to the two from the URi's).
I know there is something a bit off going on (IE: I am doing something dumb) in that all of the BitmapImage class descriptions mention that I have to do a .BeginInit() before I work with the BitmapImage object, and a .EndInit() call afterwards. These method calls don't work for me, so I know something is amiss....
Or maybe I am competely off base and I simply need a way to tell my main window to repaint itself? That thought has occurred to me as well.
Thanks again.
The following will load an image that is in the appropriate path and is set as having a build action content.
myImg.Source = new BitmapImage(new Uri("/images/filename.png", UriKind.Relative));
It assumes XAML on the page like:
<Image x:Name="myImg" />
This seems very similar to what you're doing. Simplify what you're doing ot get it working.
Does it work with just using one image?
Is the path in strImageN a valid path?
Do the image files have their build action set to Content?
Related
Trying to understand Assembly, Uri and all of those things and there is some stuff I don't understand and hopefully I can ask them here in one go.
I have a solution that contains two "Assembly's" if I understood it correctly like this.
Project Assemblies
Where I want my FetchResources to fetch that image and display it on my MainWindow.
It works without a problem when I do it in xaml like this.
Xaml FetchResources
But in behind-code I use the same Uri but for some reason it won't display the image.
var uri = new Uri("pack://application,,,/WPF_UserControll_Test;component/Images/71805972.jpg", UriKind.Absolute);
BitmapImage bmi = new BitmapImage();
bmi.UriSource = uri;
testimage.Source = bmi;
I'm not sure if I've understood Assembly correctly nor do I understand Uri to the full extent. I've read Microsofts Pack URIs in WPF but it's not clear to me how it works.
Why Can I reach the image through XAML but not in behind-code?
You are missing a colon (:) after "application". Try this:
testimage.Source = new BitmapImage(new Uri("pack://application:,,,/WPF_UserControll_Test;component/Images/71805972.jpg", UriKind.Absolute));
And if you don't use the constructor overload that accepts a Uri, you should call the BeginInit() and EndInit() methods before and after you set the UriSource property:
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource = new Uri("pack://application:,,,/WPF_UserControll_Test;component/Images/71805972.jpg", UriKind.Absolute);
bmi.EndInit();
testimage.Source = bmi;
Try this:
MSDN
The following example shows the pack URI for a XAML resource file that is located in the root folder of a referenced, version-specific assembly's project folder.
pack://application:,,,/ReferencedAssembly;v1.0.0.1;component/ResourceFile.xaml
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.
In my application I have a data entry view which I'd also like to give the user the option to store a photograph. Currently, using the tutorial at http://msdn.microsoft.com/en-US/library/windowsphone/develop/hh394006(v=vs.105).aspx, I've got a button which lets me take the photo, and can display it back properly at the bottom of the view.
However, I'd like this photo to be saved as part of the record. I thought it would be simple to save the location of the file as a string(I can access this by using PhotoResultObject.OriginalFileName), then display the photo again by using a URI like in my code here. This is inside the cameraCaptureTask_Completed method, e is a PhotoResult object.
string imageLoc = e.OriginalFileName;
Uri imageUri = new Uri(imageLoc, UriKind.RelativeOrAbsolute);
StreamResourceInfo resourceInfo = Application.GetResourceStream(imageUri);
BitmapImage bmp = new BitmapImage();
bmp.SetSource(resourceInfo.Stream);
myImage.Source = bmp;
However, this gives me an Argument exception, or various other exceptions when I move things around. I'm pretty new to c# so I'm really certain what's going on.
I reckon it's something to do with trying to access the photo straight from the phone memory, but I haven't found any other clear ways to store photos which will stay accessible inside the app.
Any tips on where I'm going wrong or how to accomplish my aim would be appreciated greatly!
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 have built a digital signage solution running in WPF. I have an executable that handles scheduling and a number of external assemblies (.dll with user controls) that is responsible of displaying an image, a video etc.
When the application starts it loads all the assemblies into a list and then uses an XML file with configuration info on which kind of slide to start with which parameters, like this:
<signage>
<slide type="image" argument="c:\image1.png"/>
<slide type="image" argument="c:\image2.png"/>
<slide type="video" argument="c:\video1.mpg"/>
...
</signage>
That list can be very long and contain many different slides. When i build my list of objects to show i create a new instance, via reflection, of the .dll that i need to show, passing it the argument. This is contained within a list. The parent executable is then traversing the list and displaying the user control instances (the instantiated slide types) on my main WPF application.
Thats the background for my question.
When i need to update the contents on runtime i need to replace the files on disk (image1, image2 etc...) but get an exception that the files are in use by another process and that i cannot access them.
Any idea on how to fix this? Is there any way i can "unload" them from my application? Is there any way to do this in a proper way?
EDIT
Here is some additional info on how i do this in an image assembly:
public ImageExtensionControl(XmlDocument document, Dictionary<string, string> settings, Action slideFinished)
{
InitializeComponent();
string path = new FileInfo(Assembly.GetCallingAssembly().Location).Directory.FullName;
string id = settings["Image"];
path = System.IO.Path.Combine(path, document.SelectSingleNode("properties/files/file[#id='" + id + "']").Attributes["path"].Value);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri(path, UriKind.Absolute);
bitmapImage.EndInit();
this.myimage.Source = bitmapImage;
}
If you're loading your images in, ensure you are closing the stream once you're finished with your operation. For instance,
using(var fs = new FileStream(...))
{
// ...
} // <-- Stream is closed and disposed.
Possibly try caching options:
Problems overwriting (re-saving) image when it was set as image source
imgTemp = new BitmapImage();
imgTemp.BeginInit();
imgTemp.CacheOption = BitmapCacheOption.OnLoad;
imgTemp.CreateOption = BitmapCreateOptions.IgnoreImageCache;
imgTemp.UriSource = uriSource;
imgTemp.EndInit();
imgAsset.Source = imgTemp;