private string GetNextAvailableDestination(IReadOnlyDictionary <string, int> potentialPlots) { var plotSize = KSize.Create(plottingManagerOptions.KSize); return(destinationFolders .Select(f => new DirectoryInfo(f)) .Where(i => i.Exists && !string.IsNullOrEmpty(i.Root.Name)) .Select(i => (i, new DriveInfo(i.Root.Name))) .OrderBy(i => i.Item2.AvailableFreeSpace) .FirstOrDefault(i => { string directoryName = i.i.FullName; long potentialPlotsSize = potentialPlots.GetValueOrDefault(directoryName) * plotSize.FinalSizeBytes; return i.Item2.AvailableFreeSpace - potentialPlotsSize > plotSize.FinalSizeBytes; }).i?.FullName); }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (int.TryParse(value?.ToString(), out int KSize)) { if (KSize / GB >= 1) { return((Math.Round(KSize / (float)GB, 2)).ToString() + "GB"); } else if (KSize / MB >= 1) { return((Math.Round(KSize / (float)MB, 2)).ToString() + "MB"); } else if (KSize / KB >= 1) { return((Math.Round(KSize / (float)KB, 2)).ToString() + "KB"); } else { return(KSize.ToString() + "Byte"); } } return(value); }
//pdf /// <summary> /// creates a PDF at path for the given files, for each file callback(success, file, message, Image) will be called /// </summary> public void CreatePdfFromFiles(string path, IEnumerable<string> files, Action<bool, string, string, Image> callback = null) { //if no callback use an empty one callback = callback ?? ((b,s1,s2,i) => { }); //create document and open Document doc = new Document(); doc.SetMargins(0, 0, 0, 0); PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read)); //writer.SetPdfVersion(PdfWriter.VERSION_1_7); writer.SetFullCompression(); doc.Open(); //set some metadata doc.AddTitle(Path.GetFileName(path).RemoveFromEnd(".pdf")); doc.AddCreationDate(); doc.AddCreator("created by " + appname + " @ " + homepage); //calculate page size if desired KSize psize = new KSize(); if (ensmallen || embiggen) { var e = files.Select(file => { var i = Image.GetInstance(new Uri(file)); return new KSize((int) i.Width, (int) i.Height); }); psize = embiggen ? e.MaxBy(s => s.area) : e.MinBy(s => s.area); } //process files foreach (string file in files) { Image i; try { i = Image.GetInstance(new Uri(file)); } catch (Exception ex) { callback(false, file, ex.Message, null); continue; } //set page size and add a new page (technically don't have to do this every time if psize isn't empty) doc.SetPageSize(psize.empty ? new Rectangle(i.Width, i.Height) : new Rectangle(psize.w, psize.h)); doc.NewPage(); //scale and add image if (psize.empty) { i.ScaleToFit(doc.PageSize.Width, doc.PageSize.Height); } else { // see https://stackoverflow.com/questions/6565703/math-algorithm-fit-image-to-screen-retain-aspect-ratio float rp = (float)psize.w / (float)psize.h; float ri = i.Width / i.Height; float iw = rp > ri ? i.Width * psize.h / i.Height : psize.w; float ih = rp > ri ? psize.h : i.Height * psize.w / i.Width; i.ScaleToFit(doc.PageSize.Width, doc.PageSize.Height); i.SetAbsolutePosition((psize.w - iw)/2, (psize.h - ih)/2); } i.Alt = Path.GetFileName(file); doc.Add(i); callback(true, file, null, i); } doc.Close(); }