问题
I have an Esri map in silverlight and I am trying to get a screenshot. However, I am facing cross-domain content protection for writeablebitmap, saying "Pixels are not accessible".
Is there a way that I can disable this? or any other work around in silverlight 3 or 4?
回答1:
you can kind of hack around this by rehosting the image. basically you can set up a wcf service on your server that acts as a proxy to the map, then when silverlight does the security check on your media source it will see that it is 'hosted' by your server, then it should let you render it to a Writeable bitmap. though this could obviously greatly increase the load on your server.
回答2:
I have videos stored on an Amazon cloudfront server with a valid clientaccesspolicy.xml to allow cross domain access for the silverlight host URL and the following function did not allow me to take screen shots of the video and manipulate the pixels until it was replaced with with a WebClient download instead (a WebClient honors the cross domain policy files, you can see it being requested in Fiddler, something that the MediaElement.Source function did not even attempt)
Old none working cross domain code
public LoadVideoFromURL(string url)
{
var uri = new Uri(url);
myMediaElement.Source(uri);
}
new working cross domain code
public LoadVideoFromURL(string url)
{
var uri = new Uri(url);
//Request the video
var videoDownloader = new WebClient();
videoDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(
(s, args) => myMediaElement.SetSource(args.Result));
videoDownloader.OpenReadAsync(uri);
}
回答3:
The simplest way to work around this problem is to make sure that the Silverlight application and the images are hosted from the same domain. Since you are asking this question, I will assume that this is not possible.
Do you control the server where the images are hosted? If so, you can add a Client Access Policy file to enable cross-domain access to the images from your Silverlight app. Then you need to change how you are consuming these images. Instead of setting the Source of the Image element to a URI of the image, use WebClient to download the image as a Stream and set the source of the Image element directly to that Stream. If you do this, you will not run into the cross-domain access violations when trying to access the WriteableBitmap pixels.
回答4:
If the source of the bitmap is another domain, the simple answer is no. I'm not sure how you're trying to take this "screen shot", but it sounds like there are good security reasons to stop this... Of course, full (read/write) pixel access is available normally, when your image is in the local domain.
The right way to resolve this, given you seem to want a screenshot of the user's desktop taken, would be to make the Silverlight app require full trust, which is possible since Silverlight 3 (and expanded in Silverlight 4). This does however require it to be an OOB (out of browser app) I believe.
来源:https://stackoverflow.com/questions/3041365/how-do-i-disable-silverlight-cross-domain-content-protection-for-writeablebitmap