有谁知道用于在 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/