Encodes Bitmap objects into WebP format
        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);
                    }
                }
            }
        }
예제 #2
0
        public void TestEncSimple()
        {
            Imazen.WebP.Extern.LoadLibrary.LoadWebPOrFail();

            var encoder = new SimpleEncoder();
            var fileName = "testimage.jpg";
            var outFileName = "testimageout.webp";
            File.Delete(outFileName);

            Bitmap mBitmap;
            FileStream outStream = new FileStream(outFileName, FileMode.Create);
            using (Stream BitmapStream = System.IO.File.Open(fileName, System.IO.FileMode.Open))
            {
                Image img = Image.FromStream(BitmapStream);

                mBitmap = new Bitmap(img);

                encoder.Encode(mBitmap, outStream, 100, false);
            }

            FileInfo finfo = new FileInfo(outFileName);
            Assert.True(finfo.Exists);
        }