Unable to load external images in Silverlight - c#

I'm trying to load an image from a URL in Silverlight and have followed the steps at this site but to no avail.
My code is as follows:
imageUri = new Uri("http://php.scripts.psu.edu/dept/iit/hbg/philanthropy/Images/BlueSkyLarge.jpg", UriKind.Absolute);
System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
bi.UriSource = imageUri;
m_Image.Source = bi;
m_Image.ImageOpened += new EventHandler<RoutedEventArgs>(Image_Opened);
The callback function (Image_Opened) is never called either..

Is your Silverlight application running from the domain php.scripts.psu.edu? If not, Silverlight will block access to it because it will not allow TCP requests made to any domain other than the one the application was loaded from.
See here for network restrictions in Silverlight.
EDIT: commenter is right. It's the cross-zone issue you're seeing now. Here's a link with a table indicating what an Image (among others) can and can't do.

Another thing I would fix in your code is that you attach the handler at the end.
In theory the event handler may not be called if the image is loaded really fast.
I suppose it would not happen on most cases, but with caching/object reuse/etc. who knows. I would attach the handler just after instantiating the object and be safe.

Related

CobaltCore assembly

I try to implement a custom Wopi host in C# that can handle the Cobalt Protocol using the CobaltCore assembly.
But I didn't found any documentation for CobaltCore.dll
Object browser is a little helpful..
Please provide some details if someone had similar issue.
How I should use Cobalt to decipher the messages?
For word editing implementation go here:
Can I just use Office Web Apps Server
// fsshttpb payload, basically decode from base64 encoded
byte[] test1 = System.Convert.FromBase64String("DAALAJzPKfM5lAabBgIAAO4CAABaBBYADW1zd29yZAd3YWN6AggA1RyhD3cBFgIGAAMFABoEIAAL3Do4buY4RJXm4575cgEiigICAAALAawCAFUDAQ==");
// create an atom object from the fsshttp input
AtomFromByteArray atomRequest = new AtomFromByteArray(test1);
RequestBatch requestBatch = new RequestBatch();
requestBatch.DeserializeInputFromProtocol(atomRequest);
// now you can inspect requestBatch to view the decoded objects
edit:
Here is a sample implementation using CobaltCore. Pretty much a combination of my answers about WOPI/FSSHTTP on this website in one project.
https://github.com/thebitllc/WopiBasicEditor
Also implementing the Cobalt approach to edits and like Julia it stops at a "cant edit screen" even after lockingstore callbacks including co-author etc.
What I have found however is the log system for OWA reveals quite considerable detail about what the OWA server is attempting to do.
C:\ProgramData\Microsoft\OfficeWebApps\Data\Logs\ULS
I can see from these logs it complains about a missing access token, by providing
&access_token=1&access_token_ttl=0
to the end of the wopi url this error goes away.
I also tested many of the file info fields and was able to see how the OWA server caches information. If we keep changing the cfi.Version
FileInfo info = new FileInfo("C:\\WOPI OWA WORD EDITOR\\OWA_Source_Documents\\" + fi.Name);
cfi.Version = info.LastWriteTimeUtc.ToString("s");
we get a fresh cached item each time we change the files contents via normal word.
These also affect the View mode for Word and I suspect will lock us out of the word edit mode but since I don't have that working I cant tell yet.
cfi.SupportsCoauth = true; // all three (3) needed to see the edit in browser menu in view mode .
cfi.SupportsCobalt = true; // all three (3) needed to see the edit in browser menu in view mode .
cfi.SupportsFolders = true; // all three (3) needed to see the edit in browser menu in view mode .
cfi.SupportsLocks = true;
cfi.SupportsScenarioLinks = false;
cfi.SupportsSecureStore = true;
cfi.SupportsUpdate = true;
This one locks out the word edit function and unless you update the version of the file it will stay locked even if you change it back to false.
cfi.WebEditingDisabled = false;
Roger Hogg
thanks to thebitllc for the correct approach to getting back the file.
System.IO.FileStream _FileStream = new System.IO.FileStream("C:\\WOPI OWA WORD EDITOR\\OWA_Updated_Documents\\output.docx", System.IO.FileMode.Create, System.IO.FileAccess.Write);
GenericFdaStream myCobaltStream = new GenericFda(cobaltFile.CobaltEndpoint, null).GetContentStream();
myCobaltStream.CopyTo(_FileStream);
_FileStream.Close();

Simple photo taking, storing, loading on Windows Phone

In my application I have a data entry view which I'd also like to give the user the option to store a photograph. Currently, using the tutorial at http://msdn.microsoft.com/en-US/library/windowsphone/develop/hh394006(v=vs.105).aspx, I've got a button which lets me take the photo, and can display it back properly at the bottom of the view.
However, I'd like this photo to be saved as part of the record. I thought it would be simple to save the location of the file as a string(I can access this by using PhotoResultObject.OriginalFileName), then display the photo again by using a URI like in my code here. This is inside the cameraCaptureTask_Completed method, e is a PhotoResult object.
string imageLoc = e.OriginalFileName;
Uri imageUri = new Uri(imageLoc, UriKind.RelativeOrAbsolute);
StreamResourceInfo resourceInfo = Application.GetResourceStream(imageUri);
BitmapImage bmp = new BitmapImage();
bmp.SetSource(resourceInfo.Stream);
myImage.Source = bmp;
However, this gives me an Argument exception, or various other exceptions when I move things around. I'm pretty new to c# so I'm really certain what's going on.
I reckon it's something to do with trying to access the photo straight from the phone memory, but I haven't found any other clear ways to store photos which will stay accessible inside the app.
Any tips on where I'm going wrong or how to accomplish my aim would be appreciated greatly!

