public static void toWebP(Bitmap bmp, string outputpath, int quality) { using (WebP webp = new WebP()) { webp.Save(bmp, outputpath, quality); } }
/// <summary>resize the webp image</summary> /// <param name="sourceUrl">valid source image url</param> /// <param name="destPath">destination image path that saved image there</param> /// <param name="width">width that image resized to them</param> /// <param name="height">height that image resized to them</param> /// <param name="compress">compress image if that true</param> /// <returns>return true if do correctly else return false</returns> public static bool ResizeWebPFromWeb(string sourceUrl, string destPath, int width, int height, bool compress = false) { try { if (String.IsNullOrEmpty(sourceUrl) || String.IsNullOrEmpty(destPath) || width <= 0 || height <= 0) { return(false); } using (WebP webp = new WebP()) { var stream = Helper.DownloadImage(sourceUrl); var webp_byte = Helper.ReadByte(stream); Bitmap bmp; if (compress) { bmp = webp.GetThumbnailFast(webp_byte, width, height); } else { bmp = webp.GetThumbnailQuality(webp_byte, width, height); } webp.Save(bmp, destPath); } return(true); } catch { throw; } }
public static bool WriteWebP(Bitmap imageToConvert, string outputPath) { bool success = false; using (WebP webp = new WebP()) webp.Save(imageToConvert, outputPath, ExportSettings.imageQuality); success = true; return(success); }
/// <summary> /// 画像ファイルをWebPファイル形式に変換する /// </summary> /// <param name="imagePath"></param> /// <param name="quality"></param> /// <param name="isDeleteSourceFile"></param> /// <returns></returns> private static bool imageToWebp(string imagePath, int quality, bool isDeleteSourceFile) { var dirPath = Path.GetDirectoryName(imagePath); if (dirPath == null) { Log.Warn($"画像ファイルパスはフルパスで指定してください file={imagePath}"); return(false); } var fileName = ""; try { var outputPath = Path.Combine(dirPath, Path.GetFileNameWithoutExtension(imagePath) + ".webp"); if (File.Exists(outputPath)) { Log.Warn($"WebPファイルが既に存在するので削除 file={outputPath}"); File.Delete(outputPath); } fileName = Path.GetFileName(imagePath); Log.Info($"WebP変換開始 file={fileName}"); using (var bitmap = new Bitmap(imagePath)) { using var convBitmap = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height) , System.Drawing.Imaging.PixelFormat.Format32bppArgb); using WebP webp = new WebP(); webp.Save(convBitmap, outputPath, quality); } Log.Info($"WebP変換終了 file={fileName}"); if (isDeleteSourceFile) { File.Delete(imagePath); } } catch (Exception e) { Log.Error(e, $"WebP変換例外 file={fileName}"); return(false); } return(true); }
/// <summary>convert png image to webp</summary> /// <param name="sourceUrl">valid source image url</param> /// <param name="destPath">destination image path that saved image there</param> /// <param name="quality">quality of converted image, between 0 and 100 <para>min quality : 0 </para><para>max quality : 100</para></param> /// <returns>return true if do correctly else return false</returns> public static bool PngToWebPFromWeb(string sourceUrl, string destPath, int quality = 100) { try { if (String.IsNullOrEmpty(sourceUrl) || String.IsNullOrEmpty(destPath)) { return(false); } if (quality <= 0 || quality > 100) { quality = 100; } using (WebP webp = new WebP()) { Bitmap bmp = new Bitmap(Helper.DownloadImage(sourceUrl)); webp.Save(bmp, destPath, quality); } return(true); } catch { throw; } }
/// <summary>convert jpeg image to webp</summary> /// <param name="sourcePath">valid source image path</param> /// <param name="destPath">destination image path that saved image there</param> /// <param name="quality">quality of converted image, between 0 and 100 <para>min quality : 0 </para><para>max quality : 100</para></param> /// <returns>return true if do correctly else return false</returns> public static bool JpegToWebP(string sourcePath, string destPath, int quality = 100) { try { if (String.IsNullOrEmpty(sourcePath) || String.IsNullOrEmpty(destPath)) { return(false); } if (quality <= 0 || quality > 100) { quality = 100; } using (WebP webp = new WebP()) { Bitmap bmp = new Bitmap(sourcePath); webp.Save(bmp, destPath, quality); } return(true); } catch { throw; } }
/// <summary>convert png image to webp and resize image</summary> /// <param name="sourceUrl">valid source image url</param> /// <param name="destPath">destination image path that saved image there</param> /// <param name="width">width that image resized to them</param> /// <param name="height">height that image resized to them</param> /// <param name="quality">quality of converted image, between 0 and 100 <para>min quality : 0 </para><para>max quality : 100</para></param> /// <returns>return true if do correctly else return false</returns> public static bool PngToWebPFromWeb(string sourceUrl, string destPath, int width, int height, int quality = 100) { try { if (String.IsNullOrEmpty(sourceUrl) || String.IsNullOrEmpty(destPath) || width <= 0 || height <= 0) { return(false); } if (quality <= 0 || quality > 100) { quality = 100; } using (WebP webp = new WebP()) { Bitmap bmp = new Bitmap(Helper.DownloadImage(sourceUrl)); var webp_byte = webp.EncodeLossless(bmp); bmp = webp.GetThumbnailQuality(webp_byte, width, height); webp.Save(bmp, destPath, quality); } return(true); } catch { throw; } }
public static void Save(Bitmap bmp, string path) { using (WebP webp = new WebP()) webp.Save(bmp, path); }