CGM(计算机图形图元文件)格式的 .NET 或 C# 库?

标签 .net

有谁知道用于在 WinForms 和 Microsoft Service Reports/CrystalReports 中显示 CGM 文件以便它也可打印的 .NET C# 库?

如果它还能够将文件转换为 web 友好的图形格式,如 jpeg、gif、png 等,那将非常有帮助。

这可能是来自 .NET Library for CGM File Conversion 的重复问题但该 OP 没有标记答案,也没有建议的解决方案与 .NET 兼容。

最佳答案

我只想与 SO 分享这个临时 hack。如果可能的话,我仍然需要一个合适的库(感谢所有在线提供 .NET Excel 教程的人):

using System;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using System.Drawing;

namespace CGM

{
    public class CGMConverter 
    {
        Image _mImage;

        public Image Image { get { return _mImage; } }

        public CGMConverter(string cgm, float width, float height)
        {
            object misValue = System.Reflection.Missing.Value;
            Excel.Application xlsApp = null;
            Excel.Workbook xlsWorkBook = null;
            Excel.Worksheet xlsWorkSheet = null;

            try
            {
                xlsApp = new Excel.ApplicationClass();
                xlsWorkBook = xlsApp.Workbooks.Add(misValue);
                xlsWorkSheet = (Excel.Worksheet)xlsWorkBook.Sheets["sheet1"];
                xlsWorkSheet.Shapes.AddPicture(cgm, MsoTriState.msoFalse, MsoTriState.msoTrue, 50, 50, width, height);
                xlsWorkSheet.Shapes.Item(0).Copy();
                _mImage = System.Windows.Forms.Clipboard.GetImage();
            }
            catch(Exception e)
            {
                throw (e);
            }
            finally
            {
                xlsApp.DisplayAlerts = false;
                xlsApp.Quit();
                releaseObject(xlsWorkSheet);
                releaseObject(xlsWorkBook);
                releaseObject(xlsApp);
            }
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}

关于CGM(计算机图形图元文件)格式的 .NET 或 C# 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4813481/

相关文章:

.Net 4.6 AppContextSwitchOverrides 未设置在配置中声明的开关

c# - 具有多个扩展名的 GetFiles

c# - sql server通知正在运行的程序

.net - 未在使用Microsoft Fakes的C#测试项目中的/reference选项“/引用选项”中指定错误 "The extern alias ' snh'

c# - Windows 7 文件资源管理器树中使用的字体和大小

c# - 如何计算半小时或一小时到 "next"的剩余分钟数?

c# - 调试代码安全 .net 框架以使用 caspol.exe

c# - 无法让 ListBox 和 UpdateTarget 工作

c# - 使用 MEF 加载带有嵌入式库的 DLL

c# - 以不同用户身份从 .NET 服务启动 .NET 应用程序时出现权限问题?