11using System ;
22using System . Drawing ;
33using System . Drawing . Imaging ;
4+ using System . Reflection ;
45using System . Runtime . InteropServices ;
56
67namespace ArcanumTextureSlicer
78{
89 public static class BitmapExtensions
910 {
11+ private static Bitmap _sampleTile ;
12+
13+ public static readonly Color AlphaColor = Color . Blue ;
14+
15+ public static Bitmap SampleTile =>
16+ _sampleTile ??
17+ ( _sampleTile = new Bitmap ( Assembly . GetExecutingAssembly ( )
18+ . GetManifestResourceStream ( "ArcanumTextureSlicer.Resources.SampleTile.png" ) ) ) ;
19+
20+ public static Bitmap CreateTile ( this Bitmap source , int x , int y )
21+ {
22+ var tile = CloneRegion ( source , new Rectangle ( x , y , SampleTile . Width , SampleTile . Height ) ) ;
23+ tile . DrawAlpha ( SampleTile ) ;
24+ return tile ;
25+ }
26+
1027 public static Bitmap CloneRegion ( this Bitmap source , Rectangle rect )
1128 {
1229 if ( rect . X >= 0 && rect . Y >= 0 && rect . Right <= source . Width && rect . Bottom <= source . Height )
1330 {
1431 return source . Clone ( rect , source . PixelFormat ) ;
1532 }
1633 var bitmap = source . Clone ( new Rectangle ( 0 , 0 , rect . Width , rect . Height ) , source . PixelFormat ) ;
17- bitmap . SetColor ( Color . Blue ) ;
34+ bitmap . SetColor ( AlphaColor ) ;
1835 bitmap . DrawImage ( source ,
1936 Math . Max ( - rect . X , 0 ) ,
2037 Math . Max ( - rect . Y , 0 ) ,
@@ -38,6 +55,42 @@ public static void SetColor(this Bitmap canvas, Color color)
3855 canvas . UnlockBits ( data ) ;
3956 }
4057
58+ public static void DrawAlpha ( this Bitmap canvas , Bitmap sample )
59+ {
60+ var rect = new Rectangle ( 0 , 0 , canvas . Width , canvas . Height ) ;
61+
62+ var sampleData = sample . LockBits ( rect , ImageLockMode . ReadOnly , sample . PixelFormat ) ;
63+ var canvasData = canvas . LockBits ( rect , ImageLockMode . WriteOnly , canvas . PixelFormat ) ;
64+ try
65+ {
66+ var sampleAlphaIndex = sample . GetColorIndex ( AlphaColor ) ;
67+ var canvasAlphaIndex = canvas . GetColorIndex ( AlphaColor ) ;
68+
69+ var sampleBytes = new byte [ sampleData . Height * sampleData . Stride ] ;
70+ var canvasBytes = new byte [ canvasData . Height * canvasData . Stride ] ;
71+ Marshal . Copy ( sampleData . Scan0 , sampleBytes , 0 , sampleBytes . Length ) ;
72+ Marshal . Copy ( canvasData . Scan0 , canvasBytes , 0 , canvasBytes . Length ) ;
73+
74+ for ( var y = 0 ; y < canvasData . Height ; y ++ )
75+ {
76+ for ( var x = 0 ; x < canvasData . Width ; x ++ )
77+ {
78+ if ( sampleBytes [ x + y * sampleData . Stride ] == sampleAlphaIndex )
79+ {
80+ canvasBytes [ x + y * canvasData . Stride ] = canvasAlphaIndex ;
81+ }
82+ }
83+ }
84+
85+ Marshal . Copy ( canvasBytes , 0 , canvasData . Scan0 , canvasBytes . Length ) ;
86+ }
87+ finally
88+ {
89+ sample . UnlockBits ( sampleData ) ;
90+ canvas . UnlockBits ( canvasData ) ;
91+ }
92+ }
93+
4194 public static void DrawImage ( this Bitmap canvas , Bitmap source , int canvasX , int canvasY , Rectangle sourceRect )
4295 {
4396 var sourceData = source . LockBits ( sourceRect , ImageLockMode . ReadOnly , source . PixelFormat ) ;
0 commit comments