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 be extremely helpful if i开发者_Go百科t is also able to convert the file to a web-friendly graphics format like jpeg, gif, png etc.
This is likely a repeated question from .NET Library for CGM File Conversion but that OP did not mark an answer nor were the suggested solutions .NET compatible.
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();
}
}
}
}
精彩评论