private static BitmapSource LoadImage(string fileName) { string pathName = RuntimeDataAccess.GetPathName(TIPS_DIR); pathName = Path.Combine(pathName, fileName); try { BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(pathName); bitmap.CacheOption = BitmapCacheOption.OnLoad; // don't hold file open bitmap.EndInit(); return(bitmap); } catch (Exception ex) { Debug.WriteLine("Unable to load bitmap '" + fileName + "': " + ex); return(null); } }
private static BitmapSource LoadImage1(string fileName) { string pathName = RuntimeDataAccess.GetPathName(TIPS_DIR); pathName = Path.Combine(pathName, fileName); try { // From "How to: Encode and Decode a PNG Image". // Note: this holds the file open (try deleting the file). BitmapSource bitmapSource; Stream imageStreamSource = new FileStream(pathName, FileMode.Open, FileAccess.Read, FileShare.Read); PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); bitmapSource = decoder.Frames[0]; //imageStreamSource.Dispose(); // image becomes blank return(bitmapSource); } catch (IOException ex) { Debug.WriteLine("Unable to load image '" + fileName + "': " + ex); return(null); } }
private bool DoLoadTips() { string pathName = RuntimeDataAccess.GetPathName(TIPS_DIR); pathName = Path.Combine(pathName, TIPS_FILE); string cereal; try { cereal = File.ReadAllText(pathName); } catch (IOException ex) { Debug.WriteLine("Failed reading tip file '" + pathName + "': " + ex.Message); return(false); } JavaScriptSerializer ser = new JavaScriptSerializer(); SerTipFile tipFile; try { tipFile = ser.Deserialize <SerTipFile>(cereal); } catch (Exception ex) { Debug.WriteLine("Failed deserializing tip JSON: " + ex.Message); return(false); } if (tipFile == null) { Debug.WriteLine("Failed to find tip list"); return(false); } foreach (SerTip serTip in tipFile.Tips) { mTips.Add(new Tip(serTip.Text, serTip.Image)); } return(true); }
/// <summary> /// Generates HTML output to the specified path. /// </summary> /// <param name="pathName">Full pathname of output file (including ".html"). This /// defines the root directory if there are additional files.</param> /// <param name="overwriteCss">If set, existing CSS file will be replaced.</param> public void OutputToHtml(string pathName, bool overwriteCss) { string exportTemplate = RuntimeDataAccess.GetPathName(HTML_EXPORT_TEMPLATE); string tmplStr; try { // exportTemplate will be null if Runtime access failed tmplStr = File.ReadAllText(exportTemplate); } catch (Exception ex) { string msg = string.Format(Res.Strings.ERR_FILE_READ_FAILED_FMT, pathName, ex.Message); MessageBox.Show(msg, Res.Strings.ERR_FILE_GENERIC_CAPTION, MessageBoxButton.OK, MessageBoxImage.Error); return; } // We should only need the _IMG directory if there are visualizations. if (GenerateImageFiles && mProject.VisualizationSets.Count != 0) { string imageDirName = Path.GetFileNameWithoutExtension(pathName) + "_IMG"; string imageDirPath = Path.Combine(Path.GetDirectoryName(pathName), imageDirName); bool exists = false; try { FileAttributes attr = File.GetAttributes(imageDirPath); if ((attr & FileAttributes.Directory) != FileAttributes.Directory) { string msg = string.Format(Res.Strings.ERR_FILE_EXISTS_NOT_DIR_FMT, imageDirPath); MessageBox.Show(msg, Res.Strings.ERR_FILE_GENERIC_CAPTION, MessageBoxButton.OK, MessageBoxImage.Error); return; } exists = true; } catch (FileNotFoundException) { } catch (DirectoryNotFoundException) { } if (!exists) { try { Directory.CreateDirectory(imageDirPath); } catch (Exception ex) { string msg = string.Format(Res.Strings.ERR_DIR_CREATE_FAILED_FMT, imageDirPath, ex.Message); MessageBox.Show(msg, Res.Strings.ERR_FILE_GENERIC_CAPTION, MessageBoxButton.OK, MessageBoxImage.Error); return; } } // All good. mImageDirPath = imageDirPath; } // Perform some quick substitutions. This could be done more efficiently, // but we're only doing this on the template file, which should be small. tmplStr = tmplStr.Replace("$ProjectName$", mProject.DataFileName); tmplStr = tmplStr.Replace("$AppVersion$", App.ProgramVersion.ToString()); string expModeStr = ((Formatter.FormatConfig.ExpressionMode) AppSettings.Global.GetEnum(AppSettings.FMT_EXPRESSION_MODE, typeof(Formatter.FormatConfig.ExpressionMode), (int)Formatter.FormatConfig.ExpressionMode.Unknown)).ToString(); tmplStr = tmplStr.Replace("$ExpressionStyle$", expModeStr); string dateStr = DateTime.Now.ToString("yyyy/MM/dd"); string timeStr = DateTime.Now.ToString("HH:mm:ss zzz"); tmplStr = tmplStr.Replace("$CurrentDate$", dateStr); tmplStr = tmplStr.Replace("$CurrentTime$", timeStr); tmplStr = tmplStr.Replace("$GenParameters$", GenerateParameterString()); // Generate and substitute the symbol table. This should be small enough that // we won't break the world by doing it with string.Replace(). string symTabStr = GenerateHtmlSymbolTable(); tmplStr = tmplStr.Replace("$SymbolTable$", symTabStr); // For the main event we split the template in half, and generate the code lines // directly into the stream writer. const string CodeLinesStr = "$CodeLines$"; int splitPoint = tmplStr.IndexOf(CodeLinesStr); if (splitPoint < 0) { Debug.WriteLine("No place to put code"); return; } string template1 = tmplStr.Substring(0, splitPoint); string template2 = tmplStr.Substring(splitPoint + CodeLinesStr.Length); // Generate UTF-8 text, without a byte-order mark. using (StreamWriter sw = new StreamWriter(pathName, false, new UTF8Encoding(false))) { sw.Write(template1); //sw.Write("<code style=\"white-space: pre;\">"); sw.Write("<pre>"); StringBuilder sb = new StringBuilder(128); for (int lineIndex = 0; lineIndex < mCodeLineList.Count; lineIndex++) { if (Selection != null && !Selection[lineIndex]) { continue; } GenerateHtmlLine(lineIndex, sw, sb); } sw.WriteLine("</pre>\r\n"); sw.Write(template2); } string cssFile = RuntimeDataAccess.GetPathName(HTML_EXPORT_CSS_FILE); string outputDir = Path.GetDirectoryName(pathName); string outputPath = Path.Combine(outputDir, HTML_EXPORT_CSS_FILE); if (File.Exists(cssFile) && (overwriteCss || !File.Exists(outputPath))) { Debug.WriteLine("Copying '" + cssFile + "' -> '" + outputPath + "'"); try { File.Copy(cssFile, outputPath, true); } catch (Exception ex) { string msg = string.Format(Res.Strings.ERR_FILE_COPY_FAILED_FMT, cssFile, outputPath, ex.Message); MessageBox.Show(msg, Res.Strings.ERR_FILE_GENERIC_CAPTION, MessageBoxButton.OK, MessageBoxImage.Error); return; } } }