/// <summary>resize the png 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 ResizePngForWeb(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()) { Bitmap bmp = new Bitmap(Helper.DownloadImage(sourceUrl)); 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; } }
public static string ConvertImageToTempWebPFile(Bitmap imageBitmap, bool lossless) { string tempIcon = ApplicationDataUtils.GenerateNonExistentFilePath(extension: ".webp"); IOUtils.EnsureParentDirExists(tempIcon); using (var webP = new WebP()) { File.WriteAllBytes( tempIcon, lossless ? webP.EncodeLossless(imageBitmap, speed: 9) : webP.EncodeLossy(imageBitmap, quality: 75, speed: 9) ); } return(tempIcon); }
/// <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; } }
/// <summary> /// Test encode functions /// </summary> private void ButtonSave_Click(object sender, System.EventArgs e) { byte[] rawWebP; try { if (this.pictureBox.Image == null) { MessageBox.Show("Please, load an image first"); } //get the picturebox image Bitmap bmp = (Bitmap)pictureBox.Image; //Test simple encode lossly mode in memory with quality 75 string lossyFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SimpleLossy.webp"); using (WebP webp = new WebP()) rawWebP = webp.EncodeLossy(bmp, 75); File.WriteAllBytes(lossyFileName, rawWebP); MessageBox.Show("Made " + lossyFileName, "Simple lossy"); //Test encode lossly mode in memory with quality 75 and speed 9 string advanceLossyFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AdvanceLossy.webp"); using (WebP webp = new WebP()) rawWebP = webp.EncodeLossy(bmp, 71, 9, true); File.WriteAllBytes(advanceLossyFileName, rawWebP); MessageBox.Show("Made " + advanceLossyFileName, "Advance lossy"); //Test simple encode lossless mode in memory string simpleLosslessFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SimpleLossless.webp"); using (WebP webp = new WebP()) rawWebP = webp.EncodeLossless(bmp); File.WriteAllBytes(simpleLosslessFileName, rawWebP); MessageBox.Show("Made " + simpleLosslessFileName, "Simple lossless"); //Test advance encode lossless mode in memory with speed 9 string losslessFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AdvanceLossless.webp"); using (WebP webp = new WebP()) rawWebP = webp.EncodeLossless(bmp, 9, true); File.WriteAllBytes(losslessFileName, rawWebP); MessageBox.Show("Made " + losslessFileName, "Advance lossless"); //Test encode near lossless mode in memory with quality 40 and speed 9 // quality 100: No-loss (bit-stream same as -lossless). // quality 80: Very very high PSNR (around 54dB) and gets an additional 5-10% size reduction over WebP-lossless image. // quality 60: Very high PSNR (around 48dB) and gets an additional 20%-25% size reduction over WebP-lossless image. // quality 40: High PSNR (around 42dB) and gets an additional 30-35% size reduction over WebP-lossless image. // quality 20 (and below): Moderate PSNR (around 36dB) and gets an additional 40-50% size reduction over WebP-lossless image. string nearLosslessFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NearLossless.webp"); using (WebP webp = new WebP()) rawWebP = webp.EncodeNearLossless(bmp, 40, 9, true); File.WriteAllBytes(nearLosslessFileName, rawWebP); MessageBox.Show("Made " + nearLosslessFileName, "Near lossless"); MessageBox.Show("End of Test"); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\nIn WebPExample.buttonSave_Click", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public static void Main(string[] args) { if (Environment.OSVersion.Platform != PlatformID.Win32NT) { MessageBox.Show(Properties.Resources.MessageApplicationCannotRun, GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionError, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Settings settings = new Settings(); if (!settings.DisableThemes) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); settings.RenderWithVisualStyles = Application.RenderWithVisualStyles; } ArgumentParser argumentParser = new ArgumentParser(); try { argumentParser.Arguments = args; } catch (Exception exception) { Debug.WriteLine(exception); ErrorLog.WriteLine(exception); MessageBox.Show(exception.Message, GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionError, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (argumentParser.HasArguments) { if (argumentParser.IsHelp) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine(Properties.Resources.HelpLine1.Replace("\\t", "\t")).AppendLine(); stringBuilder.AppendLine(Properties.Resources.HelpLine2.Replace("\\t", "\t")).AppendLine(); stringBuilder.AppendLine(Properties.Resources.HelpLine3.Replace("\\t", "\t")).AppendLine(); stringBuilder.AppendLine(Properties.Resources.HelpLine4.Replace("\\t", "\t")).AppendLine(); stringBuilder.AppendLine(Properties.Resources.HelpLine5.Replace("\\t", "\t")).AppendLine(); stringBuilder.AppendLine(Properties.Resources.HelpLine6.Replace("\\t", "\t")).AppendLine(); stringBuilder.AppendLine(Properties.Resources.HelpLine7.Replace("\\t", "\t")).AppendLine(); stringBuilder.AppendLine(Properties.Resources.HelpLine8.Replace("\\t", "\t")).AppendLine(); stringBuilder.AppendLine(Properties.Resources.HelpLine9.Replace("\\t", "\t")).AppendLine(); stringBuilder.AppendLine(Properties.Resources.HelpLine10.Replace("\\t", "\t")).AppendLine(); stringBuilder.AppendLine(Properties.Resources.HelpLine11.Replace("\\t", "\t")).AppendLine(); stringBuilder.AppendLine(Properties.Resources.HelpLine12.Replace("\\t", "\t")); MessageBox.Show(stringBuilder.ToString(), GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionHelp, MessageBoxButtons.OK, MessageBoxIcon.Question); } else if (argumentParser.IsThisTest) { try { Application.Run(new ArgumentParserForm()); } catch (Exception exception) { Debug.WriteLine(exception); ErrorLog.WriteLine(exception); MessageBox.Show(exception.Message, GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionError, MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(Properties.Resources.MessageApplicationError, GetTitle(), MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageForm messageForm = null; BackgroundForm backgroundForm = null; try { if (Directory.Exists(Path.GetDirectoryName(argumentParser.OutputFilePath)) && !string.IsNullOrWhiteSpace(Path.GetFileNameWithoutExtension(argumentParser.OutputFilePath))) { messageForm = new MessageForm(argumentParser.Text, argumentParser.Caption, argumentParser.Buttons, argumentParser.BoxIcon, argumentParser.DefaultButton, true, argumentParser.DisplayHelpButton, argumentParser.MaximumWidth, argumentParser.NoWrap); if (argumentParser.BasicTheme) { messageForm.Show(); using (Bitmap bitmap = new Bitmap(messageForm.Width, messageForm.Height, PixelFormat.Format32bppArgb)) { messageForm.DrawToBitmap(bitmap, new Rectangle(Point.Empty, bitmap.Size)); switch (Path.GetExtension(argumentParser.OutputFilePath).ToLowerInvariant()) { case Constants.ExtensionBmp: bitmap.Save(argumentParser.OutputFilePath, ImageFormat.Bmp); break; case Constants.ExtensionGif: bitmap.Save(argumentParser.OutputFilePath, ImageFormat.Gif); break; case Constants.ExtensionJpg: bitmap.Save(argumentParser.OutputFilePath, ImageFormat.Jpeg); break; case Constants.ExtensionTif: bitmap.Save(argumentParser.OutputFilePath, ImageFormat.Tiff); break; case Constants.ExtensionWebP: using (WebP webP = new WebP()) { File.WriteAllBytes(argumentParser.OutputFilePath, webP.EncodeLossless(bitmap)); } break; default: bitmap.Save(argumentParser.OutputFilePath, ImageFormat.Png); break; } } } else { backgroundForm = new BackgroundForm(); backgroundForm.Show(); backgroundForm.Location = new Point(SystemInformation.WorkingArea.Location.X - Constants.BackgroundFormOverShoot, SystemInformation.WorkingArea.Location.Y - Constants.BackgroundFormOverShoot); backgroundForm.Size = new Size(messageForm.Width + Constants.BackgroundFormOverShoot * 2, messageForm.Height + Constants.BackgroundFormOverShoot * 2); messageForm.Show(); messageForm.Location = SystemInformation.WorkingArea.Location; Application.DoEvents(); System.Threading.Thread.Sleep(Constants.ScreenFormCaptureDelay); using (Bitmap bitmap = new Bitmap(Math.Min(SystemInformation.WorkingArea.Width, messageForm.Width), Math.Min(SystemInformation.WorkingArea.Height, messageForm.Height), PixelFormat.Format32bppArgb)) { Graphics graphics = Graphics.FromImage(bitmap); graphics.CopyFromScreen(SystemInformation.WorkingArea.Location.X, SystemInformation.WorkingArea.Location.Y, 0, 0, new Size(Math.Min(SystemInformation.WorkingArea.Width, messageForm.Width), Math.Min(SystemInformation.WorkingArea.Height, messageForm.Height)), CopyPixelOperation.SourceCopy); switch (Path.GetExtension(argumentParser.OutputFilePath).ToLowerInvariant()) { case Constants.ExtensionBmp: bitmap.Save(argumentParser.OutputFilePath, ImageFormat.Bmp); break; case Constants.ExtensionGif: bitmap.Save(argumentParser.OutputFilePath, ImageFormat.Gif); break; case Constants.ExtensionJpg: bitmap.Save(argumentParser.OutputFilePath, ImageFormat.Jpeg); break; case Constants.ExtensionTif: bitmap.Save(argumentParser.OutputFilePath, ImageFormat.Tiff); break; case Constants.ExtensionWebP: using (WebP webP = new WebP()) { File.WriteAllBytes(argumentParser.OutputFilePath, webP.EncodeLossless(bitmap)); } break; default: bitmap.Save(argumentParser.OutputFilePath, ImageFormat.Png); break; } } } } } catch (Exception exception) { Debug.WriteLine(exception); ErrorLog.WriteLine(exception); } finally { if (messageForm != null) { messageForm.Close(); } if (backgroundForm != null) { backgroundForm.Close(); } } } } else { try { SingleMainForm.Run(new MainForm(settings)); } catch (Exception exception) { Debug.WriteLine(exception); ErrorLog.WriteLine(exception); MessageBox.Show(exception.Message, GetTitle() + Constants.Space + Constants.EnDash + Constants.Space + Properties.Resources.CaptionError, MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(Properties.Resources.MessageApplicationError, GetTitle(), MessageBoxButtons.OK, MessageBoxIcon.Information); } } }