/// <summary> /// Test for advanced decode function /// </summary> private void ButtonCropFlip_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); WebPDecoderOptions decoderOptions = new WebPDecoderOptions(); decoderOptions.use_cropping = 1; decoderOptions.crop_top = 100; //Top beging of crop area decoderOptions.crop_left = 100; //Left beging of crop area decoderOptions.crop_height = 400; //Height of crop area decoderOptions.crop_width = 400; //Width of crop area decoderOptions.use_threads = 1; //Use multhreading decoderOptions.flip = 1; //Flip the image using (WebP webp = new WebP()) this.pictureBox.Image = webp.Decode(rawWebP, decoderOptions); } } } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\nIn WebPExample.buttonCrop_Click", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void GetImageFromStream(Stream stream, Enums.ImageType imageType, SourceList <BitmapSource> imageList, CancellationToken token) { using var memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); token.ThrowIfCancellationRequested(); // start of expensive operation Bitmap bitmap; switch (imageType) { case Enums.ImageType.Default: bitmap = new Bitmap(memoryStream); break; case Enums.ImageType.WebP: byte[] b = memoryStream.ToArray(); using (WebP webp = new WebP()) bitmap = webp.Decode(b); break; default: throw new BadImageFormatException(); } var bitmapSource = ConvertStreamToSource(bitmap); bitmapSource.Freeze(); token.ThrowIfCancellationRequested(); // end imageList.Add(bitmapSource); bitmap.Dispose(); }
public static async Task <BitmapImage> LoadWebPImageAsync(string url) { try { if (string.IsNullOrEmpty(url)) { return(null); } System.Net.WebClient wc = new System.Net.WebClient(); wc.Headers.Add("Referer", "https://hitomi.la/"); Byte[] MyData = await wc.DownloadDataTaskAsync(url); wc.Dispose(); WebP webP = new WebP(); Bitmap bitmap = webP.Decode(MyData); MemoryStream ms = new MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); var bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.CacheOption = BitmapCacheOption.OnLoad; bi.EndInit(); return(bi); } catch { return(null); } }
/// <summary> /// The Convert /// </summary> /// <param name="value">The <see cref="object"/></param> /// <param name="targetType">The <see cref="Type"/></param> /// <param name="parameter">The <see cref="object"/></param> /// <param name="culture">The <see cref="CultureInfo"/></param> /// <returns>The <see cref="object"/></returns> public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null && value is Binary) { try { var ByteArray = value as Binary; var bmp = new BitmapImage(); using (var webp = new WebP()) bmp = Convert(webp.Decode(ByteArray.ToArray())); bmp.Freeze(); return(bmp); } catch (Exception) { } } return(null); }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) { return null; } try { var imagesBytes = (byte[]) value; int hCode = imagesBytes.GetHashCode(); if (avatarCache.ContainsKey(hCode)) { return avatarCache[hCode]; } if (ImageUtils.GetImageFormat(imagesBytes) == Images.ImageFormat.Webp) { // WebP images using (WebP webp = new WebP()) { var bmp = webp.Decode(imagesBytes); var bitmapImage = BitmapToBitmapImage(bmp); avatarCache.Add(hCode, bitmapImage); return bitmapImage; } } else { // all other images var bitmapImage = ConvertByteArrayToBitmapImage(imagesBytes); avatarCache.Add(hCode, bitmapImage); return bitmapImage; } } catch(Exception) { return null; } }
public static async Task <byte[]> LoadUrlWebPImageAsync(string url) { if (string.IsNullOrEmpty(url)) { return(null); } System.Net.WebClient wc = new System.Net.WebClient(); wc.Headers.Add("Referer", "https://" + new Uri(url).Host); Byte[] MyData = await wc.DownloadDataTaskAsync(url); wc.Dispose(); WebP webP = new WebP(); Bitmap bitmap = webP.Decode(MyData); MemoryStream ms = new MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); byte[] data = ms.ToArray(); ms.Dispose(); return(data); }
public static Image ExtractImage(this byte[] rawBytes, bool isWebp) { if (rawBytes.Length == 0) { // if no image, return a dummy image return(new Bitmap(1, 1)); } if (isWebp) { using (var webp = new WebP()) { return(webp.Decode(rawBytes)); } } else { using (var ms = new MemoryStream(rawBytes)) { return(Image.FromStream(ms)); } } }
public override FileTypeMan.OpenResult Open(string path) { try { byte[] rawWebP = File.ReadAllBytes(path); using (WebP webp = new WebP()) { WebPDecoderOptions decoderOptions = new WebPDecoderOptions(); decoderOptions.use_threads = 1; decoderOptions.alpha_dithering_strength = 100; return(new FileTypeMan.OpenResult { Bmp = webp.Decode(rawWebP, decoderOptions) }); } } catch { return(new FileTypeMan.OpenResult { ErrorMessage = TypeName + " - " + LangMan.Get("unable-open-file") + ": " + Path.GetFileName(path) }); } }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) { return(null); } try { var byteArray = value as byte[]; BitmapImage bmp; using (var webp = new WebP()) { bmp = Convert(webp.Decode(byteArray)); } bmp.Freeze(); return(bmp); } catch (Exception) { } return(null); }
private Image ConvertFromWebP(Stream stream) { using (var webp = new WebP()) return(webp.Decode(GetBytes(stream))); }