Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I use iTextSharp to convert pdf to image.

When go to : System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);

It have error :Parameter is not valid.

Edit - Moved OP's code here from Solution section.

This is code,I hope you more understand my question.Thanks!!

C#
public static void ExtractImagesFromPDF(string sourcePdf, string outputPath)
{
    // NOTE:   This will only get the first image it finds per page.
    PdfReader pdf = new PdfReader(sourcePdf);
    RandomAccessFileOrArray raf = new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf);
 
    try
    {
        for (int pageNumber = 1; pageNumber <= pdf.NumberOfPages; pageNumber++)
        {
          PdfDictionary pg = pdf.GetPageN(pageNumber);
          PdfDictionary res =
          (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
          PdfDictionary xobj =
          (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
            if (xobj != null)
             {
               foreach (PdfName name in xobj.Keys)
               {
                 PdfObject obj = xobj.Get(name);
                  if (obj.IsIndirect())
                  {
                    PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                    PdfName type =
                    (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                      if (PdfName.IMAGE.Equals(type))
                      {
 
                      int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));
                      PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);
                      PdfStream pdfStrem = (PdfStream)pdfObj;
                      byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)pdfStrem);
                      if ((bytes != null))
                      {
                        using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes))
                        {
                          memStream.Position = 0;
                          System.Drawing.Image img = System.Drawing.Image.FromStream(memStream,true, false);
                          // must save the file while stream is open.
                           if (!Directory.Exists(outputPath))
                           Directory.CreateDirectory(outputPath);
 
                           string path = Path.Combine(outputPath, String.Format(@"{0}.jpg", pageNumber));
                           System.Drawing.Imaging.EncoderParameters parms = new System.Drawing.Imaging.EncoderParameters(1);
                           parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0);
// GetImageEncoder is found below this method
                           System.Drawing.Imaging.ImageCodecInfo jpegEncoder = GetImageEncoder("JPEG");
                           img.Save(path, jpegEncoder, parms);
                           break;


Pls give me a solution!!
Thanks.
Posted
Updated 29-Nov-11 17:08pm
v2
Comments
Sergey Alexandrovich Kryukov 29-Nov-11 22:25pm    
Not enough information. Can you look at this from the standpoint of the one who tries to help you?
--SA

This is code,I hope you more understand my question.Thanks!!

public static void ExtractImagesFromPDF(string sourcePdf, string outputPath)
{
// NOTE: This will only get the first image it finds per page.
PdfReader pdf = new PdfReader(sourcePdf);
RandomAccessFileOrArray raf = new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf);

try
{
for (int pageNumber = 1; pageNumber <= pdf.NumberOfPages; pageNumber++)
{
PdfDictionary pg = pdf.GetPageN(pageNumber);
PdfDictionary res =
(PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
PdfDictionary xobj =
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
if (xobj != null)
{
foreach (PdfName name in xobj.Keys)
{
PdfObject obj = xobj.Get(name);
if (obj.IsIndirect())
{
PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
PdfName type =
(PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
if (PdfName.IMAGE.Equals(type))
{

int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));
PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);
PdfStream pdfStrem = (PdfStream)pdfObj;
byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)pdfStrem);
if ((bytes != null))
{
using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes))
{
memStream.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(memStream,true, false);
// must save the file while stream is open.
if (!Directory.Exists(outputPath))
Directory.CreateDirectory(outputPath);

string path = Path.Combine(outputPath, String.Format(@"{0}.jpg", pageNumber));
System.Drawing.Imaging.EncoderParameters parms = new System.Drawing.Imaging.EncoderParameters(1);
parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0);
// GetImageEncoder is found below this method
System.Drawing.Imaging.ImageCodecInfo jpegEncoder = GetImageEncoder("JPEG");
img.Save(path, jpegEncoder, parms);
break;
 
Share this answer
 
This code looks fine if the memStream is a stream, that's it, it's derived from the abstract type System.IO.Stream. If this cause an error, this is not. However, you did not specify what's an "error": a compilation error or run-time exception.

(Look, this is not nice to ask such questions. Do you like riddles? Why we cannot see what you can easily see. You need to post exact error or exception information, indicate relevant lines of code and know how much code to show. In your post, it's certainly not enough. That's why I have to do some guesswork.)

Here is the thing. What you are trying to do cannot help to convert PDF to image. When you have a stream which can be read by Image.FromStream, you need to have the content of the stream which should be an image in one of the valid image formats like PNG or JPG. I don't think you have it, if your problem is PDF. You are trying to do something wrong in principle; and this is a root cause of your problem.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900