Download image from server - image

I've to download an image from the server on button click.
The code is:
private void Button_Click(object sender, RoutedEventArgs e)
{
(sender as Button).IsEnabled = false;
progressbar.IsIndeterminate = true;
WebClient w = new WebClient();
w.OpenReadAsync(new Uri("http://example.com/xxx/image.png"));
w.OpenReadCompleted += new OpenReadCompletedEventHandler(w_OpenReadCompleted);
}
void w_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
progressbar.IsIndeterminate = false;
BitmapImage b = new BitmapImage();
b.SetSource(e.Result);
Image img = new Image();
img.Source = b;
LayoutRoot.Children.Add(img);
}
The problem which I'm facing is that for the first time the data is being downloaded from the server and shown correctly. However, if I quit the application and again start it, it downloads the old image even if I have deleted the image from the server or changed the image. I think the image is getting cached somewhere but don't know how to solve this problem.

I think this will be the same as your issue:
How do you disable caching with WebClient and Windows Phone 7
I haven't noticed this behaviour when using the HttpWebRequest to get data. But I'am not sure about it.
Update: HttpWebRequest has by default the same behaviour but can be disabled. This blogpost is talking about the options you have:
http://www.nickharris.net/2010/10/windows-phone-7-httpwebrequest-returns-same-response-from-cache/

Finally managed to fix it.The only change that was made is:
w.OpenReadAsync(new Uri("http://example.com/xxx/image.png?q="+Guid.NewGuid()));

You could also use HttpWebRequest to download fresh data every request. Here is a simple class which sets up the asynchronous calls
Here is a simple http client which will download data from the given uri.
public static class HttpClient
{
public static void Execute(Uri uri, Action<HttpWebRequest> onrequest, Action<HttpWebResponse> onresponse)
{
var request = HttpWebRequest.CreateHttp(uri);
onrequest(request);
request.BeginGetResponse
(
result =>
{
try
{
if (request.HaveResponse)
onresponse((HttpWebResponse)request.EndGetResponse(result));
}
catch { }
},
null
);
}
}
Using the HttpClient with your button click event you get this
private void Button_Click(object sender, RoutedEventArgs e)
{
(sender as Button).IsEnabled = false;
progressbar.IsIndeterminate = true;
HttpClient.Execute
(
new Uri(http://example.com/xxx/image.png),
request =>
{
request.UserAgent = "Custom HTTP Client";
},
response =>
{
progressbar.IsIndeterminate = false;
BitmapImage b = new BitmapImage();
b.SetSource(response.GetResponseStream());
Image img = new Image();
img.Source = b;
LayoutRoot.Children.Add(img);
}
);
}

Related

How to display images into another page using navigation query string in windows phone 7?

i'm not able to display the images to another page. The images are taken from json. So i'm trying to pass the image url of the selected item of a listbox into a navigagtion query string.The variable i'm trying to pass the data is showing as null.Plese provide me with solution. Thanks
Here the code of the first page:
private void ImageList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var lbi = (sender as ListBox).SelectedItem;
if (e.AddedItems.Count > 0)
{
Uri targetPage = new Uri("/DisplayPhoto.xaml?selectedItem="+ lbi.ToString(),UriKind.RelativeOrAbsolute);
NavigationService.Navigate(targetPage);
}
((ListBox)sender).SelectedIndex = -1;
}
Code of the second page:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
Uri uri = new Uri(selectedIndex, UriKind.Absolute);
var img = new Image();
img.Source = new BitmapImage(uri);
img.Height = 400;
img.Width = 400;
listBox1.Items.Add(img);
}
base.OnNavigatedTo(e);
}
Try to escape your parameters with Uri.EscapeUriString
Uri targetPage = new Uri("/DisplayPhoto.xaml?selectedItem="+ Uri.EscapeUriString(lbi.ToString()),UriKind.RelativeOrAbsolute);
NavigationService.Navigate(targetPage);
I prefer sending data via some static property, preferably inside the App class. This way you can "send" complex objects from one page to another.

