1.    ´ھʵ˶λͼĴ
2.    private Bitmap loadCompatibleBitmap(string filename)
3.    { 
4.      System.Type st = this.GetType(); 
5.      Assembly assembly = st.Assembly; 
6.      Stream stream = assembly.GetManifestResourceStream( 
7.    st.Namespace + "." + filename); 
8.    
9.     // Դлλͼ 
10.    Bitmap bitmap = new Bitmap(stream); 
11.    stream.Close(); 
12.    
13.    // ͸ɫͼϽǿʼ 
14.    Color bg_col = bitmap.GetPixel(0,0); 
15.    Bitmap compatible = new Bitmap(bitmap.Size.Width, bitmap.
Size.Height); 
16.    Graphics g = Graphics.FromImage(compatible); 
17.    
18.    // λͼⲿ 
19.    g.Clear(Color.Black); 
20.    
21.    // ɫ 
22.    ImageAttributes ia = new ImageAttributes(); 
23.    ia.SetColorKey(bg_col, bg_col); 
24.    g.DrawImage(bitmap, 
25.    new Rectangle(0, 0, compatible.Size.Width, 
26.    compatible.Size.Height), 
27.    0, 0, 
28.    bitmap.Size.Width, bitmap.Size.Height, 
29.    GraphicsUnit.Pixel, ia); 
30.    
31.    g.Dispose(); 
32.    bitmap.Dispose(); 
33.    
34.    return compatible; 
35.  } 






RGdiGraphics.cs 





