C# Class Bloom.ImageProcessing.ImageUtils

Show file Open project: BloomBooks/BloomDesktop

Public Methods

Method Description
AppearsToBeJpeg ( SIL.Windows.Forms.ImageToolbox.PalasoImage imageInfo ) : bool
GetImageFromFile ( string path ) : Image

Read a bitmap image from a file. The file must be known to exist before calling this method.

Image.FromFile and Image.FromStream lock the file until the image is disposed of. Therefore, we copy the image and dispose of the original. On Windows, Image.FromFile leaks file handles, so we use FromStream instead. For details, see the last answer to http://stackoverflow.com/questions/16055667/graphics-drawimage-out-of-memory-exception

ProcessAndSaveImageIntoFolder ( SIL.Windows.Forms.ImageToolbox.PalasoImage imageInfo, string bookFolderPath, bool isSameFile ) : string

Makes the image a png if it's not a jpg and saves in the book's folder. If the image has a filename, replaces any file with the same name. WARNING: imageInfo.Image could be replaced (causing the original to be disposed)

RemoveTransparencyOfImagesInFolder ( string folderPath, IProgress progress ) : void
SaveAsTopQualityJpeg ( Image image, string destinationPath ) : void

Save the image (of any format) to a jpeg file with 100 quality Note that this is still going to introduce some errors if the input is a bitmap.

Will throw if the destination is locked and the user tells us to give up.

ShouldChangeFormatToJpeg ( Image image ) : bool

When images are copied from LibreOffice, images that were jpegs there are converted to bitmaps for the clipboard. So when we just saved them as bitmaps (pngs), we dramatically inflated the size of user's image files (and this then led to memory problems). So the idea here is just to try and detect that we should would be better off saving the image as a jpeg. Note that even at 100%, we're still going to lose some quality. So this method is only going to recommend doing that if the size would be at least 50% less.

Private Methods

Method Description
CreateImageWithoutTransparentBackground ( Image image ) : Image
DrawImageWithWhiteBackground ( Image source, Bitmap target ) : void
GetFileNameToUseForSavingImage ( string bookFolderPath, SIL.Windows.Forms.ImageToolbox.PalasoImage imageInfo, bool isJpeg ) : string
LogMemoryUsage ( ) : void
RemoveTransparency ( Image original, string path, IProgress progress ) : void

Method Details

AppearsToBeJpeg() public static method

public static AppearsToBeJpeg ( SIL.Windows.Forms.ImageToolbox.PalasoImage imageInfo ) : bool
imageInfo SIL.Windows.Forms.ImageToolbox.PalasoImage
return bool

GetImageFromFile() public static method

Read a bitmap image from a file. The file must be known to exist before calling this method.
Image.FromFile and Image.FromStream lock the file until the image is disposed of. Therefore, we copy the image and dispose of the original. On Windows, Image.FromFile leaks file handles, so we use FromStream instead. For details, see the last answer to http://stackoverflow.com/questions/16055667/graphics-drawimage-out-of-memory-exception
public static GetImageFromFile ( string path ) : Image
path string
return Image

ProcessAndSaveImageIntoFolder() public static method

Makes the image a png if it's not a jpg and saves in the book's folder. If the image has a filename, replaces any file with the same name. WARNING: imageInfo.Image could be replaced (causing the original to be disposed)
public static ProcessAndSaveImageIntoFolder ( SIL.Windows.Forms.ImageToolbox.PalasoImage imageInfo, string bookFolderPath, bool isSameFile ) : string
imageInfo SIL.Windows.Forms.ImageToolbox.PalasoImage
bookFolderPath string
isSameFile bool
return string

RemoveTransparencyOfImagesInFolder() public static method

public static RemoveTransparencyOfImagesInFolder ( string folderPath, IProgress progress ) : void
folderPath string
progress IProgress
return void

SaveAsTopQualityJpeg() public static method

Save the image (of any format) to a jpeg file with 100 quality Note that this is still going to introduce some errors if the input is a bitmap.
Will throw if the destination is locked and the user tells us to give up.
public static SaveAsTopQualityJpeg ( Image image, string destinationPath ) : void
image Image
destinationPath string
return void

ShouldChangeFormatToJpeg() public static method

When images are copied from LibreOffice, images that were jpegs there are converted to bitmaps for the clipboard. So when we just saved them as bitmaps (pngs), we dramatically inflated the size of user's image files (and this then led to memory problems). So the idea here is just to try and detect that we should would be better off saving the image as a jpeg. Note that even at 100%, we're still going to lose some quality. So this method is only going to recommend doing that if the size would be at least 50% less.
public static ShouldChangeFormatToJpeg ( Image image ) : bool
image Image
return bool