Exemple #1
0
 private static byte[] ResimYükle(Bitmap bmp)
 {
     using (var webp = new WebP())
     {
         return(webp.EncodeLossy(bmp, (int)Kalite));
     }
 }
Exemple #2
0
        public static byte[] ResizeImage(Image image, int width, int height, ImageType type, bool keepRatio = true)
        {
            using (image)
            {
                if (keepRatio)
                {
                    KeepRatioResise(image.Width, image.Height, ref width, ref height);
                }
                using (var newImage = new Bitmap(width, height))
                    using (var graphics = Graphics.FromImage(newImage))
                    {
                        graphics.SmoothingMode     = SmoothingMode.AntiAlias;
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;

                        if (type == ImageType.Jpeg)
                        {
                            graphics.Clear(Color.White);
                        }

                        graphics.DrawImage(image, new Rectangle(0, 0, width, height));

                        if (type == ImageType.Png)
                        {
                            using (var ms = new MemoryStream())
                            {
                                newImage.Save(ms, ImageFormat.Png);
                                return(ms.ToArray());
                            }
                        }
                        else if (type == ImageType.Webp)
                        {
                            byte[] rawWebP;

                            using (WebP webp = new WebP())
                            {
                                rawWebP = webp.EncodeLossy(newImage, 75, 0);
                                return(rawWebP.ToArray());
                            }
                        }
                        else
                        {
                            using (var ms = new MemoryStream())
                            {
                                var jpgEncoder          = GetEncoder(ImageFormat.Jpeg);
                                var myEncoder           = Encoder.Quality;
                                var myEncoderParameters = new EncoderParameters(1);

                                var myEncoderParameter = new EncoderParameter(myEncoder, 75L); //70 qualidade para jpeg
                                myEncoderParameters.Param[0] = myEncoderParameter;

                                newImage.Save(ms, jpgEncoder, myEncoderParameters);
                                return(ms.ToArray());
                            }
                        }
                    }
            }
        }
 private void CreateWebPImage(IFormFile image, string filePath, int quality)
 {
     using (var webp = new WebP())
     {
         var encoded = webp.EncodeLossy(new Bitmap(image.OpenReadStream()), quality);
         using (var webpFileStream = new FileStream(filePath, FileMode.Create))
             webpFileStream.Write(encoded, 0, encoded.Length);
     }
 }
        private void saveWebp_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap("D:\\temp.Jpeg");

            //using (WebP webp = new WebP())
            //webp.Save(bmp, 80, "test.webp");
            //int re = 75;

            byte[] rawWebP = File.ReadAllBytes("D:\\temp.Jpeg");
            using (WebP webp = new WebP())
                rawWebP = webp.EncodeLossy(bmp, 75);
            File.WriteAllBytes("D:\\WebPimage.webp", rawWebP);

            //imageBytes = ImageToByteArray(img);
        }
        public static string ConvertImageToTempWebPFile(Bitmap imageBitmap, bool lossless)
        {
            string tempIcon = ApplicationDataUtils.GenerateNonExistentFilePath(extension: ".webp");

            IOUtils.EnsureParentDirExists(tempIcon);
            using (var webP = new WebP())
            {
                File.WriteAllBytes(
                    tempIcon,
                    lossless
                        ? webP.EncodeLossless(imageBitmap, speed: 9)
                        : webP.EncodeLossy(imageBitmap, quality: 75, speed: 9)
                    );
            }

            return(tempIcon);
        }
Exemple #6
0
        /// <summary>
        /// Test encode functions
        /// </summary>
        private void ButtonSave_Click(object sender, System.EventArgs e)
        {
            byte[] rawWebP;

            try
            {
                if (this.pictureBox.Image == null)
                {
                    MessageBox.Show("Please, load an image first");
                }

                //get the picturebox image
                Bitmap bmp = (Bitmap)pictureBox.Image;

                //Test simple encode lossly mode in memory with quality 75
                string lossyFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SimpleLossy.webp");
                using (WebP webp = new WebP())
                    rawWebP = webp.EncodeLossy(bmp, 75);
                File.WriteAllBytes(lossyFileName, rawWebP);
                MessageBox.Show("Made " + lossyFileName, "Simple lossy");

                //Test encode lossly mode in memory with quality 75 and speed 9
                string advanceLossyFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AdvanceLossy.webp");
                using (WebP webp = new WebP())
                    rawWebP = webp.EncodeLossy(bmp, 71, 9, true);
                File.WriteAllBytes(advanceLossyFileName, rawWebP);
                MessageBox.Show("Made " + advanceLossyFileName, "Advance lossy");

                //Test simple encode lossless mode in memory
                string simpleLosslessFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SimpleLossless.webp");
                using (WebP webp = new WebP())
                    rawWebP = webp.EncodeLossless(bmp);
                File.WriteAllBytes(simpleLosslessFileName, rawWebP);
                MessageBox.Show("Made " + simpleLosslessFileName, "Simple lossless");

                //Test advance encode lossless mode in memory with speed 9
                string losslessFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AdvanceLossless.webp");
                using (WebP webp = new WebP())
                    rawWebP = webp.EncodeLossless(bmp, 9, true);
                File.WriteAllBytes(losslessFileName, rawWebP);
                MessageBox.Show("Made " + losslessFileName, "Advance lossless");

                //Test encode near lossless mode in memory with quality 40 and speed 9
                // quality 100: No-loss (bit-stream same as -lossless).
                // quality 80: Very very high PSNR (around 54dB) and gets an additional 5-10% size reduction over WebP-lossless image.
                // quality 60: Very high PSNR (around 48dB) and gets an additional 20%-25% size reduction over WebP-lossless image.
                // quality 40: High PSNR (around 42dB) and gets an additional 30-35% size reduction over WebP-lossless image.
                // quality 20 (and below): Moderate PSNR (around 36dB) and gets an additional 40-50% size reduction over WebP-lossless image.
                string nearLosslessFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NearLossless.webp");
                using (WebP webp = new WebP())
                    rawWebP = webp.EncodeNearLossless(bmp, 40, 9, true);
                File.WriteAllBytes(nearLosslessFileName, rawWebP);
                MessageBox.Show("Made " + nearLosslessFileName, "Near lossless");

                MessageBox.Show("End of Test");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\nIn WebPExample.buttonSave_Click", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }