Excel中的C#chart.object为.png低分辨率 [英] c# chart.object from excel as .png low resolution

查看:302
本文介绍了Excel中的C#chart.object为.png低分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个excel文件,并希望将其内容导出为png或jpeg文件.

I have created an excel file and want to export its contents as a png or jpeg file.

不幸的是,图像质量真的很差.

Unfortunately the quality of the image is really low.

有解决方案吗?我希望有一张高分辨率的图片.

Is there a solution to this? I wish a really high resolution picture.

谢谢

我当前的代码(来自互联网):

My current code (from internet):

     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]属性.

Try this. You'll need to put a [STAThread] attribute over the entry point of whatever thread you run this on.

    //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中的C#chart.object为.png低分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