Imazen.WebP.SimpleEncoder.Encode C# (CSharp) Method

Encode() public method

Encodes the given RGB(A) bitmap to the given stream. Specify quality = -1 for lossless, otherwise specify a value between 0 and 100.
public Encode ( Bitmap from, Stream to, float quality ) : void
from System.Drawing.Bitmap
to Stream
quality float
return void
        public void Encode(Bitmap from, Stream to, float quality)
        {
            IntPtr result;
            long length;

            Encode(from, quality, out result, out length);
            try {
                byte[] buffer = new byte[4096];
                for (int i = 0; i < length; i += buffer.Length) {
                    int used = (int)Math.Min((int)buffer.Length, length - i);
                    Marshal.Copy((IntPtr)((long)result + i), buffer, 0, used);
                    to.Write(buffer, 0, used);
                }
            } finally {
                NativeMethods.WebPSafeFree(result);
            }
        }

Same methods

SimpleEncoder::Encode ( Bitmap from, Stream to, float quality, bool noAlpha ) : void
SimpleEncoder::Encode ( Bitmap b, float quality, IntPtr &result, long &length ) : void
SimpleEncoder::Encode ( Bitmap b, float quality, bool noAlpha, IntPtr &result, long &length ) : void

Usage Example

Esempio n. 1
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);
        }
All Usage Examples Of Imazen.WebP.SimpleEncoder::Encode