.NET or C# library for CGM (Computer Graphics Metafile) format?

前端 未结 1 2027
深忆病人
深忆病人 2021-01-01 06:04

Does anyone know a .NET C# library for displaying CGM files in WinForms and Microsoft Service Reports/CrystalReports so that it is printable as well?

It would also b

相关标签:
1条回答
  • 2021-01-01 06:54

    I just want to share this temporary hack with SO. I still need a proper library if possible (Credit goes to everyone who makes .NET excel tutorials available online):

    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();
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题