public void TestDecSimple() { Imazen.WebP.Extern.LoadLibrary.LoadWebPOrFail(); var decoder = new SimpleDecoder(); var fileName = "testimage.webp"; var outFile = "testimageout.jpg"; File.Delete(outFile); FileStream outStream = new FileStream(outFile, FileMode.Create); using (Stream inputStream = System.IO.File.Open(fileName, System.IO.FileMode.Open)) { var bytes = ReadFully(inputStream); var outBitmap = decoder.DecodeFromBytes(bytes, bytes.LongLength); outBitmap.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg); outStream.Close(); } FileInfo finfo = new FileInfo(outFile); Assert.True(finfo.Exists); }
/// <summary> /// Read non-animated WEBP format /// </summary> /// <param name="path"></param> /// <returns></returns> public static Bitmap ReadWebpFile(string path) { var webpDecoder = new SimpleDecoder(); using (Stream inputStream = File.Open(path, FileMode.Open)) { byte[] buffer = new byte[16 * 1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } var bytes = ms.ToArray(); Bitmap outBitmap = webpDecoder.DecodeFromBytes(bytes, bytes.LongLength); return outBitmap; } } }
public static ParseImageResult[] GetAllImagesFromUrl( string url, System.Drawing.Size minSize, bool collectIMGTags, bool collectLINKTags, bool collectMETATags, int threadCount = 6, Helpers.PercentageProgress prgItem = null, bool downloadImages = false, ParseRuleConnectionType type = ParseRuleConnectionType.Direct) { List<ParseImageResult> result = new List<ParseImageResult>(); try { Helpers.PercentageProgress prgItemPage = null; Helpers.PercentageProgress prgItemImg = null; if (prgItem != null) { prgItemPage = prgItem.GetChild(); prgItemImg = prgItem.GetChild(); } HtmlAgilityPack.HtmlDocument document = SiteManager.GetContent(url, type, out url); if (prgItemPage != null) prgItemPage.Value = 100; //threadCount = 6; object lockAdd = new Object(); var allLinks = GetAllImagesUrlsFromUrl(document, url, collectIMGTags, collectLINKTags, collectMETATags, null); int fullCnt = allLinks.Count(); int currLoaded = 0; object currLoadedLock = new Object(); //threadCount = 1; allLinks .AsParallel() .WithDegreeOfParallelism(threadCount) .ForAll(node => { Uri fileUrl = node.Url;// Helper.GetFullSourceLink(node.Url, document, url); try { System.Drawing.Size imageSize; if (Helper.CheckImageSize(fileUrl.AbsoluteUri, minSize, out imageSize, true, !downloadImages)) { if (!imageSize.IsEmpty) SetImageSize(node.Node, imageSize); string fileName = fileUrl.AbsolutePath.Split(new[] { '/' }).Last(); string tempFileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + System.IO.Path.GetExtension(fileName); if (downloadImages) { SiteManager.GetFile(type, fileUrl, tempFileName); bool isWebPArchive = false; using (Stream inputStream = System.IO.File.Open(tempFileName, System.IO.FileMode.Open)) { var webPcheck = new byte[4]; int btsRead; if ((btsRead = inputStream.Read(webPcheck, 0, webPcheck.Length)) > 0) { var firstStr = Encoding.ASCII.GetString(webPcheck, 0, btsRead); if (firstStr.ToUpper() == "RIFF") isWebPArchive = true; } } if (isWebPArchive) { Imazen.WebP.Extern.LoadLibrary.LoadWebPOrFail(); var decoder = new SimpleDecoder(); var outFile = tempFileName + ".jpg"; using (FileStream outStream = new FileStream(outFile, FileMode.Create)) using (Stream inputStream = System.IO.File.Open(tempFileName, System.IO.FileMode.Open)) { var bytes = ReadFully(inputStream); var outBitmap = decoder.DecodeFromBytes(bytes, bytes.LongLength); outBitmap.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg); outStream.Close(); } tempFileName = outFile; } } System.Drawing.Image image = downloadImages ? System.Drawing.Image.FromFile(tempFileName) : null; try { if (image != null) { imageSize = new System.Drawing.Size() { Height = image.Height, Width = image.Width }; if (!imageSize.IsEmpty) SetImageSize(node.Node, imageSize); } if (!downloadImages || Helper.CheckImageSize(imageSize, minSize, false)) result.Add(new ParseImageResult(node.Node, image, imageSize, fileUrl)); } finally { if (!result.Any(r => r.Url == fileUrl) && image != null) image.Dispose(); if (downloadImages) try { System.IO.File.Delete(tempFileName); } catch { } } } } catch (Exception ex) { Helpers.Old.Log.Add(ex, string.Format("Helper.GetAllImagesFromUrl(url:'{0}',..,type:'{1}').ForAllThread(fileUrl:'{2}',..)", url, type, fileUrl.AbsoluteUri)); } finally { if (prgItemImg != null) lock (currLoadedLock) { currLoaded++; prgItemImg.Value = ((decimal)currLoaded / (decimal)fullCnt) * 100m; } } } ); if (prgItemImg != null) prgItemImg.Value = 100; } catch (Exception ex) { Helpers.Old.Log.Add(ex, string.Format("Helper.GetAllImagesFromUrl(url:'{0}',..,type:'{1}')", url, type)); } if (prgItem != null && prgItem.Value != 100) prgItem.Value = 100; result.RemoveAll(i => result.AsParallel().Count(r => r.Url.AbsoluteUri == i.Url.AbsoluteUri) > 1); return result.OrderBy(i => i.Url.ToString()).OrderByDescending(i => i.ImageSize.Width * i.ImageSize.Height).ToArray(); }
public static System.Drawing.Size GetImageSize(string directSourceLink, bool tryToDownloadImage = true) { var size = GetImageSize(directSourceLink); if (tryToDownloadImage && (size.Width * size.Height == 0)) { string fileName = new Uri(directSourceLink).AbsolutePath.Split(new[] { '/' }).Last(); string tempFileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + System.IO.Path.GetExtension(fileName); try { SiteManager.GetFile(new Uri(directSourceLink), tempFileName); bool isWebPArchive = false; using (Stream inputStream = System.IO.File.Open(tempFileName, System.IO.FileMode.Open)) { var webPcheck = new byte[4]; int btsRead; if ((btsRead = inputStream.Read(webPcheck, 0, webPcheck.Length)) > 0) { var firstStr = Encoding.ASCII.GetString(webPcheck, 0, btsRead); if (firstStr.ToUpper() == "RIFF") isWebPArchive = true; } } if (isWebPArchive) { Imazen.WebP.Extern.LoadLibrary.LoadWebPOrFail(); var decoder = new SimpleDecoder(); var outFile = tempFileName + ".jpg"; using (FileStream outStream = new FileStream(outFile, FileMode.Create)) using (Stream inputStream = System.IO.File.Open(tempFileName, System.IO.FileMode.Open)) { var bytes = ReadFully(inputStream); var outBitmap = decoder.DecodeFromBytes(bytes, bytes.LongLength); outBitmap.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg); outStream.Close(); } tempFileName = outFile; } using (System.Drawing.Image image = System.Drawing.Image.FromFile(tempFileName)) { size.Height = image.Height; size.Width = image.Width; } } catch (Exception ex) { Helpers.Old.Log.Add(ex, string.Format("Helper.GetImageSize('{0}','{1}')",directSourceLink, tryToDownloadImage)); } finally { try { System.IO.File.Delete(tempFileName); } catch { } } } return size; }
private void TestLosslessRoundtrip(Bitmap gdiBitmap) { var encoder = new SimpleEncoder(); var decoder = new SimpleDecoder(); using (var outStream = new System.IO.MemoryStream()) { encoder.Encode(gdiBitmap, outStream, -1); outStream.Close(); var webpBytes = outStream.ToArray(); var reloaded = decoder.DecodeFromBytes(webpBytes, webpBytes.LongLength); Assert.Equal(gdiBitmap.Height, reloaded.Height); Assert.Equal(gdiBitmap.Width, reloaded.Width); for (var y = 0; y < reloaded.Height; y++) { for (var x = 0; x < reloaded.Width; x++) { var expectedColor = gdiBitmap.GetPixel(x, y); var actualColor = reloaded.GetPixel(x, y); Assert.Equal(expectedColor, actualColor); } } } }