@@ -34,9 +34,9 @@ public static Bitmap ResizeBitmap(Bitmap image, int width, int height)
3434 return destImage ;
3535 }
3636
37- public static Bitmap ConvertBitmapToFormat32bppArgb ( Bitmap bitmap )
37+ public static Bitmap ConvertBitmapToFormat ( Bitmap bitmap , PixelFormat format )
3838 {
39- Bitmap target = new Bitmap ( bitmap . Width , bitmap . Height , PixelFormat . Format32bppArgb ) ;
39+ Bitmap target = new Bitmap ( bitmap . Width , bitmap . Height , format ) ;
4040 target . SetResolution ( bitmap . HorizontalResolution , bitmap . VerticalResolution ) ; // Set both bitmap to same dpi to prevent scaling.
4141 using ( Graphics g = Graphics . FromImage ( target ) )
4242 {
@@ -46,8 +46,18 @@ public static Bitmap ConvertBitmapToFormat32bppArgb(Bitmap bitmap)
4646 return target ;
4747 }
4848
49+ /// <summary>
50+ /// Split Format32bppArgb bitmap into two Format24bppRgb bitmap, one contains the RGB channels and one contains the alpha channel in the B channel.
51+ /// </summary>
52+ /// <param name="input">Format32bppArgb bitmap</param>
53+ /// <param name="rgb">Format24bppRgb bitmap containing RGB channels.</param>
54+ /// <param name="alpha">Format24bppRgb bitmap containint alpha channel of the original image in the B channel.</param>
4955 public static void SplitChannel ( Bitmap input , out Bitmap rgb , out Bitmap alpha )
5056 {
57+ if ( input . PixelFormat != PixelFormat . Format32bppArgb )
58+ {
59+ throw new FormatException ( ) ;
60+ }
5161 rgb = new Bitmap ( input . Width , input . Height , PixelFormat . Format24bppRgb ) ;
5262 alpha = new Bitmap ( input . Width , input . Height , PixelFormat . Format24bppRgb ) ;
5363 var inputData = input . LockBits ( new Rectangle ( 0 , 0 , input . Width , input . Height ) , ImageLockMode . ReadOnly , input . PixelFormat ) ;
@@ -78,8 +88,18 @@ public static void SplitChannel(Bitmap input, out Bitmap rgb, out Bitmap alpha)
7888 }
7989 }
8090
91+ /// <summary>
92+ /// Combine RGB image and alpha image as one ARGB image.
93+ /// </summary>
94+ /// <param name="rgb">Format24bppRgb bitmap containing the RGB channels.</param>
95+ /// <param name="alpha">Format24bppRgb bitmap containng the alpha channels in the B channel.</param>
96+ /// <returns></returns>
8197 public static Bitmap CombineChannel ( Bitmap rgb , Bitmap alpha )
8298 {
99+ if ( rgb . PixelFormat != PixelFormat . Format24bppRgb || alpha . PixelFormat != PixelFormat . Format24bppRgb )
100+ {
101+ throw new FormatException ( ) ;
102+ }
83103 var output = new Bitmap ( rgb . Width , rgb . Height , PixelFormat . Format32bppArgb ) ;
84104 var outputData = output . LockBits ( new Rectangle ( 0 , 0 , output . Width , output . Height ) , ImageLockMode . WriteOnly , output . PixelFormat ) ;
85105 var rgbData = rgb . LockBits ( new Rectangle ( 0 , 0 , rgb . Width , rgb . Height ) , ImageLockMode . ReadOnly , rgb . PixelFormat ) ;
0 commit comments