/// <summary>convert webp image to png</summary> /// <param name="sourceUrl">valid source image path</param> /// <param name="destPath">destination image path that saved image there</param> /// <param name="compress">compress image if that true</param> /// <returns>return true if do correctly else return false</returns> public static bool WebPToPng(string sourcePath, string destPath, bool compress = false) { try { if (String.IsNullOrEmpty(sourcePath) || String.IsNullOrEmpty(destPath)) { return(false); } using (WebP webp = new WebP()) { var bmp = webp.Load(sourcePath); var webp_byte = webp.LoadByte(sourcePath); if (compress) { bmp = webp.GetThumbnailFast(webp_byte, bmp.Width, bmp.Height); } else { bmp = webp.GetThumbnailQuality(webp_byte, bmp.Width, bmp.Height); } bmp.Save(destPath, System.Drawing.Imaging.ImageFormat.Png); } return(true); } catch { throw; } }
/// <summary>resize the png image</summary> /// <param name="sourcePath">valid source image path</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 ResizePng(string sourcePath, string destPath, int width, int height, bool compress = false) { try { if (String.IsNullOrEmpty(sourcePath) || String.IsNullOrEmpty(destPath) || width <= 0 || height <= 0) { return(false); } using (WebP webp = new WebP()) { Bitmap bmp = new Bitmap(sourcePath); var webp_byte = webp.EncodeLossless(bmp); if (compress) { bmp = webp.GetThumbnailFast(webp_byte, width, height); } else { bmp = webp.GetThumbnailQuality(webp_byte, width, height); } bmp.Save(destPath, System.Drawing.Imaging.ImageFormat.Png); } return(true); } catch { throw; } }
/// <summary>resize the webp image</summary> /// <param name="sourcePath">valid source image path</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 ResizeWebP(string sourcePath, string destPath, int width, int height, bool compress = false) { try { if (String.IsNullOrEmpty(sourcePath) || String.IsNullOrEmpty(destPath) || width <= 0 || height <= 0) { return(false); } using (WebP webp = new WebP()) { var webp_byte = webp.LoadByte(sourcePath); 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; } }
/// <summary> /// Test for load thumbnail function /// </summary> private void ButtonThumbnail_Click(object sender, EventArgs e) { try { using (OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog()) { openFileDialog.Filter = "WebP files (*.webp)|*.webp"; openFileDialog.FileName = ""; if (openFileDialog.ShowDialog() == DialogResult.OK) { string pathFileName = openFileDialog.FileName; byte[] rawWebP = File.ReadAllBytes(pathFileName); using (WebP webp = new WebP()) this.pictureBox.Image = webp.GetThumbnailFast(rawWebP, 200, 150); } } } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\nIn WebPExample.buttonThumbnail_Click", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }