コード例 #1
0
 public static Bitmap loadFileZoomedCentered(string path, int newHeight, int newWidth)
 {
     try
     {
         return(sameAspectResize(MagickImaging.BitmapFromUrlExt(path), newWidth, newHeight));
         // no stream because it has to stay open: https://stackoverflow.com/a/1053123/1997873
     }
     catch (OutOfMemoryException ex)
     {
         MessageBox.Show("Can't load image file:\n" + path + "\n" + ex.ToString());
     }
     return(null);
 }
コード例 #2
0
        BitmapImage LoadBitmapWithoutLockingFile(string url)
        {
            //https://social.msdn.microsoft.com/Forums/vstudio/en-US/dee7cb68-aca3-402b-b159-2de933f933f1/disposing-a-wpf-image-or-bitmapimage-so-the-source-picture-file-can-be-modified?forum=wpf

            System.Windows.Media.Imaging.BitmapImage result = new System.Windows.Media.Imaging.BitmapImage(); // Create new BitmapImage
            System.IO.Stream stream = new System.IO.MemoryStream();                                           // Create new MemoryStream

            System.Drawing.Bitmap bitmap = MagickImaging.BitmapFromUrlExt(url);                               // Create new Bitmap (System.Drawing.Bitmap) from the existing image file (albumArtSource set to its path name)
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);                                      // Save the loaded Bitmap into the MemoryStream - Png format was the only one I tried that didn't cause an error (tried Jpg, Bmp, MemoryBmp)
            bitmap.Dispose();                                                                                 // Dispose bitmap so it releases the source image file

            result.BeginInit();                                                                               // Begin the BitmapImage's initialisation
            result.StreamSource = stream;                                                                     // Set the BitmapImage's StreamSource to the MemoryStream containing the image
            result.EndInit();                                                                                 // End the BitmapImage's initialisation

            return(result);
        }
コード例 #3
0
        public void WhitePercentageTest()
        {
            using (Bitmap b1 = MagickImaging.BitmapFromUrlExt("./44.33931_per_white.png"))
            {
                Assert.AreEqual("0.4433931", MagickImaging.WhiteRatio(b1).ToString("F7"));
            }

            using (Bitmap b1 = MagickImaging.BitmapFromUrlExt("./44.33931_per_white.png"))
            {
                using (Bitmap b2 = GraphicsUtils.MakeGrayscale3(b1))
                {
                    Assert.AreEqual("0.6915526", MagickImaging.WhiteRatio(b2).ToString("F7"));
                }
            }

            using (Bitmap b1 = MagickImaging.BitmapFromUrlExt("./44.33931_per_white.png"))
            {
                using (Bitmap b2 = GraphicsUtils.MakeBW1(b1)) // Hard threshold!!
                {
                    Assert.AreEqual("1.0000000", MagickImaging.WhiteRatio(b2).ToString("F7"));
                }
            }

            using (Bitmap b1 = MagickImaging.BitmapFromUrlExt("./44.33931_per_white_gray.png"))
            {
                using (Bitmap b2 = GraphicsUtils.MakeBW1(b1)) // Hard threshold!!
                {
                    Assert.AreEqual("1.0000000", MagickImaging.WhiteRatio(b2).ToString("F7"));
                }
            }

            using (Bitmap b1 = MagickImaging.BitmapFromUrlExt("./44.33931_per_white_gray.png"))
            {
                using (Bitmap b2 = GraphicsUtils.MakeGrayscale3(b1))
                {
                    Assert.AreEqual("0.6971297", MagickImaging.WhiteRatio(b2).ToString("F7"));
                }
            }
        }
コード例 #4
0
        private void MnuExport_Click(object sender, RoutedEventArgs e)
        {
            dlgSave.Title  = "Export book as PDF";
            dlgSave.Filter = "PDF |*.pdf";
            if (dlgSave.ShowDialog() == true)
            {
                FileInfo fi          = new FileInfo(dlgSave.FileName);
                int      saveCounter = 0;

                var pageInfo = new PageInfo((string)((ComboBoxItem)cbPageSize.SelectedItem).Content, float.Parse(txtPrintPadding.Text));

                int pW  = pageInfo.singlePageWidth;
                int pH  = pageInfo.singlePageHeight;
                int pad = pageInfo.paddingPx;

                int           pagesCount    = allPrintPages.Count;
                bool          convertPdf    = !(cbExportMinimal.IsChecked ?? false);
                bool          keepColors    = cbKeepColors.IsChecked ?? false;
                bool          parentText    = cbIncludeParent.IsChecked ?? false;
                List <string> filesToDelete = new List <string>();

                DateTime timeTemp;
                TimeSpan timeTemplates = TimeSpan.FromSeconds(0);
                TimeSpan timeAddPages  = TimeSpan.FromSeconds(0);
                TimeSpan timeSavePdf   = TimeSpan.FromSeconds(0);

                timeTemp = DateTime.Now;
                Exception ex = winWorking.waitForTask <Exception>(this, (updateFunc) =>
                {
                    DuplexTemplates dt = new DuplexTemplates(Properties.Resources.GitInfo.Replace("\"", "").Split(' ')[0]);
                    foreach (SelectablePrintPage page in allPrintPages)
                    {
                        try
                        {
                            updateFunc("[1/3] Export page " + page.PageNumber, (int)(100.0f * saveCounter / 2 / pagesCount));

                            var b     = dt.BuildFace(page.Front, pW, pH, pad, keepColors, parentText);
                            var bName = System.IO.Path.Combine(
                                fi.Directory.FullName,
                                "_temp_" + String.Format("{0:000000000}", saveCounter++) + ".jpg"
                                );
                            b.Save(bName);
                            filesToDelete.Add(bName);
                            b.Dispose();

                            b     = dt.BuildFace(page.Back, pW, pH, pad, keepColors, parentText);
                            bName = System.IO.Path.Combine(
                                fi.Directory.FullName,
                                "_temp_" + String.Format("{0:000000000}", saveCounter++) + ".jpg"
                                );
                            b.Save(bName);
                            filesToDelete.Add(bName);
                            b.Dispose();

                            b = null;
                        }
                        catch (Exception ex1)
                        {
                            return(new Exception("Exception exporting page " + page.PageNumber, ex1));
                        }
                    }

                    return(null);
                },
                                                                  isProgressKnwon: true);
                timeTemplates = DateTime.Now - timeTemp;

                if (ex != null)
                {
                    MessageBox.Show("Error occured while exporting pdf (image step).\n" + ex.ToString());
                }
                else
                {
                    if (convertPdf)
                    {
                        using (Core.MagickImaging pdfMagik = new MagickImaging())
                        {
                            timeTemp = DateTime.Now;
                            ex       = winWorking.waitForTask <Exception>(this, (updateFunc) =>
                            {
                                try
                                {
                                    Action <int> onUpdateIndex = new Action <int>((index) =>
                                    {
                                        updateFunc("[2/3] Adding pdf page " + index, (int)(100.0f * index / filesToDelete.Count));
                                    });
                                    pdfMagik.MakeList(filesToDelete, fi.Directory.FullName, pageInfo.pageDepth, updateIndex: onUpdateIndex);
                                }
                                catch (Exception ex2)
                                {
                                    return(ex2);
                                }

                                return(null);
                            }, true);
                            timeAddPages = DateTime.Now - timeTemp;

                            if (ex == null)
                            {
                                timeTemp = DateTime.Now;
                                ex       = winWorking.waitForTask <Exception>(this, (updateFunc) =>
                                {
                                    try
                                    {
                                        updateFunc("[3/3] Saving pdf to file...", 0);
                                        pdfMagik.SaveListToPdf(fi.FullName);
                                    }
                                    catch (Exception ex2)
                                    {
                                        return(ex2);
                                    }

                                    return(null);
                                }, false);
                                timeSavePdf = DateTime.Now - timeTemp;
                            }
                        }
                    }

                    if (ex != null)
                    {
                        MessageBox.Show("Error occured while exporting pdf (convert step).\n" + ex.ToString());
                    }
                    else
                    {
                        if (convertPdf)
                        {
                            foreach (var f in filesToDelete)
                            {
                                File.Delete(f);
                            }
                        }

                        string TimingInfo =
                            "1) Save pages  - " + timeTemplates.ToString() + "\n" +
                            "2) Add to PDF  - " + timeAddPages.ToString() + "\n" +
                            "3) PDF to File - " + timeSavePdf.ToString();
                        MessageBox.Show(this,
                                        "Export done successfully!\n" +
                                        "===============\n" +
                                        TimingInfo
                                        , "Done");
                    }
                }
            }
        }
コード例 #5
0
        private void StartWhiteRatioScan()
        {
            MessageBoxResult quick = MessageBox.Show("Should we do a quick scan? The first\\last 3 pages only?",
                                                     "White Ratio options",
                                                     MessageBoxButton.YesNoCancel
                                                     );

            if (quick == MessageBoxResult.Cancel)
            {
                return;
            }
            bool isQuick = quick == MessageBoxResult.Yes;

            int      TotlaPageCount = mangaChapters.Sum(p => isQuick ? Math.Min(6, p.Pages.Count) : p.Pages.Count);
            DateTime start          = DateTime.Now;

            shouldUpdateStats = false; // Because we are updating the stats which will update ui in the middle

            Exception ex = winWorking.waitForTask <Exception>(this, (updateFunc) =>
            {
                try
                {
                    int pageCounter = 0;
                    foreach (var ch in mangaChapters)
                    {
                        for (int i = 0; i < ch.Pages.Count; i++)
                        {
                            var page = ch.Pages[i];

                            if (!isQuick || i < 3 || i > ch.Pages.Count - 1 - 3)
                            {
                                pageCounter++;
                                updateFunc(
                                    "Processing: " + ch.Name + " -> " + page.Name,
                                    (int)(100.0f * pageCounter / TotlaPageCount)
                                    );
                                using (Bitmap b1 = MagickImaging.BitmapFromUrlExt(page.ImagePath))
                                {
                                    using (Bitmap b2 = GraphicsUtils.MakeBW1(b1))
                                    {
                                        page.WhiteBlackRatio = MagickImaging.WhiteRatio(b1);
                                    }
                                }
                            }
                        }
                        ch.updateChapterStats();
                    }
                }
                catch (Exception ex2)
                {
                    return(ex2);
                }

                return(null);
            }, true);

            shouldUpdateStats      = true;
            lblChCount.DataContext = null;
            lblChCount.DataContext = mangaChapters;

            if (ex != null)
            {
                MessageBox.Show("Error occured while reading white ratio (convert step).\n" + ex.ToString());
            }
            else
            {
                MessageBox.Show("Done!, Took:" + (DateTime.Now - start).ToString());
            }
        }