Image width is too large 800

From Bahman:

This is great , couple issues with conversion of graph to pdf. 1. I want my pie chart to be height=800 width=1000, when converting to pdf get error here: Image width is too large 800
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ApplicationException: Image width is too large 800

Source Error:
Line 147: myimage.ImageInfo.MemoryData =
myreader.ReadBytes(mstream.Length)
Line 148: bmap.Dispose()
Line 149: mypdf.Save(“c:\Chart.pdf”)

Let me know!
thanks

It might be that Aspose.Pdf treats image size as in points (1/72 inch) therefore 800 points is 11.11 inches and it does not fit onto A4 or Letter page. As I understand you need your image to fit onto the page, otherwise an exception is thrown.

Aspose.Pdf team will probably have a more detailed answer and solution.

Dear Bahman,

Thank you for considering Aspose.

By default, Aspose.Pdf use the real size of the image. If it can’t fit the page size, you will get an “image too large” exception. You can set the [Image.ImageScale] to control the image size to disply in PDF. You can also use the [ImageInfo.FixWidth] to scale the image to fixed width.

Tommy I have tried and tried and am having no luck , can you please explain with the following data points, and needing the image to be 800600 (or else you cant see all these datapoints) how you can make it so you can save this image to a pdf, I have messed with the fixwidth roperty and imagescale property but it just does not work no matter what.

please help

Dim chart As chart = New Aspose.Chart.Chart
chart.Height = ImgHolder.Height.Value (image is set too 800
600 in aspx designer)
chart.Width = ImgHolder.Width.Value

'Add a title
Dim title As Title = New Title
title.Text = “TEST”
title.Font = New System.Drawing.Font(“Arial”, 14)
chart.Titles.Add(title)
'Set axis label formatting.
chart.ChartArea.AxisY.DefaultLabel.Format = “P2”
chart.ChartArea.AxisX.DefaultLabel.Format = “dd-MMM-yy”
'Add data
Dim series1 As ASeries = New ASeries
series1.ChartType = ChartType.Bar
series1.DataPoints.Add(New DateTime(2003, 1, 31), -0.012)
series1.DataPoints.Add(New DateTime(2003, 2, 28), 0.0075)
series1.DataPoints.Add(New DateTime(2003, 3, 1), 0.01)
series1.DataPoints.Add(New DateTime(2003, 4, 30), 0.012)
series1.DataPoints.Add(New DateTime(2003, 5, 31), -0.045)
series1.DataPoints.Add(New DateTime(2003, 6, 30), -0.045)
series1.DataPoints.Add(New DateTime(2003, 7, 31), -0.012)
series1.DataPoints.Add(New DateTime(2003, 8, 31), 0.0075)
series1.DataPoints.Add(New DateTime(2003, 9, 30), 0.01)
series1.DataPoints.Add(New DateTime(2003, 10, 31), 0.012)
series1.DataPoints.Add(New DateTime(2003, 11, 30), -0.045)
series1.DataPoints.Add(New DateTime(2003, 12, 31), -0.045)

chart.NSeries.Add(series1)
'Just to make it visible more like the original.
chart.ChartArea.AxisX.IsMajorGridVisible = False
chart.ChartArea.AxisX.IsMajorTickMarkVisible = False
chart.ChartArea.LegendBox.IsVisible = False
'This seems like it has to be after NSeries.Add, otherwise does not work.
chart.ChartArea.IsXAxisCrossingZero = True

Dim tempPath As String = MapPath(“Temp”)
Dim imageFile As String = chart.GetImageFile(tempPath, System.Drawing.Imaging.ImageFormat.Png)
chart.Save(imageFile, True)


’ Gets the image filename.
Dim pos As Integer = imageFile.LastIndexOf("")
Dim imageFilename As String = imageFile.Substring(pos)

’ Displays the chart in the myimage control.
ImgHolder.ImageUrl = “Temp/” + imageFilename


'PDF convert. no matter it will not save b/c of size
Dim bmap As Bitmap = chart.Save()




’ Gets the image filename.
’ Displays the chart in the myimage control.

Dim mstream As New System.IO.MemoryStream
'Begin Pdf Conversion

bmap.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp)
Dim myreader As New System.IO.BinaryReader(mstream)
Dim mypdf As New Pdf
Dim sec1 As Section = mypdf.Sections.Add()
Dim myimage As New Aspose.Pdf.Image(sec1)
myimage.ImageInfo.ImageFileType = ImageFileType.MemoryBmp
myimage.ImageInfo.OpenType = ImageOpenType.Memory
myimage.ImageScale = 0.8F
myimage.ImageInfo.FixWidth = True
mstream.Position = 0

myimage.ImageInfo.MemoryData = myreader.ReadBytes(mstream.Length)
sec1.Paragraphs.Add(myimage)
bmap.Dispose()

mypdf.Save(“c:\Chart.pdf”)

Dear Bahman,

Thank you for considering Aspose.

If you want to display the image as it’s real size (800*600), you should set the page size to a larger number. For example:

sec1.PageInfo.PageWidth = 1000
sec1.PageInfo.PageHeight = 800

If you want the page size to be the default A4 size, you can use the ImageScale or ImageInfo.FixWidth (you need not use both of them). In your code, if you set the ImageScale to 0.5F or set the ImageInfo.FixWidth to 400, it will work.

