Image height/width too large

I am trying to include an image from a bitMap object in memory.
I have found the example in this forum (‘Set Image From Memory’) which works for me after a fashion, but my problem is this:

The image I’m trying to incorperate is a full scanned page and I want it to take up as much of the PDF document pasge as possible, but if I set ImageHeight and ImageWidth parameters for this image that make it much more than about 1/3 of the total page size then I get an error ‘Image Height() too large’ when I try and make my pdfDoc.Save() call.

My code is almost identical to the code in the above forum posting.

Hope that someone can help me cause if I can get this working…

Thanks in anticipation,

ChrisM.

Dear ChrisM,

Thanks for your consideration.

When using memory image, you can’t control the image’s width and height. The value of the ImageWidth and ImageHeight properties must be equal to the actual image width and height. If you get the “Image height/width too large” error, please enlarge your page size or decrease the page margin to fit the image size.

Thanks for your rapid reply.
What you are saying makes sense.
However, I am still having problems. Do you think you could post a little code fragment showing how to create a new document and add an image (width-612, height-842) as even by playing with page sizes and margins, I am still unable to do this.

Thanks,

Chris.

Dear Chris,

Thanks for your consideration.

Here is an example:

[C#]
private void Page_Load(object sender, System.EventArgs e)
{
Bitmap bitmap = new Bitmap(612,842);
Graphics g = Graphics.FromImage(bitmap);

g.DrawEllipse(Pens.Red, new RectangleF(0, 0, bitmap.Width, bitmap.Height));

MemoryStream mstream = new MemoryStream();

ImageCodecInfo myImageCodecInfo = GetEncoderInfo(“image/jpeg”);
EncoderParameter myEncoderParameter0 = new EncoderParameter( Encoder.Quality, (long) 100 );
EncoderParameters myEncoderParameters = new EncoderParameters(1);
myEncoderParameters.Param[0] = myEncoderParameter0;

bitmap.Save(mstream,myImageCodecInfo,myEncoderParameters);

g.Dispose();

Pdf pdf = new Pdf();

Section section = new Section(pdf);
section.PageInfo.PageWidth = 680;
section.PageInfo.Margin.Left = 30;
section.PageInfo.Margin.Right = 30;
section.PageInfo.PageHeight = 910;
section.PageInfo.Margin.Top = 30;
section.PageInfo.Margin.Bottom = 30;
pdf.Sections.Add(section);

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(section);
section.Paragraphs.Add(image1);

image1.ImageInfo.ImageFileType = ImageFileType.Jpeg;
System.IO.BinaryReader reader = new System.IO.BinaryReader(mstream);
image1.ImageWidth = 612;
image1.ImageHeight = 842;
image1.ImageInfo.ComponentNumber = 3;
image1.ImageInfo.BitsPerComponent = 8;
image1.ImageInfo.OpenType = ImageOpenType.Memory;
mstream.Position = 0;
image1.ImageInfo.MemoryData = reader.ReadBytes((int)mstream.Length);

pdf.Save(Response);
Response.End();
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}



[VisualBasic]
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bitmap As Bitmap = New Bitmap(612, 842)
Dim g As Graphics = Graphics.FromImage(bitmap)

g.DrawEllipse(Pens.Red, New RectangleF(0, 0, bitmap.Width, bitmap.Height))

Dim mstream As MemoryStream = New MemoryStream()

Dim myImageCodecInfo As ImageCodecInfo = GetEncoderInfo(“image/jpeg”)
Dim myEncoderParameter0 As EncoderParameter = New EncoderParameter(Encoder.Quality, 100)
Dim myEncoderParameters As EncoderParameters = New EncoderParameters(1)
myEncoderParameters.Param(0) = myEncoderParameter0

bitmap.Save(mstream, myImageCodecInfo, myEncoderParameters)

g.Dispose()

Dim pdf As Pdf = New Pdf()

Dim section As Section = New Section(pdf)
section.PageInfo.PageWidth = 680
section.PageInfo.Margin.Left = 30
section.PageInfo.Margin.Right = 30
section.PageInfo.PageHeight = 910
section.PageInfo.Margin.Top = 30
section.PageInfo.Margin.Bottom = 30
pdf.Sections.Add(section)

Dim image1 As Aspose.Pdf.Image = New Aspose.Pdf.Image(section)
section.Paragraphs.Add(image1)

image1.ImageInfo.ImageFileType = ImageFileType.Jpeg
Dim reader As System.IO.BinaryReader = New System.IO.BinaryReader(mstream)
image1.ImageWidth = 612
image1.ImageHeight = 842
image1.ImageInfo.ComponentNumber = 3
image1.ImageInfo.BitsPerComponent = 8
image1.ImageInfo.OpenType = ImageOpenType.Memory
mstream.Position = 0
image1.ImageInfo.MemoryData = reader.ReadBytes(CType(mstream.Length, Integer))

pdf.Save(Response)
Response.End()
End Sub
Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
Dim j As Integer
Dim encoders() As ImageCodecInfo
encoders = ImageCodecInfo.GetImageEncoders()
For j = 0 To encoders.Length - 1 Step +1
If encoders(j).MimeType = mimeType Then
Return encoders(j)
End If
Next
Return Nothing
End Function


Thanks for you answer, it does help.
However, (and I didn’t mention this in my previous post) The page size of the document has to be A4. WHich is actually slightly smaller than the image size I have. I have solved (got round?) the problem by using the GetThumbnailImage method on my original bitmap to make one that is the correct size:

'Make sure the image is no bigger than 595*842 (The required page size)
Dim sizedImg as BitMap
If img.Width > 595 Or img.Height > 842 Then
sizedImg = img.pBitmapImage.GetThumbnailImage(595, 842, Nothing, New IntPtr())
Else
sizedImg = img
End If

Thanks for your assistance on this. One more problem to solve and we may well soon be placing an order for your product…