I suggest converting the images to PNG and then using the built in ZIP compression.
public static void SaveToPNG(Bitmap SourceImage, string DestinationPath)
{
SourceImage.Save(DestinationPath, ImageFormat.Png);
CompressFile(DestinationPath, true);
}
private static string CompressFile(string SourceFile, bool DeleteSourceFile)
{
string TargetZipFileName = Path.ChangeExtension(SourceFile, ".zip");
using (ZipArchive archive = ZipFile.Open(TargetZipFileName, ZipArchiveMode.Create))
{
archive.CreateEntryFromFile(SourceFile, Path.GetFileName(SourceFile),CompressionLevel.Optimal);
}
if(DeleteSourceFile == true)
{
File.Delete(SourceFile);
}
return TargetZipFileName;
}
OR your if you do not mind a little unnoticable loss, then you could convert to a JPG at a high quality and then ZIP that. At 100% quality, your users will likely not notice any difference and the lower quality you go, the smaller the images will get, but that defeats your "without loss of quality" condition.
private static ImageCodecInfo __JPEGCodecInfo = null;
private static ImageCodecInfo _JPEGCodecInfo
{
get
{
if (__JPEGCodecInfo == null)
{
__JPEGCodecInfo = ImageCodecInfo.GetImageEncoders().ToList().Find(delegate (ImageCodecInfo codec) { return codec.FormatID == ImageFormat.Jpeg.Guid; });
}
return __JPEGCodecInfo;
}
}
public static void SaveToJPEG(Bitmap SourceImage, string DestinationPath, long Quality)
{
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, Quality);
SourceImage.Save(DestinationPath, _JPEGCodecInfo, parameters);
CompressFile(DestinationPath, true);
}
private static string CompressFile(string SourceFile, bool DeleteSourceFile)
{
string TargetZipFileName = Path.ChangeExtension(SourceFile, ".zip");
using (ZipArchive archive = ZipFile.Open(TargetZipFileName, ZipArchiveMode.Create))
{
archive.CreateEntryFromFile(SourceFile, Path.GetFileName(SourceFile),CompressionLevel.Optimal);
}
if(DeleteSourceFile == true)
{
File.Delete(SourceFile);
}
return TargetZipFileName;
}