static void Main(string[] args) { int width = 0; int height = 0; string format = ""; string loadingXML; Console.WriteLine(" Welcome to this amazing \n Photo Converter! \n Let's get started!\n"); loadingXML = OtherMethodsContainer.CheckForXmlConfig(); ImageMetaDataContainer myContainer; if (loadingXML.Equals("yes")) { string xmlFilePath = "XML file\\PhotoConverterCommands.xml"; //Defining the Container. myContainer = new ImageMetaDataContainer(xmlFilePath); //input = myContainer.input; // not needed, as it is just repackaging stuff, while it can be done in the class1 in method. and here the whole method will be simply called. } else { Tuple <string, string, string> getValueOfSourceMethod = OtherMethodsContainer.SourceMethod(); // this thing will initialise SourceMethod once and will enable to get the values of it for following variables. Console.WriteLine("\nProvide value to height or with to resize image with original aspect ratio for this size. \nOther value write '0' or press enter. \nWrite both values to loose aspect ratio but receive the demanded values"); width = OtherMethodsContainer.WidthMethod(); height = OtherMethodsContainer.HeightMethod(); format = OtherMethodsContainer.FormatMethodCheck(); Tuple <string, string, string> getValueOfDestinationMethod = OtherMethodsContainer.DestinationMethod(format); //Defining the Container. myContainer = new ImageMetaDataContainer(getValueOfSourceMethod, width, height, format, getValueOfDestinationMethod); } string joinedformatOfFileInRootFolder = "*.jpg|*.png|*.gif|*.tiff"; string[] files = OtherMethodsContainer.GetFiles(myContainer.sourceOfFolder, joinedformatOfFileInRootFolder, SearchOption.AllDirectories); //No Constructor, only one method call. PhotoConversionContainer.ImageConversionMethod(myContainer, files); Console.WriteLine("Press any key to continue"); Console.ReadKey(); }
static void Main(string[] args) { int width = 0; int height = 0; string format = ""; string loadingXML; Console.WriteLine(" Welcome to this amazing \n Photo Converter! \n Let's get started!\n"); loadingXML = InputContainer.CheckForXmlConfig(); ImageMetaDataContainer myContainer; if (loadingXML.Equals("yes")) { string xmlFilePath = "XML file\\PhotoConverterCommands.xml"; myContainer = new ImageMetaDataContainer(xmlFilePath); } else { Tuple <string, string, string> getValueOfSourceMethod = InputContainer.SourceMethod(); Console.WriteLine("\nProvide value to height or with to resize image with original aspect ratio for this size. \nOther value write '0' or press enter. \nWrite both values to loose aspect ratio but receive the demanded values"); width = InputContainer.Width(); height = InputContainer.Height(); format = InputContainer.FormatCheck(); Tuple <string, string, string> getValueOfDestinationMethod = InputContainer.Destination(format); myContainer = new ImageMetaDataContainer(getValueOfSourceMethod, width, height, format, getValueOfDestinationMethod); } string joinedformatOfFileInRootFolder = "*.jpg|*.png|*.gif|*.tiff"; string[] files = InputContainer.GetFiles(myContainer.sourceOfFolder, joinedformatOfFileInRootFolder, SearchOption.AllDirectories); PhotoConversionContainer.ImageConversion(myContainer, files); Console.WriteLine("Press any key to continue"); Console.ReadKey(); }
/// <summary> /// Alright, here I reworked this a little bit more. /// /// Here you were a bit unsure it seems on what you actually want to happen and what you actually want to do. On one hand you started making static methods, on the other you made a Constructor. /// /// Constructors are needed for classes where you know that you will need some specific values permanently. Most of the time those are classes, whose objects we instantiate as variables. These /// we intend to keep around longer, with a specific set of values. Sometimes we also want to have several of them, which differ from each other in their values and thus perhaps in other things too. /// /// On the other hand there are static methods, which are used for classes which always do the same types of operations, either with no real parameters needed, or where you will always need to give /// parameters, as otherwise it does not make sense. You would not store for instance the two numbers in a calculator class when you want to add them, right? You do not need the numbers later, you /// will instead demand always new numbers. /// /// Here it is similar, we want to be able to call this method whenever we want with valid parameters and then have pictures converted. We have no use for those images being stored anywhere. /// /// /////////////////////////////////////////////////////////////////////////////////////// /// /// Also, notice how much smaller the call is now with the container, instead of each variable for itself. Why is that good? Because /// a) it is way easier to read, and /// b) if something changes, you have to change it once in the container creation and not everywhere you use this method. /// /// /////////////////////////////////////////////////////////////////////////////////////// /// /// This method will kick off all necessary operations for converting and storing the images. /// </summary> /// <param name="myContainer">The container with most of the information needed to perform this operation.</param> /// <param name="files">An array of files, which need to be converted. Only needed when dealing with multiple files.</param> public static void ImageConversionMethod(ImageMetaDataContainer myContainer, string[] files) //public PhotoConversionContainer(string input, string sourceOfFolderAndImage, int width, int height, string destinationFullPath, string format, string[] files, string destinationFolder, string destinationFile, List<string> destinationFullPathList) { if (myContainer.input.Equals("single")) { var photo = Image.FromFile(myContainer.sourceOfFolderAndImage, true); var convertedPhoto = ResizeImage(photo, myContainer.width, myContainer.height); FormatMethod(convertedPhoto, myContainer.destinationFullPath, myContainer.format); } else { //Since you need it only here anyways, I removed it elsewhere. It is thus valid only within this "else". List <string> destinationFullPathList = new List <string>(); Console.WriteLine("files in the given directory: \n"); foreach (string file in files) { Console.WriteLine(file); } for (int i = 0; i <= files.Count() - 1; i++) { destinationFullPathList.Add(myContainer.destinationFolder + "\\" + myContainer.destinationFile + i + "." + myContainer.format); } Console.WriteLine("below are objects from destinationFullPathList"); foreach (string path in destinationFullPathList) { Console.WriteLine(path); } for (int i = 0; i <= files.Count() - 1; i++) { var newNumber = files.Count(); Console.WriteLine("There are: " + newNumber + " ,in given folders."); { var photo = Image.FromFile(files[i], true); var convertedPhoto = ResizeImage(photo, myContainer.width, myContainer.height); FormatMethod(convertedPhoto, destinationFullPathList[i], myContainer.format); } } } }
/// This method will kick off all necessary operations for converting and storing the images. /// </summary> /// <param name="myContainer">The container with most of the information needed to perform this operation.</param> /// <param name="files">An array of files, which need to be converted. Only needed when dealing with multiple files.</param> //public static void ImageConversion(ImageMetaDataContainer myContainer, string[] files) // Private? public static void ImageConversion(ImageMetaDataContainer myContainer, string[] files) { if (myContainer.input.Equals("single")) { var photo = Image.FromFile(myContainer.sourceOfFolderAndImage, true); var convertedPhoto = ResizeImage(photo, myContainer.width, myContainer.height); Format(convertedPhoto, myContainer.destinationFullPath, myContainer.format); } else { List <string> destinationFullPathList = new List <string>(); Console.WriteLine("images in the given directory: \n"); foreach (string file in files) { Console.WriteLine(file); } for (int i = 0; i <= files.Count() - 1; i++) { destinationFullPathList.Add(myContainer.destinationFolder + "\\" + myContainer.destinationFile + i + "." + myContainer.format); } Console.WriteLine("below are images from destinationFullPathList"); foreach (string path in destinationFullPathList) { Console.WriteLine(path); } for (int i = 0; i <= files.Count() - 1; i++) { var newNumber = files.Count(); Console.WriteLine("There are: " + newNumber + " ,in given folders."); { var photo = Image.FromFile(files[i], true); var convertedPhoto = ResizeImage(photo, myContainer.width, myContainer.height); Format(convertedPhoto, destinationFullPathList[i], myContainer.format); } } } }