0

I'm currently getting scanned pages through Twain and transforming the pages to Bitmap, using the BitmapRenderer of twaindotnet project, as described in this post.

My scanner allows me to scan recto and verso. When I scan recto only pages, it works like a charm: the generated bitmaps are perfect. But when it scans recto-verso, the bitmap are flipped . Sometimes vertically, sometimes horizontally.

I can't use the Bitmap.RotateFlip() method because the effect doesn't concern each picture, but only when recto-verso pages.

I've tried the Bitmap.FromHbitmap() described here or the default constructor, but it throws an error related to GDI+.

I'm pretty sure the issue is where the bitmap is converted from the pointer, in the BitmapRenderer class. Here is the code (I did not include the Dispose() methods for clarity purpose) :

public class BitmapRenderer : IDisposable
{
    private readonly IntPtr _picturePointer;
    private readonly IntPtr _bitmapPointer;
    private readonly IntPtr _pixelInfoPointer;
    private Rectangle _rectangle;
    private readonly BitmapInfoHeader _bitmapInfo;

    /// <summary>
    /// Initializes a new instance of the <see cref="BitmapRenderer"/> class.
    /// </summary>
    /// <param name="picturePointer_">The picture pointer.</param>
    public BitmapRenderer(IntPtr picturePointer_)
    {
        _picturePointer = picturePointer_;
        _bitmapPointer = Kernel32Native.GlobalLock(picturePointer_);

        _bitmapInfo = new BitmapInfoHeader();
        Marshal.PtrToStructure(_bitmapPointer, _bitmapInfo);

        _rectangle = new Rectangle();
        _rectangle.X = _rectangle.Y = 0;
        _rectangle.Width = _bitmapInfo.Width;
        _rectangle.Height = _bitmapInfo.Height;

        if (_bitmapInfo.SizeImage == 0)
        {
            _bitmapInfo.SizeImage = ((((_bitmapInfo.Width*_bitmapInfo.BitCount) + 31) & ~31) >> 3)*
                                    _bitmapInfo.Height;
        }

        // The following code only works on x86
        Debug.Assert(Marshal.SizeOf(typeof (IntPtr)) == 4);

        int pixelInfoPointer = _bitmapInfo.ClrUsed;
        if ((pixelInfoPointer == 0) && (_bitmapInfo.BitCount <= 8))
            pixelInfoPointer = 1 << _bitmapInfo.BitCount;

        pixelInfoPointer = (pixelInfoPointer*4) + _bitmapInfo.Size + _bitmapPointer.ToInt32();

        _pixelInfoPointer = new IntPtr(pixelInfoPointer);
    }

    /// <summary>
    /// Renders to bitmap.
    /// </summary>
    /// <returns></returns>
    public Bitmap RenderToBitmap()
    {
        Bitmap bitmap = new Bitmap(_rectangle.Width, _rectangle.Height);

        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            IntPtr hdc = graphics.GetHdc();
            try
            {
                Gdi32Native.SetDIBitsToDevice(hdc, 0, 0, _rectangle.Width, _rectangle.Height,
                                              0, 0, 0, _rectangle.Height, _pixelInfoPointer, _bitmapPointer, 0);
            }
            finally
            {
                graphics.ReleaseHdc(hdc);
            }
        }
        bitmap.SetResolution(PpmToDpi(_bitmapInfo.XPelsPerMeter), PpmToDpi(_bitmapInfo.YPelsPerMeter));
        return bitmap;
    }

    private static float PpmToDpi(double pixelsPerMeter_)
    {
        double pixelsPerMillimeter = pixelsPerMeter_/1000.0;
        double dotsPerInch = pixelsPerMillimeter*25.4;
        return (float) Math.Round(dotsPerInch, 2);
    }

I don't understand where this is from or how to solve it.

EDIT

Well, it appears this situation is not related to conversion to bitmap (the issue is not from twaindotnet project at all).

It only occurs with handwritten pages. This is an automatic OCR issue. Does someone know how to disable OCR for handwritten document ?

Community
  • 1
  • 1
Nate B.
  • 942
  • 11
  • 31
  • You really don't know when it's flipped vertically or horizontally? – Thomas Ayoub Dec 18 '14 at 13:39
  • Nope, otherwise it would be easy. Maybe there is a way, but I hardly see how and where – Nate B. Dec 18 '14 at 13:53
  • I recommend this question: http://meta.stackoverflow.com/questions/280408/how-do-i-ask-a-question-regarding-a-heisenbug?cb=1 but I can't help you with this amount of informations :/ – Thomas Ayoub Dec 18 '14 at 13:55
  • Thanks for your comment Thomas. I've edited my post, I hope this is clearer. – Nate B. Dec 18 '14 at 14:14
  • Does it happen at all on any other application? It might be a scanner twain driver issue (in which case it's not possible that a twain library could make it go away). Which scanner is it? – Jcl Dec 18 '14 at 16:00
  • It's a Fujitsu fi-7160. It occurs only with handwritten pages, so it is an OCR issue. I don't know how to disable this OCR check. Any idea ? – Nate B. Dec 19 '14 at 07:33

0 Answers0