Exemple #1
0
        /// <summary>
        /// Test for load from file function
        /// </summary>
        private void ButtonLoad_Click(object sender, System.EventArgs e)
        {
            try
            {
                using (OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog())
                {
                    openFileDialog.Filter   = "Image files (*.webp, *.png, *.tif, *.tiff)|*.webp;*.png;*.tif;*.tiff";
                    openFileDialog.FileName = "";
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        this.buttonSave.Enabled = true;
                        this.buttonSave.Enabled = true;
                        string pathFileName = openFileDialog.FileName;

                        if (Path.GetExtension(pathFileName) == ".webp")
                        {
                            using (WebP webp = new WebP())
                                this.pictureBox.Image = webp.Load(pathFileName);
                        }
                        else
                        {
                            this.pictureBox.Image = Image.FromFile(pathFileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\nIn WebPExample.buttonLoad_Click", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 public static Bitmap fromWebP(string filepath)
 {
     using (WebP webp = new WebP())
     {
         return(webp.Load(filepath));
     }
 }
Exemple #3
0
        private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ofdFile.ShowDialog() == DialogResult.OK)
            {
                Console.WriteLine(ofdFile.FileName);
                Console.WriteLine(Path.GetExtension(ofdFile.FileName));

                Bitmap img;

                if (Path.GetExtension(ofdFile.FileName) == ".webp")
                {
                    WebP webp = new WebP();
                    img = webp.Load(ofdFile.FileName);
                }
                else
                {
                    img = new Bitmap(ofdFile.FileName);
                }
                pibImg.Image = img;

                lsbMetadata.Items.Clear();

                IEnumerable <MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(ofdFile.FileName);
                foreach (var dir in directories)
                {
                    foreach (var item in dir.Tags)
                    {
                        Console.WriteLine(item);
                        lsbMetadata.Items.Add(item);
                    }
                }
            }
        }
 /// <summary>convert webp image to png</summary>
 /// <param name="sourceUrl">valid source image path</param>
 /// <param name="destPath">destination image path that saved image there</param>
 /// <param name="compress">compress image if that true</param>
 /// <returns>return true if do correctly else return false</returns>
 public static bool WebPToPng(string sourcePath, string destPath, bool compress = false)
 {
     try
     {
         if (String.IsNullOrEmpty(sourcePath) || String.IsNullOrEmpty(destPath))
         {
             return(false);
         }
         using (WebP webp = new WebP())
         {
             var bmp       = webp.Load(sourcePath);
             var webp_byte = webp.LoadByte(sourcePath);
             if (compress)
             {
                 bmp = webp.GetThumbnailFast(webp_byte, bmp.Width, bmp.Height);
             }
             else
             {
                 bmp = webp.GetThumbnailQuality(webp_byte, bmp.Width, bmp.Height);
             }
             bmp.Save(destPath, System.Drawing.Imaging.ImageFormat.Png);
         }
         return(true);
     }
     catch { throw; }
 }
Exemple #5
0
 private static Bitmap ReadWebP(String filePath)
 {
     using (WebP webp = new WebP())
     {
         Bitmap bitmap = webp.Load(filePath);
         return(bitmap);
     }
 }
        public static string ConvertWebPToTempPngFile(string webPPath)
        {
            string tempIcon = ApplicationDataUtils.GenerateNonExistentFilePath(extension: ".png");

            IOUtils.EnsureParentDirExists(tempIcon);
            using (var webP = new WebP())
                webP.Load(webPPath).Save(tempIcon, ImageFormat.Png);
            return(tempIcon);
        }
Exemple #7
0
        /// <summary>
        /// Loads an image from disk into the supplied CachedImage object
        /// </summary>
        /// <param name="data">CachedImage object containg the filename to load</param>
        private static void LoadImage(object data)
        {
            bool done  = false;
            int  retry = 0;

            do
            {
                try
                {
                    CachedImage ci = (CachedImage)data;

                    if (Path.GetExtension(ci.sFilename) == ".webp")
                    {
                        using (WebP webp = new WebP())
                            ci.img = webp.Load(ci.sFilename);
                    }
                    else
                    {
                        ci.img = Image.FromFile(ci.sFilename);
                    }

                    done = true;
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine("ImageCache ERROR: " + ex.ToString());
                    System.Console.WriteLine("ImageCache exception HResult: " + ex.HResult);

                    // only attempt retries if the reason for the failure was a sharing violation
                    // [or a file not found] error; otherwise assume the error is permanent
                    if (ex.HResult == unchecked ((int)0x80070020) // ERROR_SHARING_VIOLATION
                        // || ex.HResult == unchecked((int)0x80070002) // ERROR_FILE_NOT_FOUND
                        )
                    {
                        System.Console.WriteLine(" ==> ERROR_SHARING_VIOLATION.");
                    }
                    else
                    {
                        done = true;
                    }
                }
                if (!done)
                {
                    retry++;
                    System.Console.WriteLine(" failed to load image, attempting retry #" + retry);
                    // minor delay as workaround for moved files being occasionally still locked by the AV
                    System.Threading.Thread.Sleep(150);
                }
            } while ((retry < 3) && (done == false));
        }
Exemple #8
0
        /// <summary>
        /// Test GetPictureDistortion function
        /// </summary>
        private void ButtonMeasure_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.pictureBox.Image == null)
                {
                    MessageBox.Show("Please, load an reference image first");
                }

                using (OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog())
                {
                    openFileDialog.Filter   = "WebP images (*.webp)|*.webp";
                    openFileDialog.FileName = "";
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        Bitmap  source;
                        Bitmap  reference;
                        float[] result;

                        //Load Bitmaps
                        source = (Bitmap)this.pictureBox.Image;
                        using (WebP webp = new WebP())
                            reference = webp.Load(openFileDialog.FileName);

                        //Measure PSNR
                        using (WebP webp = new WebP())
                            result = webp.GetPictureDistortion(source, reference, 0);
                        MessageBox.Show("Red: " + result[0] + "dB.\nGreen: " + result[1] + "dB.\nBlue: " + result[2] + "dB.\nAlpha: " + result[3] + "dB.\nAll: " + result[4] + "dB.", "PSNR");

                        //Measure SSIM
                        using (WebP webp = new WebP())
                            result = webp.GetPictureDistortion(source, reference, 1);
                        MessageBox.Show("Red: " + result[0] + "dB.\nGreen: " + result[1] + "dB.\nBlue: " + result[2] + "dB.\nAlpha: " + result[3] + "dB.\nAll: " + result[4] + "dB.", "SSIM");

                        //Measure LSIM
                        using (WebP webp = new WebP())
                            result = webp.GetPictureDistortion(source, reference, 2);
                        MessageBox.Show("Red: " + result[0] + "dB.\nGreen: " + result[1] + "dB.\nBlue: " + result[2] + "dB.\nAlpha: " + result[3] + "dB.\nAll: " + result[4] + "dB.", "LSIM");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\nIn WebPExample.buttonMeasure_Click", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemple #9
0
 private static Image getfromWebP(string fullpath)
 {
     try
     {
         if (!Helpers.isExiestWebpDLL())
         {
             MessageBox.Show("libwebp_x86.dll and/or libwebp_x64.dll are missing.\n\nPlease Place them at \n"
                             + Helpers.WEBPDLLPATH, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
             return(null);
         }
         WebP  webp = new WebP();
         Image img  = new Bitmap(webp.Load(fullpath));
         webp.Dispose();
         return(img);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         MessageBox.Show(ex.Message + "\r\nIn GetImage.getfromWebP", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return(null);
     }
 }
        public static Image GetImage(String filePath)
        {
            Image SourceImg = null;

            if (filePath.IndexOf(".webp") > 0)
            {
                WebP   webp = new WebP();
                Bitmap bmp  = webp.Load(filePath);
                SourceImg = bmp;
            }
            else
            {
                FileStream fs    = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                byte[]     bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                fs.Close();
                MemoryStream ms = new MemoryStream(bytes);

                SourceImg = (Image) new Bitmap(ms);
            }
            return(SourceImg);
        }
Exemple #11
0
        private void LoadCurrentPathImage()
        {
            var fullPath = _model.DirectoryPath + _model.CurrentPicturePath;

            if (!File.Exists(fullPath))
            {
                pictureBox1.Image = new Bitmap(20, 20);
                return;
            }

            if (_model.CurrentPicturePath.EndsWith(".webp"))
            {
                pictureBox1.Image = webpWrapper.Load(fullPath);
            }
            else
            {
                using (var stream = File.OpenRead(fullPath))
                {
                    pictureBox1.Image = Image.FromStream(stream);
                }
            }
        }
        public void Execute(IJobExecutionContext context)
        {
            //IDbCommandInterceptor interceptor = new DbCommandInterceptor();
            //dbcontext.Session.AddInterceptor(interceptor);

            DateTime         dt        = DateTime.Now;
            string           shortdate = dt.ToString("yyyy-MM-dd");
            IQuery <Page>    cpq       = dbcontext.Query <Page>();
            IQuery <Chapter> cq        = dbcontext.Query <Chapter>();

            List <Page> plst;

            if (m_DownTotal > 0)
            {
                plst = cpq.Where(a => a.pagestate == PageState.None && a.Id % m_DownTotal == m_DownIndex).Take(100).ToList();
            }
            else
            {
                plst = cpq.Where(a => a.pagestate == PageState.None).Take(100).ToList();
            }

            //if (plst.Count < pageSize)
            //    pageIndex = 1;
            //else
            //    pageIndex++;

            HttpWebHelper web = new HttpWebHelper();

            foreach (var p in plst)
            {
                string filePath = AppDomain.CurrentDomain.BaseDirectory + "DownLoadImgs/" + p.Id + ".jpg";
                try
                {
                    string refer = "";
                    if (p.source == Source.dongmanmanhua || p.source == Source.dmzj)
                    {
                        var chapter = cq.Where(x => x.chapterid == p.chapterid).FirstOrDefault();
                        refer = chapter.chapterurl;
                        Stream stream = web.GetStream(p.pagesource, 3000, "", null, refer, null);
                        Image  img    = Image.FromStream(stream);

                        stream.Close();

                        img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                    else
                    {
                        WebClient myclient = new WebClient();
                        myclient.DownloadFile(p.pagesource, filePath);
                    }

                    string localimg = UcHelper.uploadFile("Page/" + p.Id + ".jpg", filePath);
                    p.pagelocal = localimg;
                    p.pagestate = PageState.成功;
                    p.modify    = dt;
                    dbcontext.Update(p);

                    File.Delete(filePath);
                }
                catch (Exception ex)
                {
                    try
                    {
                        WebClient myclient = new WebClient();
                        myclient.DownloadFile(p.pagesource, filePath);
                        Bitmap bytes;
                        using (WebP webp = new WebP())
                            bytes = webp.Load(filePath);
                        File.Delete(filePath);
                        bytes.Save(filePath);
                        string localimg = UcHelper.uploadFile("Page/" + p.Id + ".jpg", filePath);
                        p.pagelocal = localimg;
                        p.modify    = dt;
                        p.pagestate = PageState.成功;
                        dbcontext.Update(p);

                        File.Delete(filePath);
                        continue;
                    }
                    catch (Exception)
                    {
                        logger.Error(ex.Message);
                        Chapter chapter = cq.Where(x => x.chapterid == p.chapterid).FirstOrDefault();
                        chapter.retry  = chapter.retry + 1;
                        chapter.modify = dt;
                        if (chapter.retry > 30)
                        {
                            p.pagestate = PageState.失败;
                            p.modify    = dt;
                            dbcontext.Update(p);
                        }
                        dbcontext.Update(chapter);

                        //dbcontext.Update<Chapter>(a => a.chapterid == p.chapterid, a => new Chapter()
                        //{
                        //    retry = a.retry + 1,
                        //    modify = dt
                        //});
                        Err_PageJob err = new Err_PageJob();
                        err.imgurl    = p.pagesource;
                        err.source    = p.source;
                        err.errtype   = ErrPage.限制访问;
                        err.modify    = dt;
                        err.shortdate = shortdate;
                        err.message   = ex.Message;
                        err           = dbcontext.Insert(err);
                        continue;
                    }
                }
            }
        }
Exemple #13
0
 public void Start()
 {
     Controller.Instance.Popup.SetText("Please select any <#FF6666>Image<#FFFFFF> file to import.", false, () => {
         StandaloneFileBrowser.OpenFilePanelAsync("Import image", "", new ExtensionFilter[] { new ExtensionFilter("Image", new string[] { "png", "jpg", "jpeg", "bmp", "gif", "acnl", "webp" }) }, false, (path) =>
         {
             if (path.Length > 0)
             {
                 try
                 {
                     if (path[0].EndsWith(".acnl"))
                     {
                         var bytes = System.IO.File.ReadAllBytes(path[0]);
                         this.SetBytes(bytes);
                     }
                     else
                     {
                         Bitmap bmp       = null;
                         var imageStream  = new System.IO.FileStream(path[0], System.IO.FileMode.Open, System.IO.FileAccess.Read);
                         byte[] fourBytes = new byte[4];
                         imageStream.Read(fourBytes, 0, 4);
                         if (fourBytes[0] == 0x52 && fourBytes[1] == 0x49 && fourBytes[2] == 0x46 && fourBytes[3] == 0x46)
                         {
                             using (WebP webp = new WebP())
                                 bmp = webp.Load(path[0]);
                             imageStream.Close();
                             imageStream.Dispose();
                         }
                         else
                         {
                             bmp = new Bitmap(System.Drawing.Image.FromFile(path[0]));
                             imageStream.Close();
                             imageStream.Dispose();
                         }
                         this.SetImage(bmp);
                     }
                 }
                 catch (System.IO.FileLoadException e)
                 {
                     Controller.Instance.Popup.SetText("Failed to load the file. File error.", false, () =>
                     {
                         return(true);
                     });
                     return;
                 }
                 catch (Exception e)
                 {
                     Controller.Instance.Popup.SetText("Invalid image file.", false, () =>
                     {
                         return(true);
                     });
                     return;
                 }
                 Controller.Instance.PatternSelector.ActionMenu.Close();
                 Controller.Instance.SwitchToPatternEditor(
                     () =>
                 {
                     Controller.Instance.SwitchToNameInput(
                         () =>
                     {
                         ResultPattern.Name = this.Name;
                         Controller.Instance.CurrentSavegame.DesignPatterns[Pattern.Index].CopyFrom(ResultPattern);
                         _IsFinished = true;
                     },
                         () =>
                     {
                         _IsFinished = true;
                     }
                         );
                 },
                     () =>
                 {
                     _IsFinished = true;
                 }
                     );
             }
         });
         return(true);
     });
 }