Exemplo n.º 1
0
 /// <summary>
 /// load file extension and create output path
 /// </summary>
 private void LoadFileInfo()
 {
     //extension of input image
     inputFileExtension = System.IO.Path.GetExtension(InputImagePath).Substring(1).ToLower();
     //extension of output image
     OutputFileExtension = OutputFileExtension.ToLower();
     //path for created image
     outputImagePath = System.IO.Path.ChangeExtension(InputImagePath, $".{OutputFileExtension}");
 }
Exemplo n.º 2
0
        private static void WriteToFile(string fileName, OutputFileExtension fileExtension, string fileContents)
        {
            const char backSlash = '\\';
            string     directory = $"{CurrentDirectory}{backSlash}{Folder.Outputs}";

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            string filePath = $"{directory}{backSlash}{fileName}.{fileExtension}";

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            using (StreamWriter sw = File.CreateText(filePath))
            {
                sw.Write(fileContents);
            }

            Console.WriteLine($"Wrote file to {filePath}");
        }
        public Tuple <string, Size> Convert(Bitmap bmp, string inputfilename, string outputfolder, DateTime filetime, Action <string> logit, string bodyname, string systemname, string cmdrname) // can call independent of watcher, pass in bmp to convert
        {
            outputfolder = SubFolder(outputfolder, systemname, cmdrname, filetime);

            // bmp is the original bitmap at full res

            int index = 0;

            string outputfilename;
            string secondfilename, thirdfilename;

            do                                          // add _N on the filename for index>0, to make them unique.
            {
                string fn = CreateFileName(systemname, bodyname, inputfilename, FileNameFormat, HighRes, filetime) + (index == 0 ? "" : "_" + index);

                outputfilename = Path.Combine(outputfolder, fn + "." + OutputFileExtension.ToString());
                secondfilename = Path.Combine(outputfolder, fn + "-1." + OutputFileExtension.ToString());
                thirdfilename  = Path.Combine(outputfolder, fn + "-2." + OutputFileExtension.ToString());
                index++;
            } while (File.Exists(outputfilename));          // if name exists, pick another

            if (outputfilename.Equals(inputfilename, StringComparison.InvariantCultureIgnoreCase))
            {
                logit(string.Format(("Cannot convert {0} to {1} as names clash" + Environment.NewLine + "Pick a different conversion folder or a different output format"), inputfilename, outputfilename));
                return(new Tuple <string, Size>(null, Size.Empty));
            }

            // the OutputFilename should point to the best screenshot, and FinalSize points to this

            Size finalsize = Size.Empty;

            if (CropResizeImage1 == CropResizeOptions.Off || KeepMasterConvertedImage) // if resize 1 off, or keep full size.
            {
                WriteBMP(bmp, outputfilename, filetime);
                finalsize = bmp.Size;        // this is our image to use in the rest of the system
            }

            if (CropResizeImage1 != CropResizeOptions.Off)
            {
                string nametouse = KeepMasterConvertedImage ? secondfilename : outputfilename;     // if keep full sized off, we use this one as our image

                if (CropResizeImage1 == CropResizeOptions.Crop)
                {
                    Bitmap cropped = bmp.CropImage(CropResizeArea1);
                    WriteBMP(cropped, nametouse, filetime);
                    cropped.Dispose();
                }
                else
                {
                    Bitmap resized = bmp.ResizeImage(CropResizeArea1.Width, CropResizeArea1.Height);
                    WriteBMP(resized, nametouse, filetime);
                    resized.Dispose();
                }

                if (!KeepMasterConvertedImage)       // if not keeping the full sized one, its final
                {
                    finalsize = bmp.Size;
                }
            }

            if (CropResizeImage2 == CropResizeOptions.Crop)
            {
                Bitmap cropped = bmp.CropImage(CropResizeArea2);
                WriteBMP(cropped, thirdfilename, filetime);
                cropped.Dispose();
            }
            else if (CropResizeImage2 == CropResizeOptions.Resize)
            {
                Bitmap resized = bmp.ResizeImage(CropResizeArea2.Width, CropResizeArea2.Height);
                WriteBMP(resized, thirdfilename, filetime);
                resized.Dispose();
            }

            if (CopyToClipboard)
            {
                using (Image bmpc = Bitmap.FromFile(outputfilename))
                {
                    try
                    {
                        System.Windows.Forms.Clipboard.SetImage(bmpc);
                    }
                    catch
                    {
                        logit("Copying image to clipboard failed");
                    }
                }
            }

            if (RemoveOriginal)
            {
                try
                {
                    File.Delete(inputfilename);
                }
                catch
                {
                    logit($"Unable to remove file {inputfilename}");
                }
            }

            System.Diagnostics.Debug.WriteLine("Convert " + inputfilename + " at " + systemname + " to " + outputfilename);

            logit(string.Format("Converted {0} to {1}".T(EDTx.ScreenShotImageConverter_CNV), Path.GetFileName(inputfilename), outputfilename));

            return(new Tuple <string, Size>(outputfilename, finalsize));
        }
        // helpers for above

        private bool UpdateOutputFolderWithSubFolder()
        {
            if (String.IsNullOrWhiteSpace(OutputFolder))
            {
                OutputFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "Frontier Developments", "Elite Dangerous", "Converted");
            }

            switch (FolderFormatIndex)
            {
            case 1:         // system name
                OutputFolder += "\\" + SystemName.SafeFileString();
                break;

            case 2:         // "YYYY-MM-DD"
                OutputFolder += "\\" + Timestamp.ToString("yyyy-MM-dd");
                break;

            case 3:         // "DD-MM-YYYY"
                OutputFolder += "\\" + Timestamp.ToString("dd-MM-yyyy");
                break;

            case 4:         // "MM-DD-YYYY"
                OutputFolder += "\\" + Timestamp.ToString("MM-dd-yyyy");
                break;

            case 5:      //"YYYY-MM-DD Sysname",
                OutputFolder += "\\" + Timestamp.ToString("yyyy-MM-dd") + " " + SystemName.SafeFileString();
                break;

            case 6:      //"DD-MM-YYYY Sysname",
                OutputFolder += "\\" + Timestamp.ToString("dd-MM-yyyy") + " " + SystemName.SafeFileString();
                break;

            case 7:     //"MM-DD-YYYY Sysname"
                OutputFolder += "\\" + Timestamp.ToString("MM-dd-yyyy") + " " + SystemName.SafeFileString();
                break;

            case 8:     // CMDR name
                OutputFolder += "\\" + (EDCommander.GetCommander(CommanderID)?.Name ?? $"CmdrId{CommanderID}").SafeFileString();
                break;

            case 9:     // CMDR name at sysname
                OutputFolder += "\\" + (EDCommander.GetCommander(CommanderID)?.Name ?? $"CmdrId{CommanderID}").SafeFileString() + " at " + SystemName.SafeFileString();
                break;

            case 10:     // YYYY - MM - DD CMDR name at sysname
                OutputFolder += "\\" + Timestamp.ToString("yyyy-MM-dd") + " " +
                                (EDCommander.GetCommander(CommanderID)?.Name ?? $"CmdrId{CommanderID}").SafeFileString() + " at " + SystemName.SafeFileString();
                break;

            case 11:     // CMDR Name \ SystemName
                OutputFolder += "\\" + (EDCommander.GetCommander(CommanderID)?.Name ?? $"CmdrId{CommanderID}").SafeFileString() + "\\" + SystemName.SafeFileString();
                break;
            }

            if (!Directory.Exists(OutputFolder))
            {
                Directory.CreateDirectory(OutputFolder);
            }

            return(!(OutputFolder.Equals(InputFolder) && OutputFileExtension.ToString().Equals(InputFileExtension.ToString())));
        }
        private Bitmap ConvertImage(Bitmap bmp)
        {
            int index = 0;

            string bodyname = (JournalScreenShot.Body == null) ? "" : (JournalScreenShot.Body.Equals(SystemName, StringComparison.InvariantCultureIgnoreCase) ? "" : JournalScreenShot.Body);

            do                                          // add _N on the filename for index>0, to make them unique.
            {
                OutputFilename = Path.Combine(OutputFolder, CreateFileName(SystemName, bodyname, InputFilename, FilenameFormatIndex, HighRes, Timestamp) + (index == 0 ? "" : "_" + index) + "." + OutputFileExtension.ToString());
                index++;
            } while (File.Exists(OutputFilename));          // if name exists, pick another

            System.Drawing.Bitmap croppedbmp = null;

            if (CropImage)
            {
                /* check that crop settings are within the image, otherwise adjust. */
                if ((CropArea.Width <= 0) || (CropArea.Width > bmp.Width))
                {
                    CropArea.X     = 0;
                    CropArea.Width = bmp.Width;
                }
                else if (CropArea.Left + CropArea.Width > bmp.Width)
                {
                    CropArea.X = bmp.Width - CropArea.Width;
                }
                if ((CropArea.Height <= 0) || (CropArea.Height > bmp.Height))
                {
                    CropArea.Y      = 0;
                    CropArea.Height = bmp.Height;
                }
                else if (CropArea.Top + CropArea.Height > bmp.Height)
                {
                    CropArea.Y = bmp.Height - CropArea.Height;
                }

                /* Only crop if we need to */
                if ((CropArea.Width != bmp.Width) || (CropArea.Height != bmp.Height))
                {                                                   // CLONE new one, which creates a new object
                    croppedbmp = bmp.Clone(CropArea, System.Drawing.Imaging.PixelFormat.DontCare);
                }
                else
                {
                    croppedbmp = bmp;           // just copy reference.. no need to crop.
                }
            }
            else
            {
                croppedbmp = bmp;               // just copy reference..
            }
            return(croppedbmp);
        }
        // convert bmp from inputfilename with filetime
        // into outputfolder with properties body,system,cmdrname
        // return final inputfilename, output filename, size. or null if failed.

        public Tuple <string, string, Size> Convert(Bitmap bmp, string inputfilename, DateTime filetime, string outputfolder,
                                                    string bodyname, string systemname, string cmdrname, Action <string> logit) // can call independent of watcher, pass in bmp to convert
        {
            outputfolder = SubFolder(FolderNameFormat, outputfolder, systemname, cmdrname, filetime);

            if (!Directory.Exists(outputfolder))
            {
                Directory.CreateDirectory(outputfolder);
            }

            // bmp is the original bitmap at full res

            int index = 0;

            string outputfilename;
            string secondfilename, thirdfilename;

            do                                          // add _N on the filename for index>0, to make them unique.
            {
                string fn = CreateFileName(systemname, bodyname, inputfilename, FileNameFormat, HighRes, filetime) + (index == 0 ? "" : "_" + index);

                outputfilename = Path.Combine(outputfolder, fn + "." + OutputFileExtension.ToString());
                secondfilename = Path.Combine(outputfolder, fn + "-1." + OutputFileExtension.ToString());
                thirdfilename  = Path.Combine(outputfolder, fn + "-2." + OutputFileExtension.ToString());
                index++;
            } while (File.Exists(outputfilename));          // if name exists, pick another

            if (outputfilename.Equals(inputfilename, StringComparison.InvariantCultureIgnoreCase))
            {
                logit(string.Format(("Cannot convert {0} to {1} as names clash" + Environment.NewLine + "Pick a different conversion folder or a different output format"), inputfilename, outputfilename));
                return(null);
            }

            // the OutputFilename should point to the best screenshot, and FinalSize points to this

            Size finalsize = Size.Empty;

            if (CropResizeImage1 == CropResizeOptions.Off || KeepMasterConvertedImage) // if resize 1 off, or keep full size.
            {
                WriteBMP(bmp, outputfilename, filetime);
                finalsize = bmp.Size;        // this is our image to use in the rest of the system

                if (ClipboardOption == ClipboardOptions.CopyMaster)
                {
                    CopyClipboardSafe(bmp, logit);
                }
            }

            if (CropResizeImage1 != CropResizeOptions.Off)
            {
                Bitmap converted = null;

                if (CropResizeImage1 == CropResizeOptions.Crop)
                {
                    converted = bmp.CropImage(CropResizeArea1);
                }
                else
                {
                    converted = bmp.ResizeImage(CropResizeArea1.Width, CropResizeArea1.Height);
                }

                string nametouse = KeepMasterConvertedImage ? secondfilename : outputfilename;     // if keep full sized off, we use this one as our image
                WriteBMP(converted, nametouse, filetime);

                if (!KeepMasterConvertedImage)       // if not keeping the full sized one, its final
                {
                    finalsize = converted.Size;
                }

                if (ClipboardOption == ClipboardOptions.CopyImage1)
                {
                    CopyClipboardSafe(converted, logit);
                }

                converted.Dispose();
            }

            if (CropResizeImage2 != CropResizeOptions.Off)
            {
                Bitmap converted = null;

                if (CropResizeImage2 == CropResizeOptions.Crop)
                {
                    converted = bmp.CropImage(CropResizeArea2);
                }
                else
                {
                    converted = bmp.ResizeImage(CropResizeArea2.Width, CropResizeArea2.Height);
                }

                WriteBMP(converted, thirdfilename, filetime);

                if (ClipboardOption == ClipboardOptions.CopyImage2)
                {
                    CopyClipboardSafe(converted, logit);
                }

                converted.Dispose();
            }

            if (OriginalImageOption == OriginalImageOptions.Delete)
            {
                try
                {
                    System.Diagnostics.Debug.WriteLine("Delete {0}", inputfilename);
                    File.Delete(inputfilename);
                    inputfilename = null;
                }
                catch
                {
                    logit($"Unable to remove file {inputfilename}");
                }
            }
            else if (OriginalImageOption == OriginalImageOptions.Move)
            {
                string outfile = Path.Combine(OriginalImageOptionDirectory, Path.GetFileNameWithoutExtension(outputfilename) + Path.GetExtension(inputfilename));
                int    indexi  = 1;
                while (true)
                {
                    try
                    {
                        System.Diagnostics.Debug.WriteLine("Move {0} to {1}", inputfilename, outfile);
                        File.Move(inputfilename, outfile);
                        inputfilename = outfile;
                        break;
                    }
                    catch
                    {
                        outfile = Path.Combine(OriginalImageOptionDirectory, Path.GetFileNameWithoutExtension(outfile) + "-" + indexi++.ToString() + Path.GetExtension(outfile));
                    }
                }
            }

            System.Diagnostics.Debug.WriteLine("Convert " + inputfilename + " at " + systemname + " to " + outputfilename);

            logit(string.Format("Converted {0} to {1}".T(EDTx.ScreenShotImageConverter_CNV), Path.GetFileName(inputfilename), outputfilename));

            return(new Tuple <string, string, Size>(inputfilename, outputfilename, finalsize));
        }
