We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Image size is too large while extracting from PDF

Can anyone help me to extract the image in custom size without degrading the quality. When I extracting images in local disk, saved image size is too large which unnecessary increasing the PDF size. I also tried to scaled the images but its degrading the overall quality of PDF. Below refer the below code I have used in my project.

Document pdfDocument = null;
MemoryStream inputMemoryStream = new MemoryStream(PDFBytes);
pdfDocument = new Document(inputMemoryStream);
foreach (Page page in pdfDocument.Pages)
{
foreach (Aspose.Pdf.XImage image in page.Resources.Images)
{
using (MemoryStream imageStream = new MemoryStream())
{
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
FileStream str = new FileStream(tempPath + CurrentPage.ToString() + “.jpg”, FileMode.OpenOrCreate);
image.Save(str, ImageFormat.Jpeg);//Image size is too large which increasing the new pdf size.
str.Close();

@subodhsubujee,
Kindly send us your source PDF document. We will investigate and share our findings with you. Your response is awaited.

You can refer any pdf which which is scanned with standard dpi . you would see the difference in source and destination file.
Below is sample code.

Document pdfDocument = null;
MemoryStream inputMemoryStream = new MemoryStream(PDFBytes);
pdfDocument = new Document(inputMemoryStream);

foreach (Page page in pdfDocument.Pages)
{
foreach (Aspose.Pdf.XImage image in page.Resources.Images)
{
using (MemoryStream stream = new MemoryStream())
{
image.Save(stream, imageCodecInfo, lstEncoderParams);
System.Drawing.Image scaledImage = System.Drawing.Image.FromStream(stream);

//Scale down the Image
if (scaledImage.Height > _MaxSupportedHeight || scaledImage.Width > _MaxSupportedWidth)
{
scaledImage = scaledImage.GetThumbnailImage(_MaxSupportedWidth, _MaxSupportedHeight, null, IntPtr.Zero);
_ImageQuality = 70;
}

str = new FileStream(tempPath + CurrentPage.ToString() + ".Jpg", FileMode.OpenOrCreate);
scaledImage.Save(str, ImageFormat.Jpeg);
str.Close();

str = new FileStream(tempPath + CurrentPage.ToString() + ".Jpg", FileMode.Open);
str.Close();
page.Resources.Images.Replace(idx, new FileStream(tempPath + CurrentPage.ToString() + ".Jpg", FileMode.Open),_ImageQuality);

}

@subodhsubujee,
Your code is not in fully compilable form, kindly recheck and share the complete details of the use case with a sample PDF. We could not replicate the said problem with our sample PDF document. However, you can extract an image with Aspose.Pdf for .NET API, and then change the size of the image without downgrading its quality with Aspose.Imaging API. Please refer to these help topics: [Resize an Image with Aspose.Imaging for .NET API](https://docs.aspose.com/display/imagingnet/Crop,+Rotate+and+Resize+Images#Crop,RotateandResizeImages-ResizingImages) and Extract image from a PDF document

Messaged you my code & sample pdf.

@subodhsubujee,
We have tested your code with the latest version of Aspose.Pdf for .NET 17.9 and Aspose.BarCode for .NET 17.8 APIs. The output PDF looks fine to us. This is the output PDF: OutQR_Pdf.zip

Please look the output file size. Can we control its size with some good quality.

@subodhsubujee,
Please optimize resources before saving the output PDF document as follows:

[C#]

// Optimzie the file size by removing unused objects
pdfDocument.OptimizeResources(new Document.OptimizationOptions()
{
    LinkDuplcateStreams = true,
    RemoveUnusedObjects = true,
    RemoveUnusedStreams = true,
    CompressImages = true,
    ImageQuality = 10,
    UnembedFonts = true
});
pdfDocument.Save(tempPath + tempFile);
byte[] PDFWithQRCode = File.ReadAllBytes(tempPath + tempFile);

I have read this article but there is no guaranty this will optimize the resource every time. Can you please go through the code which I have shared , and look at the logic where I am extracting each images. Image size is too big that’s why I have compressed the image resolution.
E.g
if (scaledImage.Height > 1754 || scaledImage.Width > 1024)
{
//Scale down the Image to maximum supported resolution.
scaledImage = scaledImage.GetThumbnailImage(1024, 1754, null, IntPtr.Zero);
}

Is there any way to extract the image in our customized size. Please review my code.

@subodhsubujee,
When we open this PDF with Acrobat, and manually save this image to the computer storage, the output size is also large. Aspose.Pdf for .NET API does not customize images. However, you can use Aspose.Imaging for .NET API to resize an image or keep using the recent workaround. Please refer to this help topic: Resize an Image with Aspose.Imaging API

If you come across any such scenario, then we recommend to proactively post this use case. We will investigate and share our findings with you.

Can we store MemoryStream object into Aspose.Imaging.Image object ?

I have also tried the above recommendation to Resize an Image with Aspose.Image API but I am getting some exception.

Messaged you the new code. You can refer the previous sample pdf file .

Please advise.

@subodhsubujee,
The Image class of Aspose.Imaging API allows to import and export an image to/from a memory stream object. We have posted your query in the Aspose.Imaging forum and marked this thread as private. One of our fellow workers will assist your there soon.

Forum thread:
https://forum.aspose.com/t/error-on-resizing-an-image-with-aspose-image-api/164047