Exemplo n.º 1
0
        /// <summary>
        /// The WebP load function.
        /// </summary>
        /// <param name="webpBytes">The input image data</param>
        /// <returns>
        /// A <see cref="Bitmap"/> containing the WebP image.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="webpBytes"/> is null.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory to load the WebP image.</exception>
        /// <exception cref="WebPException">
        /// The WebP image is invalid.
        /// -or-
        /// A native API parameter is invalid.
        /// </exception>
        internal static unsafe Bitmap Load(byte[] webpBytes)
        {
            if (webpBytes == null)
            {
                throw new ArgumentNullException(nameof(webpBytes));
            }

            WebPNative.ImageInfo imageInfo;

            WebPNative.WebPGetImageInfo(webpBytes, out imageInfo);

            if (imageInfo.hasAnimation)
            {
                throw new WebPException(Resources.AnimatedWebPNotSupported);
            }

            Bitmap image = null;
            Bitmap temp  = null;

            try
            {
                temp = new Bitmap(imageInfo.width, imageInfo.height, PixelFormat.Format32bppArgb);

                BitmapData bitmapData = temp.LockBits(new Rectangle(0, 0, imageInfo.width, imageInfo.height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

                try
                {
                    WebPNative.WebPLoad(webpBytes, bitmapData);
                }
                finally
                {
                    temp.UnlockBits(bitmapData);
                }

                image = temp;
                temp  = null;
            }
            finally
            {
                if (temp != null)
                {
                    temp.Dispose();
                }
            }

            return(image);
        }
Exemplo n.º 2
0
        /// <summary>
        /// The WebP load function.
        /// </summary>
        /// <param name="webpBytes">The input image data</param>
        /// <returns>
        /// A <see cref="Bitmap"/> containing the WebP image.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="webpBytes"/> is null.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory to load the WebP image.</exception>
        /// <exception cref="WebPException">
        /// The WebP image is invalid.
        /// -or-
        /// A native API parameter is invalid.
        /// </exception>
        internal static unsafe Surface Load(byte[] webpBytes)
        {
            if (webpBytes == null)
            {
                throw new ArgumentNullException(nameof(webpBytes));
            }

            WebPNative.WebPGetImageInfo(webpBytes, out WebPNative.ImageInfo imageInfo);

            if (imageInfo.hasAnimation)
            {
                throw new WebPException(Resources.AnimatedWebPNotSupported);
            }

            Surface image = null;
            Surface temp  = null;

            try
            {
                temp = new Surface(imageInfo.width, imageInfo.height);

                WebPNative.WebPLoad(webpBytes, temp);

                image = temp;
                temp  = null;
            }
            finally
            {
                if (temp != null)
                {
                    temp.Dispose();
                }
            }

            return(image);
        }