【问题标题】:.NET or C# library for CGM (Computer Graphics Metafile) format?用于 CGM(计算机图形元文件)格式的 .NET 或 C# 库?
【发布时间】:2019-09-18 17:28:26
【问题描述】:

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

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

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

【问题讨论】:

    标签: .net


    【解决方案1】:

    我只想与 SO 分享这个临时技巧。如果可能的话,我仍然需要一个合适的库(感谢所有在线提供 .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();
                }
            }
        }
    }
    

    【讨论】:

    • 你有没有找到一个非临时的hacky解决方案?我试着把它放进去,但它似乎没有显示图像,只是说“这个图像目前无法显示。”
    猜你喜欢
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 2017-01-16
    • 2017-07-09
    相关资源
    最近更新 更多