Problems handling imported images

Sep 8, 2009 at 10:41 AM


great library - very useful!
However, I found a little issue - I tried to apply the PhysicsObjectBehavior to imported Photoshop image objects - that is, I imported a layered PSD image into Blend and then tried to apply the PhysicsObjectBehavior to individual image objects created by the importer. (The importer creates transparent PNG images per default).

When trying to run the project, I always get an exception:

Message: Unhandled Error in Silverlight Application Index was outside the bounds of the array.   at Spritehand.FarseerHelper.BoundaryHelper.GetNextVertex(UIElement element, Point current, Point last, Double left, Double top, Double width, Double height, WriteableBitmap bitmap)
   at Spritehand.FarseerHelper.BoundaryHelper.GetPointsForImage(Image element, UIElement containerElement, Boolean positionOnly)
   at Spritehand.FarseerHelper.BoundaryHelper.GetPointsForElement(UIElement element, UIElement containerElement, Boolean positionOnly)
   at Spritehand.FarseerHelper.PhysicsSprite..ctor(PhysicsSimulator physicsSim, Canvas parentCanvas, UIElement element, List`1 points, Boolean showDebug, Single defaultFriction, UIElement boundaryElement)
   at Spritehand.FarseerHelper.PhysicsControllerMain.AddPhysicsBody(PhysicsObjectMain physObject)
   at Spritehand.PhysicsBehaviors.PhysicsObjectBehavior.controller_Initialized(Object source)
   at Spritehand.FarseerHelper.PhysicsControllerMain._timerInitialize_Completed(Object sender, EventArgs e)
   at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

At first I thought the PNGs were top complex, too many points or something - but it also crashes with a very simple black Rectangle created form PSD import. It works when I create & import the PNGs individually - so I guess it has something to do with Blends importer ...


Sep 8, 2009 at 12:34 PM

I haven't tried importing PhotoShop images yet - but I'm guessing what is happening is that the import isn't leaving a zero (0) in the byte value for the transparent pixels. You could try bringing the exported image up in a different editor, such as Paint.NET and then using the magic wand tool in there to select and delete the background pixels and delete them... that might take care of the issue?

Sep 8, 2009 at 3:36 PM
Edited Sep 8, 2009 at 3:37 PM

That seems to be the issue - if you have anti-aliased edges around the image (not a straight fallof from 1 to 0 but gradual alpha values in between) then it crashes. This happens with all Photoshop exported images since Photoshop usually does the feathered selection for a softer look ...

Workaround would be to place another XAML element around the image and apply behaviors to that (tradeoff would be losing the nice irregular shape of the transparent image - so it would only work in some scenarios).