How to update the userlocation to server when the location was changed in windows phone 7?

I need to update the userlocation to server when the location is changed .Please tell me how to acheive this?
using below method:
void GeoPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
_pushpins.Clear();
var cor = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
//Using the current coordinates define the Map center to those coordinates.
map.Center = cor;
_pushpins.Add(new PushpinModel
{
UserId=userId,
Description = "test",
Name = "test",
Location = new GeoCoordinate(e.Position.Location.Latitude,
e.Position.Location.Longitude),
DatetimeAdded = DateTime.Now
});
map.ZoomLevel = 15;
//locationupdate method
locationupdate(e.Position.Location.Latitude,
e.Position.Location.Longitude);
}
private void locationupdate(double lat,double lon)
{
bool isAvailable = NetworkInterface.GetIsNetworkAvailable();
if (isAvailable == true)
{
try
{
WebClient wc = new WebClient();
wc.DownloadStringAsync(
new Uri("http://{ipaddress}/Network/Records/UpdateLocation?userid="+userId+"&latitude="+lat+"&longitude="+lon));
wc.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(
wc_DownloadStringUpdateCompleted);
}
catch (Exception ex)
{
throw;
}
}
else
{
MessageBox.Show("Network is not available.Please try again later");
}
}
Please tell me the above will update the location whenever user was changed his location?
Separate your goal...
Do you already know how to get the user location?
Use background agents to run taks on the background (if necessary) http://www.silverlightshow.net/items/Windows-Phone-7.5-Use-Background-agents.aspx
Create a webservice to update you're server

DownloadStringCompleted event does not fire

I am developing a windows phone application. The webclient does not fire as I expected. The related code is as below:
public PArticle(PocketListItem aPli)
{
this.pli = aPli;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!isf.FileExists(aPli.ID + ".json"))
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri(pli.Url));
}
else
{
string json = RetrieveDataFromLocalStorage(aPli.ID + ".json");
PocketArticle pa = JsonConvert.DeserializeObject<PocketArticle>(json);
this.text = pa.text;
}
}
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var readability = Readability.Create(e.Result);
this.text = readability.Content;
}
I know it's a synchronous/asynchronous problem. But I have no idea about how to handle it.
Thanks in advance.
I have tested the WebClient part of your code with 2 different URLs http://riktamtech.com and http://google.com. In both cases, the DownloadStringCompleted event is raised. I observed it by placing a break point.
So I suggest you to test again with break points.

Camera Capture in WP7 Mango

I've recently upgraded my WP7 app to Mango and am having some problems with the camera. The code below used to work on 7.0, but on 7.1 the completed handler fires before the dialog is even shown, so I can't capture the result. After taking the photo, the phone displays "Resuming..." which it never used to do.
var dlg = new CameraCaptureTask();
dlg.Completed += (s, e) =>
{
if (e.TaskResult == TaskResult.OK) {
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
//var img = new Image();
//img.Source = bmp;
string caption = string.Empty;
var inputDialog = new InputPrompt()
{
Title = "Caption",
Message = "Enter caption/description for snapshot",
};
inputDialog.Completed += (ss, ee) =>
{
if (ee.PopUpResult == PopUpResult.Ok)
{
caption = ee.Result;
var snap = SnapshotBLL.AddSnapshot(recipeId, bmp, caption);
onComplete(null, new SnapshotEventArgs(snap));
}
};
inputDialog.Show();
}
};
dlg.Show();
The MSDN docs appear to show a variation of my code but I can no longer get the result of the camera capture task.
Assuming that your sample comes from a single method I wouldn't expect it to ahve worked pre Mango.
The CameraCaptureTask should be created and the callback assigned in the constructor of the page for it to work properly.
Something like:
public partial class MainPage : PhoneApplicationPage
{
private CameraCaptureTask cct = new CameraCaptureTask();
public MainPage()
{
InitializeComponent();
cct.Completed += new EventHandler<PhotoResult>(cct_Completed);
}
private void cct_Completed(object sender, PhotoResult e)
{
// Do whatever here
}
private void SomeEventHandler(object sender, RoutedEventArgs e)
{
cct.Show();
}
}
This works in both 7.0 & 7.1

UI not updating in async web request callback

I'm using this to make a web request and download some data:
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
var client = new WebClient();
client.DownloadStringCompleted += (s, e) => {
textBlock1.Text = e.Result;
};
client.DownloadStringAsync(new Uri("http://example.com"));
}
}
The text of textBlock1 never changes even though e.Result has the correct data. How do I update that from the callback?
Edit: If I add MessageBox.Show(e.Result); in the callback along with the textBlock1.Text assignment, both the messsage box and the text box show the correct data.
Edit Again: If I add a TextBox and set it's text right after the line textBlock1.Text line, they both show the correct text.
I think, it's a bug.
I also ran into some problems with updating the UI from different dispatchers. What I finally did was use the TextBlock's (or other UI Element) own dispatcher and that worked for me. I think the phone framework may be using different dispatchers between the app and UI Elements. Notice the change from dispatcher.BeginInvoke to textbox1.Dispatcher...
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var dispatcher = Deployment.Current.Dispatcher;
var client = new WebClient();
client.DownloadStringCompleted += (s, e) =>
{
var result = e.Result;
textBlock1.Dispatcher.BeginInvoke(
()=> textBlock1.Text = result
);
};
client.DownloadStringAsync(new Uri("http://example.com"));
}
From browsing through the WP7 forums, a bunch of people were reporting that this was related to a video card driver issue. I've updated my ATI Radeon HD 3400 drivers to the latest version and it appears to work now.
client.DownloadStringAsync is expecting a Uri like this:
client.DownloadStringAsync(new Uri("http://example.com"));
also, shouldn't you update your TextBlock through a Dispatcher.BeginInvoke like this:
client.DownloadStringCompleted += (s, e) =>
{
if (null == e.Error)
Dispatcher.BeginInvoke(() => UpdateStatus(e.Result));
else
Dispatcher.BeginInvoke(() => UpdateStatus("Operation failed: " + e.Error.Message));
};
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var dispatcher = Deployment.Current.Dispatcher;
var client = new WebClient();
client.DownloadStringCompleted += (s, e) =>
{
var result = e.Result;
dispatcher.BeginInvoke(
()=> textBlock1.Text = result
);
};
client.DownloadStringAsync(new Uri("http://example.com"));
}
}
I want to comment but can't yet. Yes, I have a very similar issue. In my case it's my viewmodel that is updating a DownloadStatus property, then when the download is completed I do some more work and continue updating this property.
The view stops updating once the ViewModel code hits the OpenReadCompleted method. I've stepped carefully through the code. PropertyChanged fires, and the view even comes back and retrieves the new property value, but never shows the change.
I was sure it was a bug, but then I created a brand new project to reproduce the issue, and it works fine!
Here's a snippet of my non-reproducing code. The UI textblock bound to "DownloadStatus" happily updates properly all the way through. But the same paradigm doesn't work in my main project. Infuriating!
public void BeginDownload(bool doWorkAfterDownload)
{
DownloadStatus = "Starting ...";
_doExtraWork = doWorkAfterDownload;
var webClient = new WebClient();
string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("test:password"));
webClient.Headers["Authorization"] = auth;
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://www.ben.geek.nz/samsung1.jpg"));
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null)
{
DownloadStatus = e.Error.Message;
return;
}
DownloadStatus = "Completed. Idle.";
if(_doExtraWork)
{
Thread t = new Thread(DoWork);
t.Start(e.Result);
}
}
void DoWork(object param)
{
InvokeDownloadCompleted(new EventArgs());
// just do some updating
for (int i = 1; i <= 10; i++)
{
DownloadStatus = string.Format("Doing work {0}/10", i);
Thread.Sleep(500);
}
DownloadStatus = "Completed extra work. Idle.";
InvokeExtraWorkCompleted(new EventArgs());
}

Resources