本文介绍了SharpDX在Windows Phone 8中加载纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Windows Phone 8应用,并且正在使用SharpDX加载纹理.

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

我尝试使用Content.Load<Texture2D>("filename.png")加载PNG,但是遇到错误,并且我意识到Windows Phone 8中的SharpDX仅接受DDS纹理,并且不接受PNG/JPEG/BMP等.使用texconv filename.png将我的PNG转换为DDS,并将该文件添加到我的解决方案中,就像我在项目中使用的其他文件一样(可以正确加载).当我运行我的应用程序时,出现异常:

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)}

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

好吧,我已经使用texconv工具将有效的PNG转换为DDS.据我所知,这是一个文件格式错误,fourCC代码对应于DDS,这是应该的.但是SharpDX希望使用TKFX格式(工具包效果?)而不是DDS.为什么?我正在尝试使用Content.Load<Texture2D>而不是Content.Load<Effect>加载纹理(我也可以加载着色器,并且效果很好).

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).

如果有人找到了如何加载PNG而不是DDS的方法,那就更好了!

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

推荐答案

如果您仍然想加载PNG图片,我将在此处保留以下代码段:

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;
}

这篇关于SharpDX在Windows Phone 8中加载纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:56