Tommy;
I have tried and tried again but this a consistent issue.
even by having the sec1 pagewidth and height match my chart width and image width and hight (all set to the same thing) consistently with different graphs it says the width is too large depending on the graph, when I reduce the image using imagescale it works but it is WAY too small to see that image on a pdf, you can tell by looking at the PDF there is alot more room you can use…

any suggestions???

hi bsarram,

Maybe you can send some of your images that do not fit to pdf@aspose.com along with your code. It might help Tommy to explain exactly what you must to so they always fit properly.

thanks Roman. Code is on its way, if you look at the code you will see the image that is displayed on the web page, and how it has to be shrunk down for pdf…

Dear Bahman,

Thank you for considering Aspose.

Adding the following lines of code will make your pdf the same size as the chart:

sec1.PageInfo.PageWidth = chart.Width
sec1.PageInfo.PageHeight = chart.Height
sec1.PageInfo.Margin.Top = 0
sec1.PageInfo.Margin.Bottom = 0
sec1.PageInfo.Margin.Left = 0
sec1.PageInfo.Margin.Right = 0

i got it to work but its not ideal, setting the margins does help, however you still have to scale the image down to 0.8F… the chart needs to be 1000 wide for the data points to fit, but for the Pdf the largest it looks like it accepts is 0.8F

FYI this was really frustrating to find out by trial and error… I have been changing image scale and chart widths for one week…
we are still going to purchase the product, but figured to let you know the difficulty…

Dear Bahman,

Thank you for considering Aspose.

The code you sent to me has database connection and I can’t test it directly. I have tested the code you have posted on the forum. Here is the code:

Dim chart As chart = New Aspose.Chart.Chart()
chart.Height = 600 '(image is set too 800*600 in aspx designer)
chart.Width = 800

'Add a title
Dim title As Title = New Title()
title.Text = “TEST”
title.Font = New System.Drawing.Font(“Arial”, 14)
chart.Titles.Add(title)
'Set axis label formatting.
chart.ChartArea.AxisY.DefaultLabel.Format = “P2”
chart.ChartArea.AxisX.DefaultLabel.Format = “dd-MMM-yy”
'Add data
Dim series1 As ASeries = New ASeries()
series1.ChartType = ChartType.Bar
series1.DataPoints.Add(New DateTime(2003, 1, 31), -0.012)
series1.DataPoints.Add(New DateTime(2003, 2, 28), 0.0075)
series1.DataPoints.Add(New DateTime(2003, 3, 1), 0.01)
series1.DataPoints.Add(New DateTime(2003, 4, 30), 0.012)
series1.DataPoints.Add(New DateTime(2003, 5, 31), -0.045)
series1.DataPoints.Add(New DateTime(2003, 6, 30), -0.045)
series1.DataPoints.Add(New DateTime(2003, 7, 31), -0.012)
series1.DataPoints.Add(New DateTime(2003, 8, 31), 0.0075)
series1.DataPoints.Add(New DateTime(2003, 9, 30), 0.01)
series1.DataPoints.Add(New DateTime(2003, 10, 31), 0.012)
series1.DataPoints.Add(New DateTime(2003, 11, 30), -0.045)
series1.DataPoints.Add(New DateTime(2003, 12, 31), -0.045)

chart.NSeries.Add(series1)
'Just to make it visible more like the original.
chart.ChartArea.AxisX.IsMajorGridVisible = False
chart.ChartArea.AxisX.IsMajorTickMarkVisible = False
chart.ChartArea.LegendBox.IsVisible = False
'This seems like it has to be after NSeries.Add, otherwise does not work.
chart.ChartArea.IsXAxisCrossingZero = True

Dim tempPath As String = “E:/Temp”
Dim imageFile As String = chart.GetImageFile(tempPath, System.Drawing.Imaging.ImageFormat.Png)
chart.Save(imageFile, True)


’ Gets the image filename.
Dim pos As Integer = imageFile.LastIndexOf("")
Dim imageFilename As String = imageFile.Substring(pos)

’ Displays the chart in the myimage control.
'ImgHolder.ImageUrl = “Temp/” + imageFilename


'PDF convert. no matter it will not save b/c of size
Dim bmap As Bitmap = chart.Save()




’ Gets the image filename.
’ Displays the chart in the myimage control.

Dim mstream As New System.IO.MemoryStream()
'Begin Pdf Conversion

bmap.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp)
Dim myreader As New System.IO.BinaryReader(mstream)
Dim mypdf As New Pdf()
Dim sec1 As Section = mypdf.Sections.Add()

sec1.PageInfo.PageWidth = chart.Width
sec1.PageInfo.PageHeight = chart.Height
sec1.PageInfo.Margin.Top = 0
sec1.PageInfo.Margin.Bottom = 0
sec1.PageInfo.Margin.Left = 0
sec1.PageInfo.Margin.Right = 0

Dim myimage As New Aspose.Pdf.Image(sec1)
myimage.ImageInfo.ImageFileType = ImageFileType.MemoryBmp
myimage.ImageInfo.OpenType = ImageOpenType.Memory
'myimage.ImageScale = 0.5F
'myimage.ImageInfo.FixWidth = 400
mstream.Position = 0

myimage.ImageInfo.MemoryData = myreader.ReadBytes(mstream.Length)
sec1.Paragraphs.Add(myimage)
bmap.Dispose()

mypdf.Save(“c:\Chart.pdf”)

I have sent the generated pdf file via email. Please contact me using one of the IM in our live support if you still can’t get it work.