c# chart.object from excel as .png 低分辨率
c# chart.object from excel as .png low resolution
我创建了一个 excel 文件并想将其内容导出为 png 或 jpeg 文件。
遗憾的是图像质量真的很低。
有解决办法吗?我想要一个非常高分辨率的图片。
谢谢
我当前的代码(来自互联网):
Excel.Range xlRange = xlWorksheet5.get_Range("A1", "K30");
xlRange.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlPicture);
Excel.ChartObject chartObj;
chartObj = xlWorksheet5.ChartObjects().Add(xlRange.Left, xlRange.Top, xlRange.Width, xlRange.Height);
chartObj.Activate();
string path_image = path + "\image.png";
Excel.Chart chart = chartObj.Chart;
chart.Paste();
chart.Export(path_image);
试试这个。您需要将 [STAThread]
属性放在您 运行 所在的任何线程的入口点上。
//This first copy/paste is to convert from chart to image
chartObj.CopyPicture();
xlWorksheet5.Paste();
//This image has decent resolution
xlWorksheet5.Shapes.Item(xlWorksheet5.Shapes.Count).Copy();
//Save the image
System.Windows.Media.Imaging.BitmapEncoder enc = new System.Windows.Media.Imaging.BmpBitmapEncoder();
enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(System.Windows.Clipboard.GetImage()));
using (System.IO.MemoryStream outStream = new System.IO.MemoryStream())
{
enc.Save(outStream);
System.Drawing.Image pic = new System.Drawing.Bitmap(outStream);
pic.Save("image.png");
}
我创建了一个 excel 文件并想将其内容导出为 png 或 jpeg 文件。
遗憾的是图像质量真的很低。
有解决办法吗?我想要一个非常高分辨率的图片。
谢谢
我当前的代码(来自互联网):
Excel.Range xlRange = xlWorksheet5.get_Range("A1", "K30");
xlRange.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlPicture);
Excel.ChartObject chartObj;
chartObj = xlWorksheet5.ChartObjects().Add(xlRange.Left, xlRange.Top, xlRange.Width, xlRange.Height);
chartObj.Activate();
string path_image = path + "\image.png";
Excel.Chart chart = chartObj.Chart;
chart.Paste();
chart.Export(path_image);
试试这个。您需要将 [STAThread]
属性放在您 运行 所在的任何线程的入口点上。
//This first copy/paste is to convert from chart to image
chartObj.CopyPicture();
xlWorksheet5.Paste();
//This image has decent resolution
xlWorksheet5.Shapes.Item(xlWorksheet5.Shapes.Count).Copy();
//Save the image
System.Windows.Media.Imaging.BitmapEncoder enc = new System.Windows.Media.Imaging.BmpBitmapEncoder();
enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(System.Windows.Clipboard.GetImage()));
using (System.IO.MemoryStream outStream = new System.IO.MemoryStream())
{
enc.Save(outStream);
System.Drawing.Image pic = new System.Drawing.Bitmap(outStream);
pic.Save("image.png");
}