/// <summary>
        /// Creates an XNA texture from an image.
        /// </summary>
        /// <param name="bitmapSource">A BitmapSource object.</param>
        /// <returns>A Texture2D object.</returns>
        public static Texture2D CreateTexture2D(BitmapSource bitmapSource)
        {
            if (bitmapSource == null)
            {
                throw new ArgumentNullException("bitmapSource");
            }
            if (bitmapSource.PixelWidth == 0)
            {
                return(null);
            }
            var texture = new Texture2D(
                GraphicsDevice,
                bitmapSource.PixelWidth,
                bitmapSource.PixelHeight,
                false,
                SurfaceFormat.Color);

            bitmapSource.CopyTo(texture);
            return(texture);
        }