Loading an .ico from web and convert to BitmapSource - windows-phone-7

Im trying to load a file of type .ICO from web and use it as an image in my windows phone. But I get an error when trying to set the source of the BitmapImage. Here is my code:
WebClient client = new WebClient();
client.OpenReadCompleted += (s, e) => {
if (e.Cancelled) return;
if (null != e.Error) throw e.Error;
BitmapImage image = new BitmapImage();
image.SetSource(e.Result);
this.Favicon = image;
};
client.OpenReadAsync(new Uri(#"http://mysite/myimage.ico", UriKind.RelativeOrAbsolute));
Does BitmapImage support "ico" files? how to convert an "ico" to a supported BitmapImage file.
THe solution must work on Windows Phone 7.
Tks

The only formats supported by BitmapImage seem to be PNG and JPG.
Not completely stated here, but implied by the comments: http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage(VS.95).aspx
A silverlight implementation of .ico handling is here:
http://blogs.msdn.com/b/jstegman/archive/2009/09/13/silverlight-ico-icon-decoder-parser.aspx
You might have to modify it to be compatible with the version of silverlight on Windows Phone 7.

Related

How to open native video player to play local contents in Xamarin forms

I have some video file embedded with the project, that to be opened in the native video player using Xamarin forms.
Note: Having in-app video player is limited here.
Any idea for this?
Sorry for late, you could refer to FormsNativeVideoPlayer.
When you load your video file from Raw folder, you just need to modify the VideoPlayer_CustomRenderer code like this :
protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged (e);
...
string uriPath = "android.resource://" + Forms.Context.PackageName + "/" + Resource.Raw.audio;
var uri = Android.Net.Uri.Parse((uriPath));
//Set the videoView with our uri, this could also be a local video on device
videoView.SetVideoURI (uri);
...
}

XAML Image source has issues displaying a deep nested path

This is quite vexing.
I am working on an app for image management. Part of the value is the ability to store images in sub-folders based on image properties, eg. creation date.
If I store the image source in a shallow folder (app\images\img.jpg), everything works fine.
If I store the image in KnownFolders.Pictures\source\year\month\day\img.jpg, Image does not render. (Yes, that specific path won't work, I am trying to give you a sense of how the path is constructed)...
The file is actually there. The path is correct (I can open it in a browser, e.g.). The app has access to the file.
But it does not render the bitmap.
I tried to render the bitmap manually using
new BitmapImage(new Uri("KnownFolders.Pictures\source\year\month\day\img.jpg"),UriKind.Absolute))
That does not render anything. (Again, assume the path is valid and has a file at its bottom).
What Am I Missing?
The head scratcher: for GIF anims, I am using Thomas Levesque's useful component: https://github.com/XamlAnimatedGif. That one, unfortunately, does only render gifs... and it does so even when the path is the one given above. So the Standard IMAGE control does not render correctly, but Thomas's control does... infuriating.
An UWP app can't load a BitmapImage from an absolute URL to a file in a folder structure below the Pictures Library Folder.
So this won't work:
var relativePath = #"source\year\month\day\img.jpg";
var imageFile = await KnownFolders.PicturesLibrary.GetFileAsync(relativePath);
var bitmapImage = new BitmapImage(new Uri(imageFile.Path));
However, you could do this:
var relativePath= #"source\year\month\day\img.jpg";
var imageFile = await KnownFolders.PicturesLibrary.GetFileAsync(relativePath);
var bitmapImage = new BitmapImage();
using (var stream = await imageFile.OpenAsync(FileAccessMode.Read))
{
await bitmapImage.SetSourceAsync(stream);
}
So, after way too much time spent on this...
First, link to DataContextChanged of the IMAGE element. In there, parse the DataContext out. If you are using the IMAGE outside of an ItemsControl etc, this is not required...
private async void ImageView_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
if (sender is Image)
{
Image img = (Image)sender;
if (img.DataContext is ImageView)
{
MyViewDataContext dc = (MyViewDataContext)img.DataContext;
img.Source = await dc.bitmap();
}
}
}
And here the implementation of MyViewDataContext.bitmap() which has a property called source that yields, you guessed it, absolute paths:
public async Task<BitmapImage> MyViewDataContext.bitmap()
{
if (_bitmap == null)
{
try
{
StorageFile file = await StorageFile.GetFileFromPathAsync(source);
bool r = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.CheckAccess(file);
if (r)
{
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
// create a new bitmap, coz the old one must be done for...
_bitmap = new BitmapImage();
// And get that bitmap sucked in from stream.
await _bitmap.SetSourceAsync(fileStream);
}
}
}
catch (Exception e)
{
_bitmap = null;
}
}
return _bitmap;
}
BitmapImage _bitmap;
I cache the resulting bitmap until I dispose of this MyViewDataContext.
I am now most concerned about memory. This one worries me:
How to dispose BitmapImage cache?
So, as a tech debt, I am going to address the potential mem leaks later, once this whole thing is on the test bench and I can take a look at its runtime behavior...
To access the folders and libraries represented by the properties of this class, specify the corresponding capabilities in your app manifest. For example, to access KnownFolders.PicturesLibrary, specify the Pictures Library capability in the app manifest.
Hope this will help
KnowFolders

