Basically I want to display the thumbnail of a contact.
So I build a ValueConverter, that should convert the Contact.Thumbnail property (type is IRandomAccessStreamReference) to the source of an Image.
I can load a BitmapImage from an url or a RandomAccessStream, but how do I get from IRandomAccessStreamReference to an RandomAccessStream? Or am I already starting with the wrong approach?
This seems to be the way I was able to get it to work. Get the path to the file and return a BitmapImage. You can bind to the BitmapImage as your image source.
BitmapImage bmp = new BitmapImage(new Uri(((StorageFile)contact.Thumbnail).Path));
Let me know if this helps.
Related
I like to get casting device icon and display it on screen. But I do not have any clue how to do this. CastingDevice.Icon is possible to read as Stream (somehow) but how to read it if I want that result is Image or BitmapImage?
CastingDevice.Icon is possible to read as Stream (somehow) but how to read it if I want that result is Image or BitmapImage?
You could use the SetSourceAsync(IRandomAccessStream)
method of BitmapImage to create a BitmapImage object, then you could use this BitmapImage as Image's Source.
I have an BitmapImage which is generated by application itself and have no sourceFile or URI. Now i have to convert this image to WritableBitmapImage in order to save it, according to this. But all the methods require sourceFile or URI.
There is no good way to extract the pixels from a BitmapImage after the fact.
As you note, if you have the source image then you can create a WriteableBitmap from that.
Since WritableBitmap and BitmapImage are both ImageSources they can be used the same way in most cases, so if you know you'll need access to the pixels when you create the BitmapImage then you can usually create a WriteableBitmap instead.
Once the BitmapImage is created and the original source is no longer available the closest you can get is to use RenderTargetBitmap to render the displayed Image control into a new bitmap from which you can extract the pixels with GetPixelData.
This will be a second generation image though and for large images will likely have lost data to resizing interpolation between the original and the rendering.
I'd recommend using a WriteableBitmap instead of a BitmapImage to begin with when generating the original image.
I am trying to add Images to my WPF application Canvas.
From what I understand, they need to be referenced as Resource in the VS Solution.
However, I need to be able to copy an image into a folder, and from an XML file the relative Uri of the image is parsed, and the image is loaded into the canvas :
Image image = new Image();
var pic = new BitmapImage();
pic.BeginInit();
pic.UriSource = new Uri(url, UriKind.Relative); // url is from the xml
pic.EndInit();
image.Source = pic;
LayoutRoot.Children.Add(image); //since the image is not in VS marked as Resource,
// nothing shows up
Thank you for your kind advices
If you specify the full path for the URI instead of using a UriKind.Relative uri, it will work properly:
pic.BeginInit();
pic.UriSource = new Uri(#"C:\Path\To\File.jpg");
pic.EndInit();
I'll be able to program a short program where you can click on a canvas and you'll get the RGB color value for the clicked pixel.
At first i had the graphics on my own HDD with absolute paths.
Problem is, that the program will crashed on each other pc ;)
So, i had created a folder named "Recourcen" within the Project explorer.
Inserted are all the graphics i need.
If i define some images in wpf and write it as follow
...Source="/Resourcen/color_wheel_730.png"
The Image get the right source.
But if i want to use it for a canvas in source, it doesn't run without getting errors.#
canvas_colorpick.Background = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(#"..\..\Resourcen\color_wheel_730.png", UriKind.Relative))
};
I had tested some formattings, but nothing will work in the source code section :(
After some hours of testing i thought i must be put a question to these forum.
Hope somebody can help me with my noob question above.
Greetings from Germany!
Bjoern
Make sure you have set the BuildAction as Resource for added image file in your project.
Also, try using WPF Pack URI to provide the uri source:
canvas_colorpick.Background = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(#"pack://application:,,,/Resourcen/color_wheel_730.png"))
};
Note - Assuming folder Resourcen folder added in same project where XAML resides.
I'm developing WPF with C# and I have this code to get the picture angry.png from the smiles folder in the output folder (e.g. bin\debug):
BitmapImage bitmap = new BitmapImage(
new Uri(#"smiles/angry.png",
UriKind.RelativeOrAbsolute));
But I want to get the picture angry.png from another location like Resources\Image\smiles\angry.png. Could you tell me how to change the folder location so that this would work?
In that case ..\..\Resources\Image\smiles\angry.png should be sufficient.