/// <summary> /// 预览和打印(弹出 “打印预览” 框) /// </summary> public void PreviewToPrint() { if (listImagePath == null || listImagePath.Count == 0) { MessageBox.Show(Trans.tr("NoPrintableFile"), Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); return; } printPreviewDialog.Document = printDocument; printPreviewDialog.ShowDialog(); }
/// <summary> /// 将文件或目录下的文件 从格式 format1 转为 format2 /// </summary> static public int ConvertImageByPath(string path, string format1, string format2) { //收集来源 List <string> ImagesFrom = new List <string>(); if (Directory.Exists(path)) { DirectoryInfo root = new DirectoryInfo(path); foreach (FileInfo f in root.GetFiles()) { if (f.Extension.Equals("." + format1)) { ImagesFrom.Add(f.FullName); } } } else if (File.Exists(path)) { FileInfo f = new FileInfo(path); if (f.Extension.Equals("." + format1)) //格式得符合的情况才转换 { ImagesFrom.Add(path); } } try { //逐个转换 foreach (string f in ImagesFrom) { FileInfo info = new FileInfo(f); string file2 = info.Directory + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + "." + format2; int nRet = ConvertImageByPath(f, file2, format1, format2); if (nRet == 0) { File.Delete(f); } else { return(-1); } } } catch (Exception e) { MessageBox.Show(e.Message, Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); return(-1); } return(0); }
/// <summary> ///删除所有临时emf文件 /// </summary> /// ///<note> ///调用 ClearTempEmfFiles 之前,需要保证 ///外部使用完 LoaderImage 返回的 Image 之后,必须明确调用 Image.Dipose() 立刻释放, ///不然这里释放资源会崩溃,需要释放资源才能正常删除 ///</note> static public void ClearTempEmfFiles() { if (listTempEmfFiles == null) { return; } try { foreach (string file in listTempEmfFiles) { File.Delete(file); } } catch (Exception e) { MessageBox.Show(e.Message, Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); } }
/// <summary> /// 将 pathFrom 的图片文件转换为 pathTo 对应格式的图片文件 /// </summary> static public bool ConvertImage(string pathImageFrom, string pathImageTo) { FileInfo f1 = new FileInfo(pathImageFrom); FileInfo f2 = new FileInfo(pathImageTo); if (f1.Extension.Equals(".svg") && f2.Extension.Equals(".emf")) { try { SvgDocument svg = SvgDocument.Open(pathImageFrom); using (Graphics bufferGraphics = Graphics.FromHwndInternal(IntPtr.Zero)) { using (var metafile = new Metafile(pathImageTo, bufferGraphics.GetHdc())) { using (Graphics graphics = Graphics.FromImage(metafile)) { svg.Draw(graphics); } } } } catch (Exception e) { MessageBox.Show(e.Message, Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); return(false); } } else { MessageBox.Show($"unsupported convert [{pathImageFrom}]->[{pathImageTo}]\n\nSupported convert [svg]->[emf]", Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); return(false); } return(true); }
/// <summary> ///加载路径下的一张图片,读取失败或格式不支持返回一张默认图片 /// </summary> /// ///<note> ///外部使用完 Image 之后,必须明确调用 Image.Dipose() 立刻释放,因 ///为 svg 文件会生成 emf 临时文件,需要释放资源才能删除 ///</note> static public Image LoaderImage(string path) { Bitmap defaultBitmap = global::BesPrinter.Properties.Resources.unsupported_image; Image image = null; if (!File.Exists(path)) { image = defaultBitmap; //文件不存在,返回默认图片 } else { FileInfo fileInfo = new FileInfo(path); if (fileInfo.Extension == ".bmp" || fileInfo.Extension == ".png" || fileInfo.Extension == ".jpg" || fileInfo.Extension == ".jpeg") { FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); //从文件流打开,不占用文件 image = Image.FromStream(fileStream); fileStream.Close(); fileStream.Dispose(); } else if (fileInfo.Extension == ".svg") { //临时创建一个 svg 转换而来的 emf 文件 string emfTempPath = Path.GetTempFileName(); try { SvgDocument svg = SvgDocument.Open(path); using (Graphics bufferGraphics = Graphics.FromHwndInternal(IntPtr.Zero)) { using (var metafile = new Metafile(emfTempPath, bufferGraphics.GetHdc())) { using (Graphics graphics = Graphics.FromImage(metafile)) { svg.Draw(graphics); } } image = new Metafile(emfTempPath); //读取 emf 文件 } } catch (Exception e) { MessageBox.Show(e.Message, Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { //现在获得图片不能立刻删除文件,不然得到 image 也无法获得其中的数据 if (listTempEmfFiles == null) { listTempEmfFiles = new List <string>(); } listTempEmfFiles.Add(emfTempPath); } } else if (fileInfo.Extension == ".emf") { FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); //从文件流打开,不占用文件 image = new Metafile(fileStream); fileStream.Close(); fileStream.Dispose(); } else { image = defaultBitmap; //格式不支持不存在,返回默认图片 } } if (image == null) { image = defaultBitmap; //如果存在读取失败的情况,使用默认图片返回 } return(image); }
/// <summary> /// 将文件或文件夹 path1 的图片文件转换为 文件或文件夹 path2 下的文件 /// </summary> static public int ConvertImageByPath(string path1, string path2, string format1, string format2) { if (!(File.Exists(path1) || Directory.Exists(path1) && Directory.Exists(path2))) { MessageBox.Show($"invalid path: from [{path1}] to [{path2}]\n\n when from-path is file, to-path can be directory or file \n\n when from-path is directory, to-path must be directory too", Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); return(-1); } else { List <string> ImagesFrom = new List <string>(); List <string> ImagesTo = new List <string>(); if (Directory.Exists(path2) && path2.LastIndexOf("\\") != path2.Length - 1) { path2 += "\\"; } FileInfo f1 = new FileInfo(path1); FileInfo f2 = new FileInfo(path2); //第一个路径是文件的情况 if (File.Exists(path1) && Directory.Exists(f2.DirectoryName)) { //确认文件2的名称 string fileName2 = System.IO.Path.GetFileNameWithoutExtension(path2); if (f2.Extension == null || f2.Extension.Equals("")) //没有后缀认为路径2为目录 { fileName2 = System.IO.Path.GetFileNameWithoutExtension(path1); } //构建文件2的路径 string file2 = f2.DirectoryName + "\\" + fileName2 + "." + format2; if (f1.Extension.Equals("." + format1)) //格式得符合的情况才转换 { // ImagesFrom 和 ImagesTo 个数一致 ImagesFrom.Add(path1); ImagesTo.Add(file2); } } else if (Directory.Exists(path1) && Directory.Exists(path2)) { //收集符合格式要求的文件,或文件夹下的文件 DirectoryInfo root = new DirectoryInfo(path1); foreach (FileInfo f in root.GetFiles()) { if (f.Extension.Equals("." + format1)) { ImagesFrom.Add(f.FullName); } } string directory2 = f2.Directory.FullName; foreach (string f in ImagesFrom) { FileInfo info1 = new FileInfo(f); string file1Name = System.IO.Path.GetFileNameWithoutExtension(f); string file2 = directory2 + "\\" + file1Name + "." + format2; ImagesTo.Add(file2); } // ImagesFrom 和 ImagesTo 个数一致 } //将 ImagesFrom 和 ImagesTo 的文件对应进行装换 for (int i = 0; i < ImagesFrom.Count; i++) { if (!ConvertImage(ImagesFrom[i], ImagesTo[i])) //失败任意一个则退出 { return(-1); } } return(0); } }
//分析得到运行模式相关数据 static public ExeModeManager AnaliseModeFromArgs(string[] args) { ExeModeManager exeMode = new ExeModeManager(); if (args.Length > 0) { //读取模式 EXE_MODE mode = EXE_MODE.MODE_NORMAL; try { mode = (EXE_MODE)int.Parse(args[0]); } catch (Exception) { mode = EXE_MODE.MODE_NORMAL; } finally { if (mode < EXE_MODE.MODE_NORMAL || mode >= EXE_MODE.MODE_END) { mode = EXE_MODE.MODE_NORMAL; } } //根据模式,进一步读取相关参数 switch ((EXE_MODE)int.Parse(args[0])) { case EXE_MODE.MODE_NORMAL: break; case EXE_MODE.MODE_INIT_PATH: { if (args.Length == 2) { exeMode.initPath = args[1]; } else { mode = EXE_MODE.MODE_NORMAL; } } break; case EXE_MODE.MODE_SINGLE_IMAGE: { if (args.Length == 2) { exeMode.singleImagePath = args[1]; if (!File.Exists(exeMode.singleImagePath)) { MessageBox.Show($"Invalid image path: {exeMode.singleImagePath}", Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); mode = EXE_MODE.MODE_NORMAL; } } else { mode = EXE_MODE.MODE_NORMAL; } } break; case EXE_MODE.MODE_FORMAT_CONVERT: { if (args.Length == 4 || args.Length == 5) { exeMode.formatFrom = args[1]; exeMode.formatTo = args[2]; exeMode.pathFrom = args[3]; if (args.Length == 5) { exeMode.pathTo = args[4]; } else { exeMode.pathTo = null; } if (exeMode.formatFrom == "svg" && exeMode.formatTo == "emf") { if (!(File.Exists(exeMode.pathFrom) || (Directory.Exists(exeMode.pathFrom) && (exeMode.pathTo == null || Directory.Exists(exeMode.pathTo))) ) ) { MessageBox.Show($"invalid path:\n\n path1 [{exeMode.pathFrom}]\n\n path2 [{exeMode.pathTo}]\n\n when from-path is file, to-path can be directory or file \n\n when from-path is directory, to-path must be directory too", Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); mode = EXE_MODE.MODE_END; //格式转换时发生错误,不能继续执行,置为 MODE_END 表示错误 } } else { MessageBox.Show($"unsupported convert [{exeMode.formatFrom}]->[{exeMode.formatTo}]\n\nSupported convert [svg]->[emf]", Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); mode = EXE_MODE.MODE_END; //格式转换时发生错误,不能继续执行,置为 MODE_END 表示错误 } } else { mode = EXE_MODE.MODE_END; //格式转换时发生错误,不能继续执行,置为 MODE_END 表示错误 } } break; default: mode = EXE_MODE.MODE_NORMAL; break; } //执行模式 exeMode.mode = mode; } return(exeMode); }
/// <summary> /// 打印过程处理 /// </summary> private void PrintPage(object sender, PrintPageEventArgs args) { //没有数据,直接返回 if (listImagePath == null || listImagePath.Count == 0) { args.HasMorePages = false; return; } //处理当前页面数据 Image pageImage = null; try { //获得需要打印的数据 string fileName = listImagePath[indexPrinting]; pageImage = ImageHelper.LoaderImage(fileName); //获得打印位置信息 GraphicsUnit unit = GraphicsUnit.Millimeter; RectangleF rectImage = pageImage.GetBounds(ref unit); RectangleF rectContainer = args.PageBounds; RectangleF rectDraw = rectContainer; //初始值 //根据是否缩放设置,获得对应的绘制区域 if (saver.KeepRatio) { rectDraw = ImageHelper.GetKeepRadioRect(rectImage, rectContainer, saver.MarginsCustom.Left, saver.MarginsCustom.Right, saver.MarginsCustom.Top, saver.MarginsCustom.Bottom); } else { rectDraw = ImageHelper.GetContainerMarginRect(rectContainer, saver.MarginsCustom.Left, saver.MarginsCustom.Right, saver.MarginsCustom.Top, saver.MarginsCustom.Bottom); } //绘制 args.Graphics.DrawImage(pageImage, rectDraw); } catch (Exception exception) { MessageBox.Show(exception.Message, Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { //调用 ImageHelper.LoaderImage(fileName) 后要求使用完立刻释放图片资源 if (null != pageImage) { pageImage.Dispose(); } } //分析是否有下一页 ++indexPrinting; if (indexPrinting == listImagePath.Count) { args.HasMorePages = false; indexPrinting = 0; } else { args.HasMorePages = true; } }