SharpDX load a texture in Windows Phone 8

天涯浪子 提交于 2019-12-11 07:42:55

问题


I'm creating a Windows Phone 8 app and I'm using SharpDX to load a texture.

I've tried to load PNG using Content.Load<Texture2D>("filename.png") but I've got errors and I've realized that SharpDX in Windows Phone 8 only accepts DDS textures and doesn't accept PNG/JPEG/BMP etc. I've converted my PNG to DDS using texconv filename.png and added the file to my solution just like other files that I'm using in my project (that I can load correctly). When I run my app, I'm getting an exception:

{SharpDX.Serialization.InvalidChunkException: Unexpected chunk [DDS /0x20534444] instead of [TKFX/0x58464B54] at SharpDX.Serialization.BinarySerializer.BeginChunk(FourCC chunkId)}

Well, I've converted my valid PNG to DDS using the texconv tool. From what I see, it's a file format error, and the fourCC code corresponds to DDS, which is as it should be. But SharpDX expects TKFX format (toolkit effect?) instead of DDS. Why? I am trying to load a texture with Content.Load<Texture2D> not Content.Load<Effect> (I also have it to load a shader and it works perfectly).

If anyone finds out how to load a PNG instead of DDS, that's even better!


回答1:


In case you still want to load PNG images, I'll just leave this code snippet here:

public static Texture2D FromImage(BitmapImage image, GraphicsDevice device)
{
    WriteableBitmap bitmap = new WriteableBitmap(image);

    return FromImageData(bitmap.Pixels, bitmap.PixelWidth, 
                            bitmap.PixelHeight, device);
}

public static Texture2D FromImageData(int[] data, int width, int height, GraphicsDevice device)
{
    Texture2D texture = Texture2D.New(device, width, height,
                                    PixelFormat.B8G8R8A8.UNorm);

    texture.SetData<int>(data);

    return texture;
}



回答2:


I've figured the problem out. I've switched the order of the lines of loading my texture and loading my shader. This time, the texture loaded correctly, and my shader failed with this error:

{SharpDX.Serialization.InvalidChunkException: Unexpected chunk [TKFX/0x58464B54] instead of [TKTX/0x58544B54] at SharpDX.Serialization.BinarySerializer.BeginChunk(FourCC chunkId)}

It was expecting TKTX (possibly, toolkit texture) but got TKFX which is a shader. I don't currently have a solution for this misbehavior of ContentManager, but it wasn't about the DDS file.



来源:https://stackoverflow.com/questions/17682622/sharpdx-load-a-texture-in-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!