Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Convert .NET Bitmap to Leptonica Pix #1

nguyenq opened this Issue Dec 13, 2012 · 7 comments


None yet
4 participants

nguyenq commented Dec 13, 2012

I looked but couldn't find a function that converts .NET Bitmap type to Leptonica Pix type. I tried the following code but the depth value returned by LeptonicaAPI is always -1, causing exceptions to be thrown:

    private IPix ConvertBitmapToPix(Bitmap bmp)
        IntPtr pval = IntPtr.Zero;
        BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

            pval = bd.Scan0;
            //var depth1 = Bitmap.GetPixelFormatSize(bmp.PixelFormat);
            //var depth2 = Tesseract.Interop.LeptonicaApi.GetDepth(pval);
            return Pix.Create(pval);

LeshaG commented Dec 14, 2012

Maybe you are talking about this (image is path to file)?

IPix image2 = Pix.LoadFromFile(image);

Tesseract.Page text = tess.Process(image2, rect, PageSegMode.Auto);


charlesw commented Dec 14, 2012

Yes if you can use the Pix.LoadFromFile(imagePath) that would be the best approach, provided you don't need to convert it to\from a bitmap. However if you need to use a System.Drawing.Image you'll need to do something like this:

  1. Lock bitmap
  2. Create a new Pix with the correct width, height, and depth (bits per pixel, can be 1,2,4,8,16, 32).
  3. Iterate through bitmap data and set corresponding pix data *
  4. Unlock bitmap (finally)
  • Note: I missed adding color support when porting Leptonica's Pix structure. I'll work on adding that next and may look into adding some System.Image conversion then.

nguyenq commented Dec 15, 2012

I knew about LoadFromFile, but it's not what I need. My program manipulates .NET bitmaps before sending them for OCR, thus requiring their conversion to Leptonica Pix image.

Yes, .NET Pix type needs to expose the image data for setting or manipulations.


@charlesw charlesw added a commit that referenced this issue Jan 5, 2013

@charlesw charlesw Convert from Bitmap to Pix - #1
Added support for converting from a 32bpp or 24bpp Bitmap to Leptonicas
Pix structure. Also did some reworked Pix to make data access more like

charlesw commented Jan 6, 2013

I've added support for converting Bitmaps with the pixel format Format32bppArgb and Format24bppRgb to a Pix (use PixConverter.ToPix). These seem to be the most common pixel formats however I have not yet added support for 8bpp since I haven't yet ported Leptonicas ColorMap\Pallet handling (Issue #2). I plan to look into that next however probably won't be until next weekend at the earliest. Will also need to add the inverse operation (Pix to Bitmap) at some point. Any feedback on which pixel formats are required would be most welcome. Feel free to add support for them as push request (see BitmapToPixelConverter).


nguyenq commented Jan 10, 2013

Binary (B/W or monochome) and 8-bit grayscale are also very common formats in OCR applications. I can wait for their integrated support and then provide testing feedback. Thanks.

@charlesw charlesw added a commit that referenced this issue Jan 12, 2013

@charlesw charlesw Added support for colormaps - #1 #2
Added support for 8 bit color maps and updated BitmapToPixConverter to
support 8Bit Indexed images.

@charlesw charlesw was assigned Jan 19, 2013


charlesw commented Jan 19, 2013

Added support for converting 8bit bitmaps to pix's. I'm closing this task as we should now support all the major formats. Please feel free to file a new issue if you need support for another PixelFormat. As per issue #3 we also now support converting pix's to bitmaps for display purposes.

@charlesw charlesw closed this Jan 19, 2013

Shawnbam commented Jul 2, 2016 edited

I am having problem converting pix to bitmap,
Pix pixs=baseApi.getThresholdedImage();
Bitmap bitmap = WriteFile.writeBitmap(pixs);
i get error on last line i.e. getThresholdedImage() returns null value
plz help

@charlesw charlesw pushed a commit that referenced this issue Aug 27, 2016

@nguyenq nguyenq Merge pull request #1 from charlesw/develop
Sync to Develop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment