public static Texture2D ConvertBitmapToTexture(BitmapImage bs) { var texture2D = new Texture2D(GraphicsDeviceManager.Current.GraphicsDevice, bs.PixelWidth, bs.PixelHeight, false, SurfaceFormat.Color); bs.CopyTo(texture2D); return texture2D; }
public static Texture2D LoadBitmapFromApplicationResources(string imageName) { var sr = Application.GetResourceStream(new Uri(imageName, UriKind.Relative)); var bs = new BitmapImage(); bs.SetSource(sr.Stream); var texture2D = new Texture2D(GraphicsDeviceManager.Current.GraphicsDevice, bs.PixelWidth, bs.PixelHeight, false, SurfaceFormat.Color); bs.CopyTo(texture2D); return texture2D; }
private void LoadNormalTexture() { Debug.Assert( TextureType == TextureType.OneD || TextureType == TextureType.TwoD ); if ( Root.Instance.RenderSystem.ConfigOptions[ "Use Content Pipeline" ].Value == "Yes" ) { var acm = new AxiomContentManager( (XnaRenderSystem)Root.Instance.RenderSystem, "Content" ); _normTexture = acm.Load<Texture2D>( Name ); _texture = _normTexture; Width = _normTexture.Width; Height = _normTexture.Height; internalResourcesCreated = true; } #if !( XBOX || XBOX360 ) else { Stream stream; if ( Name.EndsWith( ".dds" ) ) { stream = ResourceGroupManager.Instance.OpenResource( Name ); // use Xna to load the image directly from the stream //XFG.TextureCreationParameters tcp = new XFG.TextureCreationParameters(); //tcp.Filter = Microsoft.Xna.Framework.Graphics.FilterOptions.Triangle; //tcp.MipLevels = MipmapCount; //Not sure how to set MipLevels. _normTexture.LevelCount is get-only... #if SILVERLIGHT var im = new BitmapImage(); im.SetSource(stream); _normTexture = new Texture2D(_device, im.PixelWidth, im.PixelHeight, false, SurfaceFormat.Color); im.CopyTo(_normTexture); #else _normTexture = Texture2D.FromStream( _device, stream ); //.FromFile( _device, stream, tcp ); #endif // store a ref for the base texture interface _texture = _normTexture; //reset stream position to read Texture information ////stream.Position = 0; // set the image data attributes //Not sure if these lines accomplish the same thing as the below commented-out ones. SetSrcAttributes( _normTexture.Width, _normTexture.Height, 1, XnaHelper.Convert( _normTexture.Format ) ); SetFinalAttributes( _normTexture.Width, _normTexture.Height, 1, XnaHelper.Convert( _normTexture.Format ) ); //XFG.TextureInformation info = XFG.Texture2D.GetTextureInformation( stream ); //SetSrcAttributes( info.Width, info.Height, 1, XnaHelper.Convert( info.Format ) ); //SetFinalAttributes( info.Width, info.Height, 1, XnaHelper.Convert( info.Format ) ); internalResourcesCreated = true; } else { // find & load resource data intro stream to allow resource group changes if required stream = ResourceGroupManager.Instance.OpenResource( Name, Group, true, this ); //#if SILVERLIGHT // if (stream == null) // { // Name += ".png"; // stream = ResourceGroupManager.Instance.OpenResource( Name, Group, true, this ); // } //#endif var pos = Name.LastIndexOf( "." ); var ext = Name.Substring( pos + 1 ); // Call internal LoadImages, not LoadImage since that's external and // will determine load status etc again var image = Image.FromStream( stream, ext ); LoadImages( new[] { image } ); image.Dispose(); } if (stream != null) stream.Close(); } #endif }