I get a warning on that line. You're not using imageData afterwards. Is it normal ? If yes, I would just test the dynamic cast without setting a variable.