简体   繁体   中英

Silverlight: Using WriteableBitmap to Catch Pixels from an External URL Image Throws an Security Exception

I have several UIElement s, including an Image (with an External URL image), and then I want to catch a thumbnail of these elements, so I use WriteableBitmap to catch the pixels for each UIElement.

However, when I try to catch the pixel for the Image using:

WriteableBitmap wb = new WriteableBitmap(image, new ScaleTransform()
                     {
                         ScaleX = 0.5,
                         ScaleY = 0.5,
                     });
...
wb.GetPixeli(x, y); // Throws exception

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap(v=vs.95).aspx

The WriteableBitmap class has a security model that restricts access to the Pixels array, if the WriteableBitmap is constructed using cross-domain content. For example, a WriteableBitmap that is constructed using a BitmapImage referencing a URL that comes from another domain does not permit access to its Pixels array. The restriction extends to any UI element that uses a URL-derived property for setting some or all of its content. In particular, this restriction applies to the "Grab a frame of a running video from MediaElement" scenario. If the MediaElement.Source references a video file from another domain, the WriteableBitmap created by referencing the MediaElement as the element source restricts access to the Pixels array.

So in order to catch the thumbnail of these several UIElement s (including an Image element) do I have to download the image to a temp directory and then render it?

My advice would be to double-check what you're trying to do - if you're writing a Silverlight application, chances are you want to reduce the amount of data flowing to the client, before downloading the full image(s). Would it be better to produce these thumbnails server-side (either on-the-fly or caching) and deliver them already smaller, and where you can use the full .NET framework to produce the thumbnails?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM