Exemple #1
0
 public ImageFromFile(string directoryFullPath, string directoryRelativePath, string nameWithoutPathAndExtension, ImageFiletypes type)
 {
     imageFileFullPath = directoryFullPath;
     imageFileDirectoryRelativePath       = directoryRelativePath;
     imageFileNameWithoutPathAndExtension = nameWithoutPathAndExtension;
     imageFileType = type;
 }
Exemple #2
0
        private static bool SaveImage(RenderTargetBitmap rtb, ImageFiletypes FileFormat, string SaveDirectoryPath, string FileNameWithoutExtensionAndPath)
        {
            BitmapEncoder be;

            switch (FileFormat)
            {
            case ImageFiletypes.bmp:
                be = new BmpBitmapEncoder();
                break;

            case ImageFiletypes.png:
                be = new PngBitmapEncoder();
                break;

            case ImageFiletypes.jpg:
            default:
                be = new JpegBitmapEncoder();
                break;
            }

            be.Frames.Add(BitmapFrame.Create(rtb));

            string ext = ".jpeg";
            Type   t   = be.GetType();

            if (t == typeof(BmpBitmapEncoder))
            {
                ext = ".bmp";
            }
            else
            if (t == typeof(PngBitmapEncoder))
            {
                ext = ".png";
            }
            try
            {
                //check derectory exists
                if (!Directory.Exists(SaveDirectoryPath))
                {
                    Directory.CreateDirectory(SaveDirectoryPath);
                }

                //write file to directory
                using (Stream stm = File.Create(SaveDirectoryPath + "\\" + FileNameWithoutExtensionAndPath + ext))
                {
                    be.Save(stm);
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemple #3
0
        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            //check save settings
            if (images == null)
            {
                ShowMessageBoxFromResourceDictionary("warningNoResultsToSave", "warning", MessageBoxImage.Error);
                return;
            }
            if (images.Count == 0)
            {
                ShowMessageBoxFromResourceDictionary("warningNoResultsToSave", "warning", MessageBoxImage.Error);
                return;
            }

            string savePath = textBoxDestinationPath.Text;

            if (savePath.Length == 0)
            {
                ShowMessageBoxFromResourceDictionary("warningNoDestinationPath", "warning", MessageBoxImage.Error);
                return;
            }

            //disable controls to avoid parallel invoke or processing data update
            buttonLoadSourceImages.IsEnabled = false;
            tabItemTextWatermarks.IsEnabled  = false;
            buttonSave.Visibility            = System.Windows.Visibility.Collapsed;
            labelMessage.Content             = (string)Application.Current.FindResource("processingImagesMessage");

            //prepare resize dimensions if they are necessary in next processing
            //1024*768 will be default values if validation not passed
            int width  = 1024;
            int height = 768;

            try
            {
                string[] s;
                if (comboBoxResultResolution.SelectedItem != null)
                {
                    ComboBoxItem c = (ComboBoxItem)comboBoxResultResolution.SelectedItem;
                    s = c.Content.ToString().Split('*');
                }
                else
                {
                    s = comboBoxResultResolution.Text.ToString().Split('*');
                }

                width  = int.Parse(s[0]);
                height = int.Parse(s[1]);
            }
            catch (Exception)
            {
            }
            if (height > width)
            {
                int t = width;
                width  = height;
                height = t;
            }

            //prepare for convertion if necessary
            ImageFiletypes iType = ImageFiletypes.jpg;

            if (checkBoxChangeFormat.IsChecked.GetValueOrDefault())
            {
                switch (((ComboBoxItem)comboBoxFileType.SelectedItem).Content.ToString().ToLower())
                {
                case "png":
                    iType = ImageFiletypes.png;
                    break;

                case "bmp":
                    iType = ImageFiletypes.bmp;
                    break;

                case "jpg":
                default:
                    iType = ImageFiletypes.jpg;
                    break;
                }
            }

            progressBar.Maximum = images.Count;
            progressBar.Value   = 0;

            //start processing and saving in other thread to avoid UI lock
            Thread th = new Thread(new ParameterizedThreadStart(ProcessAndSaveResults));

            th.Start(new ProcessAndSaveResultsParams(checkBoxResizeResults.IsChecked.GetValueOrDefault(), width, height,
                                                     checkBoxChangeFormat.IsChecked.GetValueOrDefault(), iType, savePath));
        }
Exemple #4
0
        private void ProcessAndSaveResults(object parameters)
        {
            //image processing result counter
            int numOk = 0;

            if (parameters.GetType() != typeof(ProcessAndSaveResultsParams))
            {
                return;                                                             //do not throw exception, just do nothing
            }
            ProcessAndSaveResultsParams p  = (ProcessAndSaveResultsParams)parameters;
            ParallelOptions             po = new ParallelOptions();

            cancelTS                  = new CancellationTokenSource();
            po.CancellationToken      = cancelTS.Token;
            po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
            try
            {
                Parallel.ForEach(images, po, im =>
                {
                    po.CancellationToken.ThrowIfCancellationRequested();
                    try
                    {
                        string s = p.savePath + im.imageFileDirectoryRelativePath;

                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.CacheOption = BitmapCacheOption.None;
                        bi.UriSource   = new Uri(im.imageFileFullPath);
                        bi.EndInit();

                        ImageFiletypes curType = p.iType;
                        if (!p.needConvert)
                        {
                            curType = im.imageFileType;
                        }

                        int pw = bi.PixelWidth;
                        int ph = bi.PixelHeight;
                        if (p.needResize)
                        {
                            pw = p.width;
                            ph = p.height;
                            if (bi.PixelWidth < bi.PixelHeight)
                            {
                                int tmp = pw;
                                pw      = ph;
                                ph      = tmp;
                            }
                        }
                        //???????????????????????????????
                        //if (Watermarking.WatermarkScaleAndSaveImageFromBitmapImage(curType, bi, pw, ph, watermarks, s, im.imageFileNameWithoutPathAndExtension))

                        if (Watermarking.WatermarkScaleAndSaveImageFromBitmapImage(curType, bi, pw, ph, textWatermarks, s, im.imageFileNameWithoutPathAndExtension))
                        {
                            Interlocked.Increment(ref numOk);
                        }
                        bi = null;

                        //update progress bar
                        this.Dispatcher.Invoke(new Action(() =>
                        {
                            //multithread safe increment
                            lock (progressBar)
                            { progressBar.Value++; }
                        }));
                    }
                    catch (Exception)
                    {
                        //bad image file?
                    }
                }
                                 );
            }
            catch (Exception)
            { }

            //enable controls and write message
            this.Dispatcher.Invoke(new Action(() => {
                buttonLoadSourceImages.IsEnabled = true;
                tabItemTextWatermarks.IsEnabled  = true;
                buttonSave.Visibility            = System.Windows.Visibility.Visible;

                labelMessage.Content  = numOk.ToString() + " " + (string)Application.Current.FindResource("okProcessedImagesMessage");
                labelMessage.Content += ", " + (images.Count - numOk).ToString() + " " + (string)Application.Current.FindResource("failProcessedImagesMessage");
            }));
        }
Exemple #5
0
 public ProcessAndSaveResultsParams(bool NeedResize, int Width, int Height, bool NeedConvert, ImageFiletypes IType, string SavePath)
 {
     needResize = NeedResize; width = Width; height = Height; needConvert = NeedConvert; iType = IType; savePath = SavePath;
 }
Exemple #6
0
        public static bool WatermarkScaleAndSaveImageFromBitmapImage(ImageFiletypes fileFormat, BitmapImage sourceImage, int newPixelWidth, int newPixelHight, TextWatermarkListWithSerchByUiLabel watermarks, string saveDirectoryPath, string fileNameWithoutExtensionAndPath)
        {
            RenderTargetBitmap rtb = CreateWatermarkedBitmapEncoder(sourceImage, watermarks, newPixelWidth, newPixelHight);

            return(SaveImage(rtb, fileFormat, saveDirectoryPath, fileNameWithoutExtensionAndPath));
        }