Exemplo n.º 1
0
        public static Texture2D LoadTexture(IGraphicsService graphicsService, string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName));
            }
            if (fileName.Length == 0)
            {
                throw new ArgumentException("The file name must not be empty.", nameof(fileName));
            }

            // Load API-independent texture.
            Texture texture = null;

            using (var stream = File.OpenRead(fileName))
            {
                string extension = Path.GetExtension(fileName);
                if (!string.IsNullOrEmpty(extension))
                {
                    extension = extension.ToUpperInvariant();
                    if (extension == ".DDS")
                    {
                        texture = DdsHelper.Load(stream, DdsFlags.ForceRgb | DdsFlags.ExpandLuminance);
                    }
                    else if (extension == ".TGA")
                    {
                        texture = TgaHelper.Load(stream);
                    }
                }

                if (texture == null)
                {
                    // TODO: Register ImagingFactory as service.
                    using (var imagingFactory = new ImagingFactory())
                        texture = WicHelper.Load(imagingFactory, stream, WicFlags.ForceRgb | WicFlags.No16Bpp);
                }
            }

            //Tests(texture);

            // Convert to XNA texture.
            var description = texture.Description;

            if (description.Dimension == TextureDimension.TextureCube)
            {
                var texture2D = new Texture2D(graphicsService.GraphicsDevice, description.Width, description.Height, false, description.Format.ToSurfaceFormat());
                texture2D.SetData(texture.Images[0].Data);
                return(texture2D);
            }
            else
            {
                var texture2D = new Texture2D(graphicsService.GraphicsDevice, description.Width, description.Height, description.MipLevels > 1, description.Format.ToSurfaceFormat());
                for (int i = 0; i < texture.Description.MipLevels; i++)
                {
                    texture2D.SetData(i, null, texture.Images[i].Data, 0, texture.Images[i].Data.Length);
                }

                return(texture2D);
            }
        }
Exemplo n.º 2
0
 public static Texture2D LoadTexture2D(Device device, string fileName, Texture2DLoadOptions options)
 {
     using (var bitmapSource = WicHelper.LoadBitmapSourceFromFile(fileName)) {
         return(CreateTexture2DFromBitmapSource(device, bitmapSource, options));
     }
 }
Exemplo n.º 3
0
 private static Bitmap LoadBitmap(SharpDX.Direct2D1.RenderTarget target, string fileName)
 {
     using (var bmp = WicHelper.LoadBitmapSourceFromFile(fileName)) {
         return(Bitmap.FromWicBitmap(target, bmp));
     }
 }