TWAIN Working Group

Newsletter Signup
Donate
Help keep TWAIN free
  • About TWAIN
    • What’s New?
    • News
    • Events
    • Membership
    • Consider a Donation
    • Contact Us
  • Why TWAIN?
  • Developers
    • Driver Developer
    • Application Developer
    • TWAIN Features
    • Specification & Tools
    • Self Certification Process
  • Support Forums
  • Scanner End-User
  • Find Certified Drivers
    • Facebook
    • LinkedIn
    • Vimeo

.Net Bitmap 1bpp problem?

Forums › TWAIN Classic › .Net Bitmap 1bpp problem?

  • This topic has 0 replies, 1 voice, and was last updated 11 years ago by polo.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • April 9, 2010 at 9:29 am #22598 Reply
    polo
    Participant
    • Topics - 6
    • Replies - 5
    • Total Posts - 11

    Hi guys,
    Iam trying to get scanned image from IntPtr in memory to .net Bitmap format.

    Firstly i used peace of code i found on internet. Basicly it does
    MethodInfo mi = typeof(Bitmap).GetMethod(“FromGDIplus”, BindingFlags.Static | BindingFlags.NonPublic);
    GdiPlusLib.Gdip.GdipCreateBitmapFromGdiDib(bmPtr, _pixptr, ref gdiBMP);
    new Bitmap((Bitmap)mi.Invoke(null, new object[] { gdiBMP }));

    this works fine, except that it always produces 32bitARGB bitmap which takes a lot of memory.

    so i start to dig again and created this class, so i will have controll over when the memory allocated by twain is released.

    the code is very simple and works fine, except one case.

    ➡ when i scan 1bpp image i got Attempted to read or write protected memory.

    this is probably caused by the fact that iam trying to work with unalocated memory, but where? And why it only happens when 1bpp picture is scanned?

    💡 code follows:

    namespace csq_twain
    {
    public class TwImage : IDisposable
    {
    private IntPtr lockedImagePtr;
    private IntPtr imagePtr;
    private BITMAPINFOHEADER imageHeader;

    public TwImage(IntPtr p)
    {
    imageHeader = new BITMAPINFOHEADER();
    imagePtr = p;
    lockedImagePtr = Twain.GlobalLock(imagePtr); //zamknout pamet
    Marshal.PtrToStructure(lockedImagePtr, imageHeader); //nahrat hlavicku bmp
    }

    private Bitmap GetImage()
    {
    IntPtr pixelDataHandle = IntPtr.Zero;
    PixelFormat pixelFormat = PixelFormat.Format32bppArgb;
    Bitmap b;
    int stride = 0;
    ColorPalette pal;

    pixelDataHandle = (IntPtr)((int)lockedImagePtr + imageHeader.biSize + imageHeader.biClrUsed * 4); //zacatek dat = velikost hlavicky + paleta * 4 (RGBP)

    switch (imageHeader.biBitCount)
    {
    case 1: //BW
    pixelFormat = PixelFormat.Format1bppIndexed;
    break;
    case 8: //Gray
    pixelFormat = PixelFormat.Format8bppIndexed;
    break;
    case 16:
    pixelFormat = PixelFormat.Format16bppRgb565;
    break;
    case 24:
    pixelFormat = PixelFormat.Format24bppRgb;
    break;
    case 32:
    pixelFormat = PixelFormat.Format32bppArgb;
    break;
    }

    // stride urcuje kolik pixelu ma kazdy radek. Musi byt delitelny 4, takze si tu musime pomoct.
    if (imageHeader.biBitCount == 1)
    stride = imageHeader.biWidth * imageHeader.biBitCount;
    else
    stride = imageHeader.biWidth * (imageHeader.biBitCount >> 3);

    if (stride % 4 != 0)
    {
    int tmp = stride / 4;
    stride = ++tmp * 4;
    }

    b = new Bitmap(imageHeader.biWidth, imageHeader.biHeight, stride, pixelFormat, pixelDataHandle);

    // pokud je > 0 znamena to ze se nejedna o obrazek s indexovanou paletou.
    if (imageHeader.biClrUsed > 0)
    {
    // vytvoreni genericke palety pro grayscale
    pal = b.Palette;

    //for (int i = 0; i < bmpHeader.biClrUsed; i++)
    for (int i = 0; i < pal.Entries.GetLength(0); i++)
    {
    pal.Entries = Color.FromArgb(i, i, i);
    }
    b.Palette = pal;
    }

    Image tb = (Image)b;
    tb.RotateFlip(RotateFlipType.RotateNoneFlipY);
    return tb as Bitmap;
    }

    public Bitmap Image
    {
    get { return this.GetImage(); }
    }

    #region IDisposable Members

    void IDisposable.Dispose()
    {
    Twain.GlobalUnlock(lockedImagePtr);
    Twain.GlobalFree(imagePtr);
    }

    #endregion
    }
    }

  • Author
    Posts
Viewing 1 post (of 1 total)
Reply To: .Net Bitmap 1bpp problem?
Your information:




Quick Links

Service Providers
TWAIN Support Forums
Membership
Contact Us
Privacy Policy

Newsletter Signup

TWAIN Working Group Family

TWAIN Working Group
TWAIN Direct®
TWAIN Resources
TWAIN Certified Drivers
PDF/raster

  • Facebook
  • GitHub
  • LinkedIn
  • Vimeo

Recent Topics

  • Sports betting Bonus Code
  • PDF/R For who and where?
  • Making searchable PDF with PDF/R
  • Backward compatibility with PDF/A and traditional PDF
  • could not open the twain source. Make sure there is a valid source for your sca
  • Quarterly Newsletter
  • TWAIN Working Group Membership
  • Logo Usage
  • TWAIN License
  • Contact Us
Privacy Policy • Privacy Tools • Copyright © 2021 TWAIN Working Group • by iHwy, LLC • Log in

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.