Get a screenshot of the web browser control?

There are a lot threads about this but none of them were clear and none I tried actually even worked right.
What is the code to get the contents of the entire web browser control (even that which is off screen)?
It looks like they did have:
webBrowser1.DrawToBitmap(); // but its unsupported and doesnt work
3rd party api - not wasting my time
.DrawToBitmap and nonanswer-links
100 wrong answers
just takes a screenshot
Try to make sure you are calling the method in the DocumentCompleted event.
webBrowser1.Width = wb.Document.Body.ScrollRectangle.Width;
webBrowser1.Height = wb.Document.Body.ScrollRectangle.Height;
Bitmap bitmap = new Bitmap(webBrowser1.Width, webBrowser1.Height);
webBrowser1.DrawToBitmap(bitmap, new Rectangle(0, 0, webBrowser1.Width, webBrowser1.Height));
I was working on a similiar function in my project last week, read a few posts on this topic including your links. I'd like to share my experience:
The key part of this function is System.Windows.Forms.WebBrowser.DrawToBitmap method.
but its unsupported and doesnt work
It is supported and does work, but not always works fine. In some circumstances you will get a blank image screenshot(in my experience, the more complex html it loads, the more possible it fails. In my project only very simple and well-formatted htmls will be loaded into the WebBrowser control so I never get blank images).
Anyway I have no 100% perfect solution either. Here is part of my core code and hope it helps (it works on ASP.NET MVC 3).
using (var browser = new System.Windows.Forms.WebBrowser())
{
browser.DocumentCompleted += delegate
{
using (var pic = new Bitmap(browser.Width, browser.Height))
{
browser.DrawToBitmap(pic, new Rectangle(0, 0, pic.Width, pic.Height));
pic.Save(imagePath);
}
};
browser.Navigate(Server.MapPath("~") + htmlPath); //a file or a url
browser.ScrollBarsEnabled = false;
while (browser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}

Moving XML file to be hosted

Please be gentle. I am not a terribly proficient developer!
So this is the last thing I need to fix in my Windows Phone 7.5 app before I consider it done. In short, the data sources on the menus are driven by an xml file. That file is stored locally with the app. I would like to store that file somewhere on the Internet). Currently if I need to make a change to this xml file, I have to re-submit the app to the Marketplace taking about 5 days before the change goes live. How 2003 of me.
So I can't figure out what they are expecting returned in the code below. I've hacked away and it always give some error I don't understand.
I've set the filename variable to a URL of a file on the Internet but apparently that is not supported. So I either need a new way for that whole section to work or a way to convert the hosted filename converted into something that will work.
private static void FirstLaunch()
{
// On the first launch, just add everything from the OPML file
string filename;
//This file should really be hosted on the Internet somewhere.
filename = "/RSSReader;component/LyncNews-opml.xml";
StreamResourceInfo xml = App.GetResourceStream(new Uri(filename, UriKind.Relative));
List<RSSPage> rssPages = ParseOPML(xml.Stream);
}
You can set it to a URL, but you'll need to download the content, not through App.GetResourceStream. Try WebClient, it's easy and simple.
A simple usage:
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Client_DownloadStringCompleted);
Uri token = new Uri("your url");
client.DownloadStringAsync(token);
and handle xml parsing in the event.

Newb: Update XAML Image Element During Runtime (WIndows Phone, .NET 4)

Greetings all,
Thanks for reading and sharing any insights. I've created a new simple Windows Phone 7 application. I have two Image objects on the form I would like to update with various .png files. I have included the .png files into my project, and I believe I am building proper URi resources pointing to these files.
Uri myImage1URi = new Uri( strImage1, UriKind.RelativeOrAbsolute );
Uri myImage2URi = new Uri( strImage2, UriKind.RelativeOrAbsolute );
System.Windows.Media.Imaging.BitmapImage bi1 = new System.Windows.Media.Imaging.BitmapImage(myImage1URi);
System.Windows.Media.Imaging.BitmapImage bi2 = new System.Windows.Media.Imaging.BitmapImage(myImage2URi);
image1.Source = bi1;
image1.Stretch = Stretch.Fill;
image2.Source = bi2;
image2.Stretch = Stretch.Fill;
This alone is not accomplishing what I want (to update the images to the two from the URi's).
I know there is something a bit off going on (IE: I am doing something dumb) in that all of the BitmapImage class descriptions mention that I have to do a .BeginInit() before I work with the BitmapImage object, and a .EndInit() call afterwards. These method calls don't work for me, so I know something is amiss....
Or maybe I am competely off base and I simply need a way to tell my main window to repaint itself? That thought has occurred to me as well.
Thanks again.
The following will load an image that is in the appropriate path and is set as having a build action content.
myImg.Source = new BitmapImage(new Uri("/images/filename.png", UriKind.Relative));
It assumes XAML on the page like:
<Image x:Name="myImg" />
This seems very similar to what you're doing. Simplify what you're doing ot get it working.
Does it work with just using one image?
Is the path in strImageN a valid path?
Do the image files have their build action set to Content?

Categories

Resources