@@ -20,6 +20,7 @@ public class ImageConversionHandler
2020 private static BmpBitmapEncoder bmpEncoder ;
2121 private static GifBitmapEncoder gifEncoder ;
2222
23+ private static string tempImgPath = null ;
2324 private static string imageName ; //name of the image to convert
2425 private static string directoryOfImageToConvert ; //directory of the image to convert
2526 private static int color = 0 ; //color to replace the transparency with
@@ -34,18 +35,18 @@ public static bool IsImage(string pathOfFile)
3435 return false ;
3536 }
3637
38+ /// <summary>
39+ /// Starts the conversion of one or more image to the specified format
40+ /// </summary>
41+ /// <param name="format"> format to which convert the image</param>
42+ /// <param name="pathsOfImagesToConvert"> path of the image to convert </param>
43+ /// <param name="gifRepeatTimes"> the times the gif shall repeat: infinite(0)-10 </param>
44+ /// <param name="colorToReplTheTranspWith"> Color to replace the transparency of a png image with</param>
45+ /// <param name="delayTime"> delay between two frames of a gif</param>
46+ /// <returns></returns>
3747 public static async Task < List < bool > > StartConversion ( string format , string [ ] pathsOfImagesToConvert , int gifRepeatTimes , int colorToReplTheTranspWith , int delayTime )
3848 {
39- if ( colorToReplTheTranspWith != 0 )
40- {
41- MessageBox . Show ( "Replace transparency not implemented yet" , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
42- var list = new List < bool > ( ) ;
43- list . Add ( false ) ;
44- return list ;
45-
46- color = colorToReplTheTranspWith ;
47- }
48-
49+ color = colorToReplTheTranspWith ;
4950 conversionsResults = new List < bool > ( ) ;
5051 foreach ( var imageToConvertPath in pathsOfImagesToConvert )
5152 {
@@ -137,16 +138,29 @@ private static async Task<bool> ToJpegOrJpg(string pathOfImage, string format)
137138 using ( Stream st = File . OpenRead ( pathOfImage ) )
138139 {
139140 var imageToConv = new BitmapImage ( ) ;
140- imageToConv . BeginInit ( ) ;
141- imageToConv . StreamSource = st ;
142- imageToConv . CacheOption = BitmapCacheOption . OnLoad ;
143- imageToConv . EndInit ( ) ;
144- jpegOrJpgEncoder . Frames . Add ( BitmapFrame . Create ( imageToConv ) ) ;
145-
146- st . Close ( ) ;
147- } //loads image to convert from a stream and converts it
141+ if ( color != 0 )
142+ {
143+ Image imgToConvertImage = Image . FromStream ( st ) ;
144+ using ( Stream st2 = File . OpenRead ( ReplaceTransparency ( imgToConvertImage ) ) )
145+ {
146+ imageToConv . BeginInit ( ) ;
147+ imageToConv . StreamSource = st2 ;
148+ imageToConv . CacheOption = BitmapCacheOption . OnLoad ;
149+ imageToConv . EndInit ( ) ;
150+ jpegOrJpgEncoder . Frames . Add ( BitmapFrame . Create ( imageToConv ) ) ;
151+ }
152+ } //if the user selected a color to convert the image transparency with
153+ else
154+ {
155+ imageToConv . BeginInit ( ) ;
156+ imageToConv . StreamSource = st ;
157+ imageToConv . CacheOption = BitmapCacheOption . OnLoad ;
158+ imageToConv . EndInit ( ) ;
159+ jpegOrJpgEncoder . Frames . Add ( BitmapFrame . Create ( imageToConv ) ) ;
160+ }
161+ } //loads image to convert from a stream, eventually replace the transparency, and converts it
148162
149- #region Saves image based on format(jpeg or jpg) and checks wether it was saved correctly
163+ #region Saves image based on format(jpeg or jpg), eventually deletes temp file, and checks wether it was saved correctly
150164 if ( format == "jpeg" )
151165 {
152166 using ( Stream st = File . Create ( $ "{ directoryOfImageToConvert } \\ { imageName } .jpeg") )
@@ -164,6 +178,9 @@ private static async Task<bool> ToJpegOrJpg(string pathOfImage, string format)
164178 }
165179 }
166180
181+ if ( tempImgPath != null ) //deletes the temporary file in the temp folder(the image with the transparency replaced but still not converted)
182+ File . Delete ( tempImgPath ) ;
183+
167184 if ( File . Exists ( $ "{ directoryOfImageToConvert } \\ { imageName } .jpeg") || File . Exists ( $ "{ directoryOfImageToConvert } \\ { imageName } .jpg") )
168185 {
169186 return true ;
@@ -185,14 +202,27 @@ private static async Task<bool> ToBmp(string pathOfImage)
185202 using ( Stream st = File . OpenRead ( pathOfImage ) )
186203 {
187204 var imageToConv = new BitmapImage ( ) ;
188- imageToConv . BeginInit ( ) ;
189- imageToConv . StreamSource = st ;
190- imageToConv . CacheOption = BitmapCacheOption . OnLoad ;
191- imageToConv . EndInit ( ) ;
192-
193- bmpEncoder . Frames . Add ( BitmapFrame . Create ( imageToConv ) ) ;
194- st . Close ( ) ;
195- } //loads image to convert from a stream and converts it
205+ if ( color != 0 )
206+ {
207+ Image imgToConvertImage = Image . FromStream ( st ) ;
208+ using ( Stream st2 = File . OpenRead ( ReplaceTransparency ( imgToConvertImage ) ) )
209+ {
210+ imageToConv . BeginInit ( ) ;
211+ imageToConv . StreamSource = st2 ;
212+ imageToConv . CacheOption = BitmapCacheOption . OnLoad ;
213+ imageToConv . EndInit ( ) ;
214+ bmpEncoder . Frames . Add ( BitmapFrame . Create ( imageToConv ) ) ;
215+ }
216+ } //if the user selected a color to convert the image transparency with
217+ else
218+ {
219+ imageToConv . BeginInit ( ) ;
220+ imageToConv . StreamSource = st ;
221+ imageToConv . CacheOption = BitmapCacheOption . OnLoad ;
222+ imageToConv . EndInit ( ) ;
223+ bmpEncoder . Frames . Add ( BitmapFrame . Create ( imageToConv ) ) ;
224+ }
225+ } //loads image to convert from a stream, eventually replace the transparency, and converts it
196226
197227 #region saves bmp image and checkes whether it was saved correctly
198228 using ( Stream st = File . Create ( $ "{ directoryOfImageToConvert } \\ { imageName } .bmp") )
@@ -249,7 +279,7 @@ private static async Task<bool> ImagesToGif(string[] imagesPaths, int repeatTime
249279 int a = 1 ;
250280 for ( int i = 0 ; i < fileBytesList . Count ; i ++ )
251281 {
252- if ( fileBytesList [ i ] == 44 && fileBytesList [ i + 1 ] == 0 && fileBytesList [ i + 2 ] == 0 && fileBytesList [ i + 3 ] == 0 && fileBytesList [ i + 4 ] == 0 )
282+ if ( fileBytesList [ i ] == 44 && fileBytesList [ i + 1 ] == 0 && fileBytesList [ i + 2 ] == 0 && fileBytesList [ i + 3 ] == 0 && fileBytesList [ i + 4 ] == 0 )
253283 {
254284 System . Diagnostics . Debug . WriteLine ( $ "Found start of image descriptor block at index: { i } .\n Image descriptor n°{ a } ") ;
255285 fileBytesList . InsertRange ( i , graphicExtension ) ;
@@ -282,7 +312,7 @@ private static async Task<bool> ImagesToGif(string[] imagesPaths, int repeatTime
282312 } //otherwise: return false
283313 }
284314
285- private static async Task < bool > ToIconOrCur ( string imgToConvertPath , string format )
315+ private static async Task < bool > ToIconOrCur ( string imgToConvertPath , string format ) //TODO: Fix conversion of bmp images to ico or cur
286316 {
287317 var ext = Path . GetExtension ( imgToConvertPath ) . ToLower ( ) ;
288318 #region if the image to convert isn't a png or bmp image it can't be converterd: return null
@@ -348,7 +378,18 @@ private static async Task<bool> ToIconOrCur(string imgToConvertPath, string form
348378 #endregion
349379 #region Image data
350380 if ( ext == ".png" )
351- imgToConvert . Save ( memStream , ImageFormat . Png ) ;
381+ {
382+ if ( color != 0 )
383+ {
384+ using ( Stream st = File . OpenRead ( ReplaceTransparency ( imgToConvert ) ) )
385+ {
386+ var imgToConvWithReplacedTransp = Image . FromStream ( st ) ;
387+ imgToConvWithReplacedTransp . Save ( memStream , ImageFormat . Png ) ;
388+ }
389+ } //if the user selected a color to convert the image transparency with
390+ else
391+ imgToConvert . Save ( memStream , ImageFormat . Png ) ;
392+ }
352393 else
353394 {
354395 byte [ ] bmpBytes = File . ReadAllBytes ( imgToConvertPath ) ;
@@ -406,9 +447,34 @@ private static async Task<bool> ToIconOrCur(string imgToConvertPath, string form
406447 #endregion
407448 }
408449
409- private static async Task < BitmapImage > ReplaceTransparency ( Image img )
450+ private static string ReplaceTransparency ( Image img )
410451 {
411- return new BitmapImage ( ) ;
452+ Bitmap imgWithTranspReplaced = new Bitmap ( img . Width , img . Height ) ;
453+ Graphics g = Graphics . FromImage ( imgWithTranspReplaced ) ;
454+
455+ if ( color == 1 )
456+ {
457+ g . Clear ( Color . White ) ;
458+ } //replace transparency with white
459+ else
460+ {
461+ g . Clear ( Color . Black ) ;
462+ } //replace transparency with black
463+ g . DrawImage ( img , 0 , 0 ) ;
464+
465+ string tempPath = Path . GetTempPath ( ) ;
466+
467+ #region Creates folder of ImageConverter in temp folder, saves imgWithTranspReplaced in it and returns its path
468+ if ( ! Directory . Exists ( $ "{ tempPath } \\ ImageConverter") ) //if the temp folder of ImageConverter doesn't exist create it
469+ Directory . CreateDirectory ( $ "{ tempPath } \\ ImageConverter") ;
470+
471+ imgWithTranspReplaced . Save ( $ "{ tempPath } \\ ImageConverter\\ tempImgWithTranspReplaced.png") ; //save image, return its path and dispose objects
472+ tempImgPath = $ "{ tempPath } \\ ImageConverter\\ tempImgWithTranspReplaced.png";
473+ imgWithTranspReplaced . Dispose ( ) ;
474+ g . Dispose ( ) ;
475+
476+ return tempImgPath ;
477+ #endregion
412478 }
413479 }
414480}
0 commit comments