Save image to windows phone 7 pictures hub - windows-phone-7

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.

Related

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;
}

In C# on windows phone Attempt to access the method failed: System.IO.FileStream..ctor(System.String, System.IO.FileMode)

FileStream FS = new FileStream("MyFolder\\MyFile.txt", FileMode.Open);
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("MyFolder\\MyFile.txt", FileMode.Append, myIsolatedStorage));
using (writeFile)
{
FS.Seek(0, SeekOrigin.End);
writeFile.WriteLine(txtWrite.Text);
writeFile.Close();
System.Diagnostics.Debug.WriteLine("Now I am here");
}
When I am trying to run this code(trying to append data into an existing text file), getting exception
"Attempt to access the method failed:
System.IO.FileStream..ctor(System.String, System.IO.FileMode)"
What is the mistake I have done here?
Don't use the FileStream class directory. Get your streams via the methods on IsolatedStorageFile:
IsolatedStorageFile myIsolatedStorage =
IsolatedStorageFile.GetUserStoreForApplication();
using (var writeFile = myIsolatedStorage.OpenFile("MyFolder\\MyFile.txt", FileMode.Append))
using (var writeFileStream = new StreamWriter(writeFile))
{
writeFileStream.WriteLine(txtWrite.Text);
System.Diagnostics.Debug.WriteLine("Now I am here");
}
Could it be that you're attempting to open the same file twice?
A version of your question (with an answer) can be seen at How to append data into the same file in IsolatedStorage for Windows Phone
Finally I made it working after struggling for 4 hrs:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("MyFolder\\MyFile.txt", FileMode.Append, myIsolatedStorage));
writeFile.Flush();
System.Diagnostics.Debug.WriteLine(txtWrite.Text);
writeFile.WriteLine(txtWrite.Text);
writeFile.Close();
System.Diagnostics.Debug.WriteLine("Now I am here");
I removed the file stream method and did some modifications. Its started to work.
Thanks to everybody who tried to help me with your suggestions

Bind images from isolated storage in windows phone 7

I have a list box which binds a list of objects . Each object has the field image file name . Each of these images present in the isolated storage .
I have tried to implement binding of these images into the list box , I am not getting the images . Please advise on how to do this .
I have looked into many forums and unable to solve this .
Best Regards,
Yash
Store your image in isolated storage in stream rather than images and than read the stream.
It will work for you. Below is the sample code.
store images in isolated like this
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("IsoStoreFile.png", FileMode.Create, isoStore))
{
//Save the image file stream rather than BitmapImage to Isolated Storage.
byte[] content = new byte[e.Result.Length];
e.Result.Read(content, 0, content.Length);
isoStream.Write(content, 0, content.Length);
isoStream.Flush();
}
Now you can open the saved file and display it in an image:
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream = isoStore.OpenFile("IsoStoreFile.png", FileMode.Open))
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(isoStream);
img.Source = bmp;
}
}

how to get length of wav file in windows phone

I jwant to get length of wave file. Currently I'm using following code
using (IsolatedStorageFile isofile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isostream = new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Open,System.IO.FileAccess.Read, isofile))
{
me = new MediaElement();
me.SetSource(isostream);
}
}
embedVoiceLength = me.NaturalDuration.TimeSpan.TotalSeconds;
However, it doesn't return the length from naturalduration.timespan.totalseconds , because me is not opened;
If you can't get the length until the file is opened then try opening it.
If all you want is the length and to not actually play it then handle the MediaOpened event and when it is triggered get the length and then stop the playback.
You can use something like this:
Microphone microphone = Microphone.Default;
using (IsolatedStorageFileStream stream = storage.OpenFile(filename, FileMode.Open, FileAccess.Read))
{
TimeSpan duration = microphone.GetSampleDuration((int)stream.Length);
}
I picked this sample code from an interesting article on working with audio on WP7. Here it is, code is also available for download. Hope this helps! :)

Loading an .ico from web and convert to BitmapSource

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.

Resources