This project has moved. For the latest updates, please go here.

Adding scalable picture

Sep 19, 2011 at 3:26 PM

I need to add graphs to my worksheet that are beyond what the EPPlus API provides.  So I'm using other tools (specifically, MS Chart Controls for .Net) to render the graphs, then transferring them to the worksheet.

I would like to do this in a manner that provides scalable graphics, so that text and lines are rendered correctly.  I've done this by drawing to a metafile, which I can put in a Stream or a file.  Unfortunately, it seems that if when add the metafile to the worksheet (via a Stream or file and worksheet.Drawings.AddPicture) it gets converted to a JPEG image, which isn't really suitable because of the artifacts and because of the fixed pixel resolution.  Relevant here is the comment against the ExcelPicture.ImageFormat property, which says "If the picture is created from an Image this type is always Jpeg".

Is there a way around this?  For example, if I save my graph as a metafile (.wmf or .emf), then use Insert...Picture in Excel or OOo Spreadsheet, the graphics are drawn as I require.  But I can't find a means to accomplish the same using EPPlus.

Thanks, Phil

Coordinator
Sep 20, 2011 at 7:16 PM

I think the only workaround is to save the file in the format you want and then use the AddPicture method with a FileInfo parameter. Then it is saved in the package depending on the extension of the file.

Jan

Feb 19, 2012 at 1:40 AM
Edited Feb 19, 2012 at 5:00 AM

[UPDATE] The problem was with the chart control, not EPPlus.  I didn't have EnableViewState set on the Chart control, so on the postback the chart image was blank.

 

Phil:

I am doing exactly the same thing.  However, when I add my images via AddPicture, the images do not display in the worksheet.  Can you share the bit of your code wherein you get the images from the chart and save them too the worksheet?  Here is what I did:

                string filename = Server.MapPath("~") + "/" + uniquefilename;

                if (chart1.Visible)

                {

                    chart1.SaveImage(filename, System.Web.UI.DataVisualization.Charting.ChartImageFormat.Bmp);

                    System.Drawing.Image imgChart1 = new Bitmap(filename);

                    if (imgChart1 != null)

                    {

                        //set picture column width to accommodate the picture

                        ws.Column(pictureCol).Width = ExcelHelper.Pixel2ColumnWidth(ws, imgChart1.Width + 1);

 

                        //set row height to accommodate the picture

                        ws.Row(iCurrentRow).Height = ExcelHelper.Pixel2RowHeight(imgChart1.Height + 1);

 

                        //add picture to cell

                        ExcelPicture pic = ws.Drawings.AddPicture("pic" + iCurrentRow.ToString() + pictureCol.ToString(), imgChart1);

                        //position picture on desired column

                        pic.From.Column = pictureCol - 1;

                        pic.From.Row = iCurrentRow - 1;

                        pic.From.ColumnOff = ExcelHelper.Pixel2MTU(1);

                        pic.From.RowOff = ExcelHelper.Pixel2MTU(1);

                        //set picture size to fit inside the cell

                        pic.SetSize(imgChart1.Width, imgChart1.Height);

                    } 

                }

Feb 19, 2012 at 12:37 PM

Do I understand from your update that you’ve solved this yourself?

Phil

Description: Logo smallPixel Analytics is a limited company registered in England

Company number: 7747526; Registered office: 93A New Road, Haslingfield, Cambridge CB23 1LP

From: SolarFlareSW [email removed]
Sent: 19 February 2012 02:41
To: phil.atkin@pixelanalytics.co.uk
Subject: Re: Adding scalable picture [EPPlus:273082]

From: SolarFlareSW

Phil:

I am doing exactly the same thing. However, when I add my images via AddPicture, the images do not display in the worksheet. Can you share the bit of your code wherein you get the images from the chart and save them too the worksheet? Here is what I did:

string filename = Server.MapPath("~") + "/" + uniquefilename;

if (chart1.Visible)

{

chart1.SaveImage(filename, System.Web.UI.DataVisualization.Charting.ChartImageFormat.Bmp);

System.Drawing.Image imgChart1 = new Bitmap(filename);

if (imgChart1 != null)

{

//set picture column width to accommodate the picture

ws.Column(pictureCol).Width = ExcelHelper.Pixel2ColumnWidth(ws, imgChart1.Width + 1);

//set row height to accommodate the picture

ws.Row(iCurrentRow).Height = ExcelHelper.Pixel2RowHeight(imgChart1.Height + 1);

//add picture to cell

ExcelPicture pic = ws.Drawings.AddPicture("pic" + iCurrentRow.ToString() + pictureCol.ToString(), imgChart1);

//position picture on desired column

pic.From.Column = pictureCol - 1;

pic.From.Row = iCurrentRow - 1;

pic.From.ColumnOff = ExcelHelper.Pixel2MTU(1);

pic.From.RowOff = ExcelHelper.Pixel2MTU(1);

//set picture size to fit inside the cell

pic.SetSize(imgChart1.Width, imgChart1.Height);

}

}

Jun 3, 2014 at 2:47 AM
I think i also got the same problem you faced right now . So is it resolved now?
Jun 3, 2014 at 9:38 AM
I'm sorry but I'm no longer using this code, and I can't recall what happened. Phil On 03/06/2014 03:47, gumuruh wrote: > > From: gumuruh > > I think i also got the same problem you faced right now . So is it > resolved now? > > Read the full discussion online > . > > To add a post to this discussion, reply to this email > ([email removed] > ) > > To start a new discussion for this project, email > [email removed] > > You are receiving this email because you subscribed to this discussion > on CodePlex. You can unsubscribe > on > CodePlex.com. > > Please note: Images and attachments will be removed from emails. Any > posts to this discussion will also be available online at CodePlex.com > -- *Pixel Analytics* is a limited company registered in England. Company number: 7747526; Registered office: 93A New Road, Haslingfield, Cambridge CB23 1LP I'm sorry but I'm no longer using this code, and I can't recall what happened.
Phil
On 03/06/2014 03:47, gumuruh wrote:

From: gumuruh

I think i also got the same problem you faced right now . So is it resolved now?

--
Pixel Analytics is a limited company registered in England. Company number: 7747526; Registered office: 93A New Road, Haslingfield, Cambridge CB23 1LP