Why is my image distorted when decoding as FlateDecode using iTextSharp?

前端 未结 2 2141
Happy的楠姐
Happy的楠姐 2021-01-06 05:26

When decoding an image within a PDF as FlateDecode via iTextSharp the image is distorted and I can\'t seem to figure out why.

The recognized bpp is

2条回答
  •  暖寄归人
    2021-01-06 05:49

    Try copy your data row by row, maybe it will solve the problem.

    int w = imgObj.GetAsNumber(PdfName.WIDTH).IntValue;
    int h = imgObj.GetAsNumber(PdfName.HEIGHT).IntValue;
    int bpp = imgObj.GetAsNumber(PdfName.BITSPERCOMPONENT).IntValue;
    var pixelFormat = PixelFormat.Format1bppIndexed;
    
    byte[] rawBytes = PdfReader.GetStreamBytesRaw((PRStream)imgObj);
    byte[] decodedBytes = PdfReader.FlateDecode(rawBytes);
    byte[] streamBytes = PdfReader.DecodePredictor(decodedBytes, imgObj.GetAsDict(PdfName.DECODEPARMS));
    // byte[] streamBytes = PdfReader.GetStreamBytes((PRStream)imgObj); // same result as above 3 lines of code.
    
    using (Bitmap bmp = new Bitmap(w, h, pixelFormat))
    {
        var bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, pixelFormat);
        int length = (int)Math.Ceiling(w * bpp / 8.0);
        for (int i = 0; i < h; i++)
        {
            int offset = i * length;
            int scanOffset = i * bmpData.Stride;
            Marshal.Copy(streamBytes, offset, new IntPtr(bmpData.Scan0.ToInt32() + scanOffset), length);
        }
        bmp.UnlockBits(bmpData);
    
        bmp.Save(fileName);
    }
    

提交回复
热议问题