How to convert rad controls to images in silverlight

I'm using rad controls(charts and gridview) for developing an application,which i need to export the controls(each) into image.I have tried each control converting them into bytes format and send to webservice and converting them to images but sometime sending the byte data to service throws an error.Is any other way to convert each control into image.I have tried another way like.
Stream fileStream = File.OpenRead(#"\\HARAVEER-PC\TempImages\FlashPivot.png");
//PART 2 - use the stream to write the file output.
productChart.ExportToImage(fileStream, new Telerik.Windows.Media.Imaging.PngBitmapEncoder());
fileStream.Close();
It throwing me an error like cannot access to the folder TempImages.I have given sharing permissions to everyone but it doesn't access the folder.
Any solution is much appreciated.
private BitmapImage CreateChartImages()
{
Guid photoID = System.Guid.NewGuid();
string photolocation = #"D:\Temp\" + photoID.ToString() + ".jpg";
BitmapImage bi = new BitmapImage(new Uri(photolocation, UriKind.Absolute));
using (MemoryStream ms = new MemoryStream())
{
radChart.ExportToImage(ms, new PngBitmapEncoder());
bi.SetSource(ms);
}
return bi;
}

Save image to windows phone 7 pictures hub

I'm trying to save a selected picture in a listbox to phone's memory but i don't understand why i'm getting an "InvalidOperationException was Unhandled" error.
var filePath = "Uploads/" + fileListBox.SelectedItem;
var fileUriSource = new Uri(filePath, UriKind.Relative);
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(filePath.ToString(), FileMode.Open))
{
MediaLibrary mediaLibrary = new MediaLibrary();
Picture pic = mediaLibrary.SavePicture("saved" + filePath.ToString(), fileStream);
fileStream.Close();
}
}
I already search on the internet but i couldn't find any answer. Any help would be great.
Thanks!
As per MSDN:
InvalidOperationException
Exception that is thrown if SavePicture is called while the user is
tethered to a computer running .
This is because the library is locked when connected to Zune on the PC to avoid any issues with changing files during syncing.
If you must do this on a device while connected you can use the WPConnect tool instead of Zune.

xna - why cant i see the bitmap class while adding photos through photoChooser

I am trying to add photos to my game through photochooser.
I am using all the right usings (images, shell, etc.) but can't get to the bitmap class
What am I missing?
Update:
I tried using System.Windows.Media.Imaging.
and still can't find the bitmapimage class.
I can't find using System.Windows.dll.
can you be more specific? I get a sgwigly line below bitmapImage.
void photoChooserTask_Completed(object sender, PhotoResult result)
{
if (result.TaskResult == TaskResult.OK)
{
var bitmapImage=new
System.Windows.Media.Imaging.BitmapImage bmp =
new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(result.ChosenPhoto);
}
}
If you're trying to use System.Bitmap then this isn't supported on the phone.
You probably want System.Windows.Media.Imaging.BitmapImage (in System.Windows.dll) instead.

Resources