How to Use texture2d. fromstream to load PNG images in xna4.0

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.