Recently, I used WPF + xNa to create an editor. Because xNa is embedded in WPF, it is directly loaded using texture2d. fromstream instead of using contentmanager to load textures. However, when a translucent PNG image is loaded using texture2d. fromstream, it is found that the texture is inconsistent with the source image. In particular, the translucent information is basically lost, and the image becomes a mess.
Effect
From left to right: the results are processed after contentmanager is used for loading, texture2d. fromstream is used for loading, and texture2d. fromstream is used for loading.
Visible for translucent PNG images, if texture2d. fromstream is used for direct rendering after loading, the effect is very poor and must be processed. We can use the following static method for processing:
1 Private Static Void Premultiplyalphas (texture2d RET)
2 {
3 Byte4 [] DATA = New Byte4 [ret. Width * ret. Height];
4 Ret. getdata <byte4> (data );
5 For ( Int I = 0 ; I <data. length; I ++)
6 {
7 Vector4 VEC = data [I]. tovector4 ();
8 Float Alpha = Vec. W/ 255.0f ;
9 Int A = ( Int ) (VEC. W );
10 Int R = ( Int ) (Alpha * Vec. X );
11 Int G = ( Int ) (Alpha * Vec. y );
12 Int B = ( Int ) (Alpha * Vec. z );
13 Uint Packed = ( Uint )(
14 (A < 24 ) +
15 (B < 16 ) +
16 (G < 8 ) +
17 R
18 );
19
20 Data [I]. packedvalue = packed;
21 }
22 Ret. setdata <byte4> (data );
23 }
The methods are found on the msdn forum.