Example #1
0
        public static string GetDecoderVersion()
        {
            uint v        = (uint)LibwebpUtils.WebPGetDecoderVersion();
            var  revision = v % 256;
            var  minor    = (v >> 8) % 256;
            var  major    = (v >> 16) % 256;

            return(major + "." + minor + "." + revision);
        }
Example #2
0
        public Bitmap DecodeFromPointer(IntPtr data, long length)
        {
            int w = 0, h = 0;

            //Validate header and determine size
            if (LibwebpUtils.WebPGetInfo(data, (UIntPtr)length, ref w, ref h) == 0)
            {
                throw new Exception("Invalid WebP header detected");
            }

            bool       success = false;
            Bitmap     b       = null;
            BitmapData bd      = null;

            try
            {
                //Allocate canvas
                b = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                //Lock surface for writing
                bd = b.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                //Decode to surface
                IntPtr result = LibwebpUtils.WebPDecodeBGRAInto(data, (UIntPtr)length, bd.Scan0, (UIntPtr)(bd.Stride * bd.Height), bd.Stride);
                if (bd.Scan0 != result)
                {
                    throw new Exception("Failed to decode WebP image with error " + (long)result);
                }
                success = true;
            }
            finally
            {
                //Unlock surface
                if (bd != null && b != null)
                {
                    b.UnlockBits(bd);
                }
                //Dispose of bitmap if anything went wrong
                if (!success && b != null)
                {
                    b.Dispose();
                }
            }
            return(b);
        }