Exemplo n.º 7
0
        private void SaveOutputWriterKeys()
        {
            Manager dm  = new Manager();
            IdpeKey key = new IdpeKey();

            key.Name  = IdpeKeyTypes.OutputPartialRecordsAllowed.ToString();
            key.Value = AllowPartial.ToString();
            key.Type  = (int)IdpeKeyTypes.OutputPartialRecordsAllowed;
            dm.Save(key, DataSource.Id);

            key       = new IdpeKey();
            key.Name  = IdpeKeyTypes.OutputIsFirstRowHeader.ToString();
            key.Value = OutputHasHeader.ToString();
            key.Type  = (int)IdpeKeyTypes.OutputIsFirstRowHeader;
            dm.Save(key, DataSource.Id);


            switch (OutputDataFormatType)
            {
            case OutputTypes.Xml:
                dm.DeleteKeyFromApplication(this.DataSource.Id, IdpeKeyTypes.OutputDelimiter.ToString(), true);
                dm.DeleteKeyFromApplication(this.DataSource.Id, IdpeKeyTypes.OutputIsFirstRowHeader.ToString(), true);
                dm.DeleteKeyFromApplication(this.DataSource.Id, IdpeKeyTypes.OutputDelimiterDoNotEncloseWithDoubleQuote.ToString(), true);
                break;

            case OutputTypes.Delimited:

                key      = new IdpeKey();
                key.Name = IdpeKeyTypes.OutputDelimiter.ToString();
                if (OutputDelimiter.ToLower() == "tab")
                {
                    key.Value = "\t";
                }
                else if (string.IsNullOrEmpty(OutputDelimiter))
                {
                    key.Value = ",";
                }
                else
                {
                    key.Value = OutputDelimiter;
                }
                key.Type = (int)IdpeKeyTypes.OutputDelimiter;
                dm.Save(key, DataSource.Id);


                key       = new IdpeKey();
                key.Name  = IdpeKeyTypes.OutputDelimiterDoNotEncloseWithDoubleQuote.ToString();
                key.Value = (!EncloseWithDoubleQuote).ToString();
                key.Type  = (int)IdpeKeyTypes.OutputDelimiterDoNotEncloseWithDoubleQuote;
                dm.Save(key, DataSource.Id);

                break;

            case OutputTypes.FixedLength:
                dm.DeleteKeyFromApplication(this.DataSource.Id, IdpeKeyTypes.OutputDelimiter.ToString(), true);
                dm.DeleteKeyFromApplication(this.DataSource.Id, IdpeKeyTypes.OutputIsFirstRowHeader.ToString(), true);
                dm.DeleteKeyFromApplication(this.DataSource.Id, IdpeKeyTypes.OutputDelimiterDoNotEncloseWithDoubleQuote.ToString(), true);
                break;

            case OutputTypes.Database:
                OutputFileExtension = ".csv";
                break;

            case OutputTypes.Custom:
                break;
            }

            if (OutputFileNameFormat.ToLower() != "same as input")
            {
                key       = new IdpeKey();
                key.Name  = IdpeKeyTypes.OutputFileName.ToString();
                key.Value = OutputFileNameFormat;
                key.Type  = (int)IdpeKeyTypes.OutputFileName;
                dm.Save(key, DataSource.Id);
            }
            else
            {
                dm.DeleteKeyFromApplication(this.DataSource.Id, IdpeKeyTypes.OutputFileName.ToString(), true);
            }

            if (OutputFileExtension.ToLower() != ".xml")
            {
                key       = new IdpeKey();
                key.Name  = IdpeKeyTypes.OutputFileExtension.ToString();
                key.Value = OutputFileExtension;
                key.Type  = (int)IdpeKeyTypes.OutputFileExtension;
                dm.Save(key, DataSource.Id);
            }
            else
            {
                dm.DeleteKeyFromApplication(this.DataSource.Id, IdpeKeyTypes.OutputFileExtension.ToString(), true);
            }
        }
        public bool Convert(Bitmap bmp, Action <string> logit) // can call independent of watcher, pass in bmp to convert
        {
            OutputFilename = null;
            FinalSize      = Point.Empty;
            Converted      = false;

            if (!UpdateOutputFolderWithSubFolder())  // add on any sub folder options to output folder
            {
                logit(string.Format(("Cannot convert {0} into the same folder as they are stored into" + Environment.NewLine + "Pick a different conversion folder or a different output format").T(EDTx.ScreenShotImageConverter_FolderErr), InputFileExtension.ToString()));
                return(false);
            }

            // bmp is the original bitmap at full res

            int index = 0;

            string bodyname = (JournalScreenShot.Body == null) ? "" : (JournalScreenShot.Body.Equals(SystemName, StringComparison.InvariantCultureIgnoreCase) ? "" : JournalScreenShot.Body);

            string secondfilename, thirdfilename;

            do                                          // add _N on the filename for index>0, to make them unique.
            {
                string fn = CreateFileName(SystemName, bodyname, InputFilename, FilenameFormatIndex, HighRes, Timestamp) + (index == 0 ? "" : "_" + index);

                OutputFilename = Path.Combine(OutputFolder, fn + "." + OutputFileExtension.ToString());
                secondfilename = Path.Combine(OutputFolder, fn + "-1." + OutputFileExtension.ToString());
                thirdfilename  = Path.Combine(OutputFolder, fn + "-2." + OutputFileExtension.ToString());
                index++;
            } while (File.Exists(OutputFilename));          // if name exists, pick another

            // the OutputFilename should point to the best screenshot, and FinalSize points to this

            if (CropResizeImage1 == ScreenShotConverter.CropResizeOptions.Off || KeepMasterConvertedImage) // if resize 1 off, or keep full size.
            {
                WriteBMP(bmp, OutputFilename);
                FinalSize = new Point(bmp.Size);        // this is our image to use in the rest of the system
            }

            if (CropResizeImage1 != ScreenShotConverter.CropResizeOptions.Off)
            {
                string nametouse = KeepMasterConvertedImage ? secondfilename : OutputFilename;     // if keep full sized off, we use this one as our image

                if (CropResizeImage1 == ScreenShotConverter.CropResizeOptions.Crop)
                {
                    Bitmap cropped = bmp.CropImage(CropResizeArea1);
                    WriteBMP(cropped, nametouse);
                    cropped.Dispose();
                }
                else
                {
                    Bitmap resized = bmp.ResizeImage(CropResizeArea1.Width, CropResizeArea1.Height);
                    WriteBMP(resized, nametouse);
                    resized.Dispose();
                }

                if (!KeepMasterConvertedImage)       // if not keeping the full sized one, its final
                {
                    FinalSize = new Point(bmp.Size);
                }
            }

            if (CropResizeImage2 == ScreenShotConverter.CropResizeOptions.Crop)
            {
                Bitmap cropped = bmp.CropImage(CropResizeArea2);
                WriteBMP(cropped, thirdfilename);
                cropped.Dispose();
            }
            else if (CropResizeImage2 == ScreenShotConverter.CropResizeOptions.Resize)
            {
                Bitmap resized = bmp.ResizeImage(CropResizeArea2.Width, CropResizeArea2.Height);
                WriteBMP(resized, thirdfilename);
                resized.Dispose();
            }

            Converted = true;

            if (Converted)
            {
                if (JournalScreenShot != null)
                {
                    JournalScreenShot.SetConvertedFilename(InputFilename, OutputFilename, FinalSize.X, FinalSize.Y);
                }

                System.Diagnostics.Debug.WriteLine("Convert " + InputFilename + " at " + SystemName + " to " + OutputFilename);

                logit(string.Format("Converted {0} to {1}".T(EDTx.ScreenShotImageConverter_CNV), Path.GetFileName(InputFilename), Path.GetFileName(OutputFilename)));
            }

            return(Converted);
        }