setting image source from url in window phone - silverlight

i am setting image source from URL , but it does not show image properly ,
code which i use in windows phone 8 is below , thanks in advance ..
image1.Source = new BitmapImage(new Uri("http://d3sdoylwcs36el.cloudfront.net/VEN-virtual-enterprise-network-business-opportunities-small-fish_id799929_size485.jpg",UriKind.Absolute));
result is

i done it ,, call web service for image .. code is below ..
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += ImageOpenReadCompleted;
webClient.OpenReadAsync(new Uri("http://d3sdoylwcs36el.cloudfront.net/VEN-virtual-enterprise-network-business-opportunities-small-fish_id799929_size485.jpg"));
private void ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.Result);
image1.Source = bmp;
}
}
done it ... :)

Related

How to display images one after another on a single page

This is my code , please let me know the solution for it.
var images = (from pd in SvarkWindow.prodlist where pd.Product_name.StartsWith(imgname) select pd.Image).ToList();
BitmapImage b = new BitmapImage();
b.BeginInit();
//b.UriSource = new Uri(images.ElementAtOrDefault(0), UriKind.Relative);
b.UriSource = new Uri("http://portal.liftech.in/presc/" + images.ElementAtOrDefault(0));
b.EndInit();
// ... Get Image reference from sender.
Image img1 = Productimage0 as Image;
img1.Source = b;
I did this in this way.
look at the code.
ImageSource imageSource = new BitmapImage(new Uri("http://portal.liftech.in/presc/" + images.ElementAtOrDefault(0)));
System.Windows.Controls.Image image1 = new System.Windows.Controls.Image();
image1.Source = imageSource;
Grid.SetColumn(image1, 0);//image1 is added to column 0
Grid.SetRow(image1, 0);//row 0
ProductGrid0.Children.Add(image1);

byte[] to ImageSource stream

I need to convert byte[] to BitmapImage and show it in WPF image control. (img.Source = ...).
if i convert it like this:
m_photo = new BitmapImage();
using (MemoryStream stream = new MemoryStream(photo.ToArray()))
{
m_photo.BeginInit();
m_photo.StreamSource = stream;
m_photo.EndInit();
}
it can't do XAML binding to Source property because "m_photo owns another stream"... What can I do?
Set the cache option to OnLoad after begininit
m_photo.CacheOption = BitmapCacheOption.OnLoad;
EDIT: complete code for bmp array to Image source
DrawingGroup dGroup = new DrawingGroup();
using (DrawingContext drawingContext = dGroup.Open())
{
var bmpImage = new BitmapImage();
bmpImage.BeginInit();
bmpImage.CacheOption = BitmapCacheOption.OnLoad;
bmpImage.StreamSource = new MemoryStream(photoArray);
bmpImage.EndInit();
drawingContext.DrawImage(bmpImage, new Rect(0, 0, bmpImage.PixelWidth, bmpImage.PixelHeight));
drawingContext.Close();
}
DrawingImage dImage = new DrawingImage(dGroup);
if (dImage.CanFreeze)
dImage.Freeze();
imageControl.Source = dImage;
Ok, I just found solution. If use this code (converting byte[] to bitmapSource) in code of class - you have this error, that the object is in another stream. But if create a Converter (IValueConverter) and use it with same code of converting in XAML binding - everything ok!
Thanks everybody!

WP7: can't get img from httpget url using webclient.OpenReadAsync

I need to get image from this API on Windows phone7 appplication,
getvmsimg
Description :
API for get variable message sign (VMS) as img
URL:
http://athena.traffy.in.th/apis/apitraffy.php?api=…&id=…[w=...]&[h=...]
Formats:
Image: PNG
HTTP Method:
GET
Requires Authentication :
true
API rate limited :
unlimited
and this is my code
I have to get a session key by another API first(Completely,No problem) and then i have to use the session key as a parameter in httpget url.
my key is correctly 100 percent, i have checked.
but It error at "image.SetSource(e.Result);" line (Unspecified error).
public intsign()
{
InitializeComponent();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://api.traffy.in.th/apis/getKey.php?appid="+appid));
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
rd = e.Result;
sessionkey = MD5Core.GetHashString(appid + rd) + MD5Core.GetHashString(hiddenkey + rd);
//MessageBox.Show(sessionkey);
client2.OpenReadCompleted += new OpenReadCompletedEventHandler(client2_OpenReadCompleted);
client2.OpenReadAsync(new Uri("http://athena.traffy.in.th/apis/apitraffy.php?api=getvmsimg&key=" + sessionkey + "&appid=" + appid + "&id=1&h=480&w=480"),client2);
}
void client2_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
BitmapImage image = new BitmapImage();
image.SetSource(e.Result);
intsignimg.Source = image;
}
thx, guy
Did you try to write the entire stream (with a buffer or else) into a MemoryStream and then use that MemoryStream instead of using directly the resulting stream ?

Creating WPF examples in LinqPad

Is there any way to sanely instantiate WPF objects in LinqPad? Here's my example (the correct assemblies are added in the query, etc):
var w = new Window();
w.Loaded += (o,e) => {
w.Content = new TextBlock() { Text = "Foo" };
};
w.Show();
However, this dies a horrible death:
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.
at System.Windows.Input.TextServicesContext.StopTransitoryExtension()
at System.Windows.Input.TextServicesContext.Uninitialize(Boolean appDomainShutdown)
at System.Windows.Input.TextServicesContext.TextServicesContextShutDownListener.OnShutDown(Object target, Object sender, EventArgs e)
at MS.Internal.ShutDownListener.HandleShutDown(Object sender, EventArgs e)
Any clues on how I can get this to work?
Another way to do it is as follows:
w.ShowDialog();
Dispatcher.CurrentDispatcher.InvokeShutdown(); // Cleanly end WPF session.
More examples:
new Window { Content = "Foo" }.ShowDialog();
new Window { Content = new Button { FontSize = 50, Content = "Foo" } }.ShowDialog();
Dispatcher.CurrentDispatcher.InvokeShutdown(); // Cleanly end WPF session.
You need to run a message loop by calling new Application().Run(w).

How to display Bitmap Image in image control on WPF using C#

I want that when I double click on a row in ListView, it should display the Image corresponding to that row. This row also contains the path of the Image.
I tried the following but it displays the same Image for all rows because I have given the path for a specific Image:
private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ImageSource imageSource = new BitmapImage(new Uri(#"C:\northwindimages\king.bmp"));
image1.Source = imageSource;
}
Please suggest something.
They key is to retrieve the row index that was clicked, and get the image URL for that row. Since you say you are clicking on the row, this can be done in a method similar to that below
private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataRow row = (DataRow)sender; //Get the row that was clicked
string imageURL = row["imageUrl"].ToString();//Get the img URL for that row
ImageSource imageSource = new BitmapImage(new Uri(imageURL));
image1.Source = imageSource;
}
Hope this helps
Suppose that:
The list you are binding to contains Elephant objects and,
You want the image to show Elephant.Picture whenever you double-click an item.
You can set the image from an event handler as follows:
private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var viewItem = sender as ListViewItem;
if(viewItem!=null)
{
var elephant = viewItem.DataContext as Elephant;
image1.Source = elephant.Picture;
}
}
Note that it is important to only accept double clicks on the ListViewItem.
The above code assumes that elephant.Picture is of type ImageSource. If it is something else you will have to convert it. For example, if instead Elephant has a string "PicturePath" property, the image1.Source line would change to:
image1.Source = new BitmapImage(new Uri(elephant.PicturePath));

Resources