public static string BitmapToPngTempFile(Bitmap image, string title) { string tempPath = IntegrationUtils.GetTempPathAndCreateIfNecessary(); string random = Path.GetRandomFileName().Substring(0, 8); string filePath = FolderUtils.CleanPath(Path.Combine(tempPath, string.IsNullOrWhiteSpace(title) ? $"chordious.{random}.png" : $"chordious.{FolderUtils.CleanTitle(title)}.{random}.png")); using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { BitmapEncoder encoder = new PngBitmapEncoder(); BitmapImage bmpImage = BitmapToBitmapImage(image, ImageFormat.Png); BitmapMetadata frameMetadata = GetExportMetadata(ExportFormat.PNG); BitmapFrame frame = BitmapFrame.Create(bmpImage, null, frameMetadata, null); encoder.Frames.Add(frame); encoder.Save(fs); } return(filePath); }
private string GetFullFilePath(int diagramIndex = 0, bool overwriteFiles = true) { string outputPath = OutputPath; string filenameFormat = SelectedFilenameFormat; string filePath = ""; for (int i = 0; i < SelectedFilenameFormat.Length; i++) { if (SelectedFilenameFormat[i] != '%') { filePath += SelectedFilenameFormat[i]; } else { if (i + 1 < SelectedFilenameFormat.Length) { i++; // step forward to look at the next character char nextChar = SelectedFilenameFormat[i]; switch (nextChar) { case 't': filePath += FolderUtils.CleanTitle(DiagramsToExport[diagramIndex].Title); break; case 'c': filePath += CollectionName; break; case 'h': filePath += DiagramsToExport[diagramIndex].TotalHeight.ToString(); break; case 'w': filePath += DiagramsToExport[diagramIndex].TotalWidth.ToString(); break; case '0': filePath += diagramIndex.ToString(); break; case '1': filePath += (diagramIndex + 1).ToString(); break; case '#': filePath += DiagramsToExport.Count.ToString(); break; case 'x': filePath += ExportFormat.ToString().ToLower(); break; case 'X': filePath += ExportFormat.ToString().ToUpper(); break; case '%': filePath += "%"; break; default: //i--; // back up so we don't miss the next character break; } } } } string rawFileName = FolderUtils.CleanPath(Path.Combine(outputPath, filePath)); string folder = Path.GetDirectoryName(rawFileName); string fileName = Path.GetFileNameWithoutExtension(rawFileName); string extension = Path.GetExtension(rawFileName); string testfileName = Path.Combine(folder, fileName + extension); int attempt = 1; bool foundFilename = false; while (!foundFilename) { bool fileAlreadyExists = File.Exists(testfileName); bool fileCreatedThisExport = _createdFiles.Exists(createdFile => createdFile.Equals(testfileName, StringComparison.CurrentCultureIgnoreCase)); if (!fileAlreadyExists || (fileAlreadyExists && !fileCreatedThisExport && overwriteFiles)) { foundFilename = true; } else { testfileName = Path.Combine(folder, fileName + " (" + attempt + ")" + extension); attempt++; } } return(testfileName); }