The url has to be an Image URL, ending with jpeg, png, jpg, gif
Note:
1 ) The entire code has not be en implemented following the MVVM pattern Every thing has been done from the code behind just for example purpose.
2 ) The Project should also have a reference to * Microsoft.Xna.Framework.dll *
3 ) All the contents in the xaml Page should be places inside the TAG
4 ) The example also Demonstrates the use of Regular Expression for Image URLs
Only two pages to design this simple application
1) MainPage.xaml 2) MainPage.xaml.cs
Code For:
MainPage.xaml.
MainPage.xaml.cs:
public partial class MainPage : PhoneApplicationPage
{
///
/// Constructor to main page
///
public MainPage()
{
InitializeComponent();
}
///
/// Call web client to read the data
///
/// Uri
public void Fetch(Uri uri)
{
//web client to communicate with the specified URL
WebClient webClient = new WebClient();
//attach event of on read compleated
webClient.OpenReadCompleted += this.ReadCompleted;
//opeans a readable stream for the asynchronous call
webClient.OpenReadAsync(uri);
}
///
/// Executes when reading of the page has been compleated
///
/// object
/// OpenReadCompletedEventArgs
private void ReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
Stream stream = e.Result;
//create a bitmap image of the image from url
BitmapImage bmp = new BitmapImage();
bmp.SetSource(stream);
//stream to rad Image Data
using (MemoryStream streamImageData = new MemoryStream())
{
WriteableBitmap wbmp = new WriteableBitmap(bmp);
//set target width and height to Save Image to media library
wbmp.SaveJpeg(streamImageData, 300, 300, 0, 100);
//set the position of the stream to beggning
streamImageData.Seek(0, SeekOrigin.Begin);
//save image in media library
using (MediaLibrary mediaLibrary = new MediaLibrary())
mediaLibrary.SavePicture("Picture.jpg", streamImageData);
}
}
///
/// Click Event for download button
///
/// object
/// RoutedEventArgs
private void btnDownload_Click(object sender, RoutedEventArgs e)
{
//image url
string strImageURL = wbGetImage.Source.ToString();
//Check valid Image Url
if (Regex.IsMatch(strImageURL, "http://(\\S+?)\\.(jpg|png|gif|jpeg)"))
{
//create new URL for the Image
Uri imageURl = new Uri(wbGetImage.Source.ToString(), UriKind.Absolute);
Fetch(imageURl);
}
else
{
MessageBox.Show("Invalid Image URL");
}
}
}
Now to view all the Images and your downloaded images from the device’s Media Library please go through the following Link :
http://www.ourgoalplan.com/KLMS/TipView.aspx?id=2994