public static void LongWork(IProgress <int> progress, int selectedOutput, int webpq, string outpath, DataGridViewRowCollection rows) { int p = 0; foreach (DataGridViewRow row in rows) { var extension = row.Cells[1].Value as string; var filepath = row.Cells[0].Value as string + extension; var filename = Path.GetFileNameWithoutExtension(filepath); byte[] buffer = File.ReadAllBytes(row.Cells[0].Value as string + row.Cells[1].Value as string); Bitmap bitmap = null; //check if current image is webp and encode if (extension.ToLowerInvariant().Contains("webp")) { var decoder = new Imazen.WebP.SimpleDecoder(); bitmap = decoder.DecodeFromBytes(buffer, buffer.LongLength); } else // If not, load file into bitmap { bitmap = new Bitmap(new MemoryStream(buffer)); } System.Drawing.Image img = bitmap; //If not saving as webp, use image.save with the appropriate format if (selectedOutput != 5) { img.Save(outpath + filename + $".{FormatExtensions[selectedOutput]}", ImageFormats[selectedOutput]); } else { //If saving as webp encode the file var encoder = new Imazen.WebP.SimpleEncoder(); FileStream outStream = new FileStream(outpath + filename + $".webp", FileMode.Create); encoder.Encode(bitmap, outStream, webpq); outStream.Close(); } p++; progress.Report(p); // report the progress to the ui thread if (Stop) { break; // if user requested to stop the conversion, break out of loop } } }
private void ImageExecute_Click(object sender, EventArgs e) { if (ImageValidate()) { return; } string directory = tImage.Text; DirectoryInfo dir = new DirectoryInfo(directory); List <string> imageTypes = new List <string>(); List <Img> listImage = new List <Img>(); string newFormat = cImageConvertTo.SelectedItem == null ? "" : cImageConvertTo.SelectedItem.ToString().ToLower(); if (cImageConvert.Checked) { imageTypes.AddRange(listImageConvert); } else { imageTypes.AddRange(new List <string> { "jpg", "png", "gif", "jpeg", "bmp", "jpe", "ico", "tif", "tiff", "jfif" }); } foreach (var file in dir.GetFiles()) { if (imageTypes.Contains(file.Extension.ToLower().Trim('.'))) { listImage.Add(new Img() { Name = file.Name, Extension = file.Extension.ToLower().Trim('.'), FullDir = file.FullName }); } } string strError = string.Empty; FileStream fStream; Image img; foreach (Img im in listImage) { // Load Image if (im.Extension == "webp") { var sE = new Imazen.WebP.SimpleDecoder(); using (var fS = new FileStream(im.FullDir, FileMode.Open)) { byte[] buffer = new byte[fS.Length]; fS.Read(buffer, 0, (int)fS.Length); img = sE.DecodeFromBytes(buffer, fS.Length); } im.Width = img.Width; im.Height = img.Height; } else { using (fStream = new FileStream(im.FullDir, FileMode.Open, FileAccess.Read)) { img = Image.FromStream(fStream); im.Width = img.Width; im.Height = img.Height; } } //Resize img = ResizeImage(img, im); // Convert if (cImageConvert.Checked) { FileInfo fI = new FileInfo(im.FullDir.Substring(0, im.FullDir.Length - im.Extension.Length) + newFormat); if (fI.Exists) { strError += " File already exists: " + im.Name.Substring(0, im.FullDir.Length - im.Extension.Length) + newFormat + Environment.NewLine; } else { var newFile = im.FullDir; if (!cImageOldExtension.Checked) { newFile = im.FullDir.Substring(0, im.FullDir.Length - im.Extension.Length) + newFormat; } im.Extension = newFormat; RemoveFile(newFile); if (newFormat == "webp") { using (var stream = new FileStream(newFile, FileMode.CreateNew)) { var sE = new Imazen.WebP.SimpleEncoder(); sE.Encode((Bitmap)img, stream, 96.6f); } } else { img.Save(newFile, im.Format); } if (cImageRemoveOrigin.Checked && !cImageOldExtension.Checked) { RemoveFile(im.FullDir); } } } else { EncoderParameters myEncoderParameters = new EncoderParameters(1); var myEncoderParameter = new EncoderParameter(Encoder.Quality, long.Parse(tImageQuality.Text ?? "98")); myEncoderParameters.Param[0] = myEncoderParameter; img.Save(im.FullDir, GetEncoder(im.Format), myEncoderParameters); } img.Dispose(); } if (strError != "") { MessageBox.Show(strError); } if (!cKeepAll.Checked) { listImageConvert.Clear(); lImageFrom.Items.Clear(); } MessageBox.Show("Finished."); }