/// <summary> /// Makes the image png if it's not a jpg, makes white transparent, compresses it, and saves in the book's folder. /// Replaces any file with the same name. /// </summary> /// <returns>The name of the file, now in the book's folder.</returns> private string ProcessAndCopyImage(PalasoImage imageInfo, string bookFolderPath) { var isJpeg = ShouldSaveAsJpeg(imageInfo); try { using (Bitmap image = new Bitmap(imageInfo.Image)) //nb: there are cases (undefined) where we get out of memory if we are not operating on a copy { //photographs don't work if you try to make the white transparent if (!isJpeg && image is Bitmap) { ((Bitmap)image).MakeTransparent(Color.White); //make white look realistic against background } string imageFileName = GetImageFileName(bookFolderPath, imageInfo, isJpeg); var dest = Path.Combine(bookFolderPath, imageFileName); if (File.Exists(dest)) { try { File.Delete(dest); } catch (System.IO.IOException error) { throw new ApplicationException("Bloom could not replace the image " + imageFileName + ", probably because Bloom itself has it locked."); } } image.Save(dest, isJpeg ? ImageFormat.Jpeg : ImageFormat.Png); if (!isJpeg) { using (var dlg = new ProgressDialogBackground()) { dlg.ShowAndDoWork((progress, args) => ImageUpdater.CompressImage(dest, progress)); } } imageInfo.Metadata.Write(dest); return(imageFileName); } } catch (System.IO.IOException) { throw; //these are informative on their own } catch (Exception error) { if (!string.IsNullOrEmpty(imageInfo.FileName) && File.Exists(imageInfo.OriginalFilePath)) { var megs = new System.IO.FileInfo(imageInfo.OriginalFilePath).Length / (1024 * 1000); if (megs > 2) { var msg = string.Format("Bloom was not able to prepare that picture for including in the book. \r\nThis is a rather large image to be adding to a book --{0} Megs--.", megs); if (isJpeg) { msg += "\r\nNote, this file is a jpeg, which is normally used for photographs, not line-drawings (png, tiff, bmp). Bloom can handle smallish jpegs, large ones are difficult to handle, especialy if memory is limitted."; } throw new ApplicationException(msg, error); } } throw new ApplicationException("Bloom was not able to prepare that picture for including in the book. Is it too large, or an odd format?\r\n" + imageInfo.FileName, error); } }