public void DownloadAvatar(Jid userId, string avatarId, string avatarType, RequestAvatarCallback finished) { PubSubIq dataiq = new PubSubIq(IqType.get, userId, Program.Jabber.conn.MyJID); dataiq.PubSub.Items = new Items("urn:xmpp:avatar:data"); dataiq.PubSub.Items.AddItem(new Item(avatarId)); Program.Jabber.conn.IqGrabber.SendIq(dataiq, (IqCB)((object sender_3, IQ result2, object unused_4) => { if (result2.Type != IqType.result) { finished(false, null); return; } Element data = result2.SelectSingleElement("data", true); byte[] imageBytes = Convert.FromBase64String(data.Value); Image img; switch (avatarType) { case "image/webp": var dec = new Imazen.WebP.SimpleDecoder(); img = dec.DecodeFromBytes(imageBytes, imageBytes.Length); break; default: img = Image.FromStream(new MemoryStream(imageBytes)); break; } string fileName = GetAvatarFilename(userId); img.Save(fileName, ImageFormat.Png); finished(true, fileName); })); }
private Bitmap LoadBitmapUnlocked(string file_name) { Bitmap bitmapToUse = null; string upperFileName = file_name.ToUpper(); if (upperFileName.Contains(".WEBP")) { Console.WriteLine("Found WEBP :" + upperFileName); Imazen.WebP.SimpleDecoder decoder = new Imazen.WebP.SimpleDecoder(); var bytes = File.ReadAllBytes(file_name); var bitmap = decoder.DecodeFromBytes(bytes, bytes.Length); return(bitmap); } try { using (Bitmap bm = new Bitmap(file_name)) { bitmapToUse = new Bitmap(bm); } } catch (Exception) { //throw; } return(bitmapToUse); }
private byte[] DecodeWebP(byte[] Data) { var Decoder = new Imazen.WebP.SimpleDecoder(); using (MemoryStream Output = new MemoryStream()) using (var Img = Decoder.DecodeFromBytes(Data, Data.LongLength)) { Img.Save(Output, ImageFormat.Png); return(Output.ToArray()); } }
// bitmapWillLockFile indicates whether holding onto Bitmap will hold lock on a file. public static Bitmap GetBitmap(string path, JpegRotationFinder shouldrotate, out bool bitmapWillLockFile) { Bitmap bitmap = null; try { if (ModeUtils.IsWebp(path)) { byte[] bytesData = File.ReadAllBytes(path); var decoder = new Imazen.WebP.SimpleDecoder(); bitmap = decoder.DecodeFromBytes(bytesData, bytesData.LongLength); bitmapWillLockFile = false; } else { bitmap = new Bitmap(path); bitmapWillLockFile = true; // some image files have custom resolutions, // I prefer seeing all files at the same resolution. bitmap.SetResolution(96.0f, 96.0f); } } catch (Exception e) { if (ModeUtils.IsWebp(path) && e.ToString().ToUpperInvariant().Contains("0x8007007E")) { Utils.MessageErr("It appears that the Visual C++ Redistributable " + "Packages for Visual Studio 2013 are not installed; please run " + "vcredist_x64.exe so that libwebp.dll can be used."); } Utils.MessageErr("Could not load the image " + path + Utils.NL + Utils.NL + Utils.NL + "Details: " + e.ToString(), true); if (bitmap != null) { bitmap.Dispose(); } bitmapWillLockFile = true; bitmap = new Bitmap(1, 1, PixelFormat.Format32bppPArgb); } if (shouldrotate != null && shouldrotate.ShouldRotate(path)) { bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone); } return(bitmap); }
public static Bitmap Load(string path) { string ext = Path.GetExtension(path); if (ext == ".webp") { byte[] bytes = File.ReadAllBytes(path); Imazen.WebP.SimpleDecoder decoder = new Imazen.WebP.SimpleDecoder(); return(decoder.DecodeFromBytes(bytes, bytes.Length)); } else { return(new Bitmap(path)); } }
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 } } }
static void Main(string[] args) { try { if (args.Length > 0) { foreach (var arg in args) { var path = Path.GetFullPath(arg); var name = Path.GetFileNameWithoutExtension(arg); var folder = Path.Combine(AppContext.BaseDirectory, "output"); var newpath = Path.Combine(folder, $"{name}.png"); Directory.CreateDirectory(folder); var image = File.ReadAllBytes(path); var decoder = new Imazen.WebP.SimpleDecoder(); var bitmap = decoder.DecodeFromBytes(image, image.Length); bitmap.Save(newpath, ImageFormat.Png); bitmap.Dispose(); Console.WriteLine(name); } Console.WriteLine("Done."); } else { Console.WriteLine("Arguments error!"); Console.WriteLine("Webp2Png [path1] [path2] ..."); } Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
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."); }
public static string GenerateIcons(string path, string cfg_path, int random_integer, int integer, bool skip_entry = false) { string num = ""; double proportion; System.Drawing.Bitmap bitmap = null; System.Drawing.Bitmap bitmap1 = null; System.Drawing.Bitmap bitmap2 = null; if (path.ToLower().Contains(".png") || path.ToLower().Contains(".jpg") || path.ToLower().Contains(".jpeg")) { try { try { bitmap = new System.Drawing.Bitmap(path); } catch (Exception ex0) { byte[] buffer = new byte[12]; try { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { fs.Read(buffer, 0, buffer.Length); fs.Close(); } if (buffer[0] == 82 && buffer[1] == 73 && buffer[2] == 70 && buffer[3] == 70 && buffer[8] == 87 && buffer[9] == 69 && buffer[10] == 66 && buffer[11] == 80) { //Webp Imazen.WebP.SimpleDecoder decoder = new Imazen.WebP.SimpleDecoder(); bitmap = decoder.DecodeFromBytes(File.ReadAllBytes(path), new System.IO.FileInfo(path).Length); } } catch (System.UnauthorizedAccessException ex1) { File.AppendAllText(cfg_path + "\\Error.log", "\r\n[Error: " + DateTime.Now + "] " + ex0.ToString()); } } bitmap1 = bitmap.Clone(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.PixelFormat.Format32bppPArgb);//BitmapHandler.CloneImage(ConvertToBitmap((BitmapSource)bitmap)); if (bitmap.Width > bitmap.Height) { proportion = bitmap.Width / 128; } else { proportion = bitmap.Height / 128; } if (bitmap1.Height > 128 && bitmap1.Width > 128) { bitmap2 = new System.Drawing.Bitmap(bitmap1, (int)(bitmap1.Width / proportion), (int)(bitmap1.Height / proportion)); } else { bitmap2 = bitmap1; } bitmap2.Save(cfg_path + "\\Cache\\thumbnail_" + random_integer + integer, System.Drawing.Imaging.ImageFormat.Png); if (!skip_entry) { File.AppendAllText(cfg_path + "\\Cache\\iconcache.db", path + "*" + "\\Cache\\thumbnail_" + random_integer + integer + "\r\n"); } num = "\\Cache\\thumbnail_" + random_integer + integer; bitmap = null; bitmap1 = null; bitmap2 = null; } catch (Exception ex) { } //bitmap2 = null; } else { IntPtr hIcon = Util.GetJumboIcon(Util.GetIconIndex(path)); // from native to managed try { using (System.Drawing.Icon ico = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(hIcon).Clone()) { // save to file (or show in a picture box) if (!IsSmallIcon(ico.ToBitmap())) { ico.ToBitmap().Save(cfg_path + "\\Cache\\icon_" + random_integer + integer, System.Drawing.Imaging.ImageFormat.Png); } else { System.Drawing.Rectangle cropRect = new System.Drawing.Rectangle(0, 0, 48, 48); System.Drawing.Image img = ico.ToBitmap(); Util.CropImage(img, cropRect).Save(cfg_path + "\\Cache\\icon_" + random_integer + integer, System.Drawing.Imaging.ImageFormat.Png); } if (!skip_entry) { File.AppendAllText(cfg_path + "\\Cache\\iconcache.db", path + "*" + "\\Cache\\icon_" + random_integer + integer + "\r\n"); } num = "\\Cache\\icon_" + random_integer + integer; } Util.Shell32.DestroyIcon(hIcon); // don't forget to cleanup } catch (Exception ex) { } } return(num); }