1.    using System; 
2.    using System.Drawing; 
3.    using System.Drawing.Imaging; 
4.    using System.Windows.Forms; 
5.     
6.     
7.    //********************************* 
8.    namespace Alfray.Nerdkill.Platform 
9.    { 
10.     publicclass RGdiGraphics: IDisposable 
11.    { 
12.         public RGdiGraphics(Form window_form) 
13.          { 
14.               mWindowForm = window_form; 
15.               CreateOffscreenBuffer(); 
16.          } 
17.     
18.           //********************************* 
19.           publicvoid CreateOffscreenBuffer() 
20.           { 




21.               if (mWindowForm == null) 
22.                    return; 
23.     
24.               if (mOffscreenBuffer != null) 
25.                    mOffscreenBuffer.Dispose(); 
26.     
27.               Size = mWindowForm.Size; 
28.               mOffscreenBuffer = new Bitmap(sz.Width, sz.Height); 
29.           } 
30.     
31.    //***************************************************** 
32.    public Object CreateNativeBuffer(int width, int height) 
33.    { 
34.         returnnew Bitmap(width, height); 
35.    } 
36.     
37.    
38.    //*************************************************** 
39.    public Object CreateNativeBuffer(Bitmap bitmap,
40.                                               Rectangle crop_area, 
41.                                               bool transparent) 
42.    { 
43.        Color bg_col = bitmap.GetPixel(0,0); 
44.    
45.        int srcX = 0; 
46.        int srcY = 0; 
47.        int width = bitmap.Size.Width; 
48.        int height = bitmap.Size.Height; 
49.    
50.        if (!crop_area.IsEmpty) 
51.        { 
52.            srcX = crop_area.X; 
53.            srcY = crop_area.Y; 
54.            width = crop_area.Width; 
55.            height = crop_area.Height; 
56.        } 
57.    
58.        Bitmap compatible = new Bitmap(width, height); 
59.        Graphics g = Graphics.FromImage(compatible); 
60.    
61.    
62.        if (transparent) 
63.        { 
64.            g.Clear(kTransparentColor); 
65.            ImageAttributes ia = new ImageAttributes(); 
66.              ia.SetColorKey(bg_col, bg_col); 
67.    
68.              Rectangle full = new Rectangle(0, 0, width, height); 
69.    
70.              g.DrawImage(bitmap, full, 
71.                  srcX, srcY, width, height, 
72.                  GraphicsUnit.Pixel, ia); 
73.           } 
74.           else 
75.          { 
76.              g.DrawImage(bitmap, 0, 0); 
77.          } 
78.    
79.              g.Dispose(); 
80.    
81.              return compatible; 
82.          } 
83.    
84.    
85.           //************************************ 
86.           public void DisposeBuffer(Object data) 
87.           { 
88.               if (data != null && data is IDisposable) 
89.                     (data as IDisposable).Dispose(); 
90.           } 
91.    
92.           //******************************** 
93.            public void PaintOffscreenBuffer() 
94.           { 
95.               PaintOffscreenBuffer(Rectangle.Empty); 
96.           } 
97.    
98.    
99.           //************************************************** 
100.          public void PaintOffscreenBuffer(Rectangle clipRect) 
101.         { 
102.             if (mOffscreenBuffer == null) 
103.                 return; 
104.    
105.             Graphics g = mWindowForm.CreateGraphics(); 
106.    
107.             int w = mOffscreenBuffer.Width; 
108.             int h = mOffscreenBuffer.Height; 
109.    
110.             Rectangle source = new Rectangle(0, 0, w, h); 
111.             Rectangle dest = new Rectangle(0, 0, w, h); 
112.    
113.             if (!clipRect.IsEmpty) 
114.             { 
115.                 source.Intersect(clipRect); 
116.                 dest.Intersect(clipRect); 
117.             } 
118.    
119.             g.DrawImage(mOffscreenBuffer, dest, source, 
GraphicsUnit.Pixel); 
120.    
121.             g.Dispose(); 
122.         } 
123.    
124.         //******************************************* 
125.         public bool DrawSprite(Engine.RSprite sprite, 
126.             Rectangle vscreen, 
127.             int destX, int destY) 
128.         { 
129.             if (mOffscreenBuffer == null || sprite == null) 
130.                 return false; 
131.    
132.             Image img = sprite.SpriteData as Image; 
133.    
134.             if (img == null) 
135.                 return false; 
136.    
137.             int width = sprite.Size.Width; 
138.             int height = sprite.Size.Height; 
139.             Rectangle rdest = new Rectangle(destX+vscreen.X, 
destY+vscreen.Y, width, height); 
140.             Rectangle rsrc = new Rectangle(sprite.Origin.X, 
sprite.Origin.Y, width, height); 
141.    
142.             Rectangle rscreen = new Rectangle
(0, 0, mOffscreenBuffer.Size.Width, mOffscreenBuffer.Size.Height); 
143.             vscreen.Intersect(rscreen); 
144.             if (vscreen != rdest && !vscreen.Contains(rdest)) 
145.             { 
146.                 destX = rdest.X; 
147.                 destY = rdest.Y; 
148.                 rdest.Intersect(vscreen); 
149.                 rsrc.Offset(rdest.X-destX, rdest.Y-destY); 
150.                 rsrc.Size = rdest.Size; 
151.             } 
152.    
153.             if (rdest.Width <= 0 || rdest.Height <= 0 || rsrc.Width <= 0 || rsrc.Height <= 0) 
154.                  return true; 
155.    
156.             Graphics g = Graphics.FromImage(mOffscreenBuffer); 
157.    
158.             if (sprite.IsTransparent) 
159.             { 
160.                 ImageAttributes ia = new ImageAttributes(); 
161.                 ia.SetColorKey(kTransparentColor, kTransparent
Color);
162.    
163.                 g.DrawImage(img, 
164.                     rdest, 
165.                     rsrc.X, rsrc.Y, rsrc.Width, rsrc.Height, 
166.                     GraphicsUnit.Pixel, 
167.                     ia); 
168.             } 
169.             else 
170.             { 
171.                 Rectangle dest = new Rectangle(destX, destY, 
172.                     width, height); 
173.    
174.                 g.DrawImage(img, rdest, rsrc, GraphicsUnit.Pixel); 
175.             } 
176.    
177.             g.Dispose(); 
178.             return true; 
179.         } 
180.    
181.    
182.         private Color kTransparentColor = Color.Black;
183.     
184.         private Form  mWindowForm;  
185.         private Bitmap mOffscreenBuffer;  
186.    
187.    } // class class RGdiGraphics 
188.  } // namespace Alfray.Nerdkill.Platform 
