Home › Forums › TWAIN Classic › BitDepth Set for B/W (C#)
- This topic has 3 replies, 2 voices, and was last updated 15 years, 7 months ago by kitchenam.
- AuthorPosts
I’m trying to scan an image with the lowest resolution and bit depth settings possible. I’m using an HP Scanjet 7650 and scanning the image to JPG. In code (C#), I set the resolution to 150 for x and y, pixel type to 1 for B/W, and bit depth to 1. All settings are stat=success. When I view the properties of the saved image I see that the image pixel dimensions are 150/150 (as set), but the bit depth is 24 rather than 1 (inflating the image size). Anyone have a thought as to what the issue may be?
Here’s the code…
...
// set pixel type to black and white
TwCapability ptCap = new TwCapability(TwCap.IPixelType, (short)TwPT.BW, TwType.Int16);
rc = DScap( appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, ptCap);
if( rc != TwRC.Success)
{
CloseSrc();
return false;
}
// set pixel bit depth to 1 bit (b/w)
TwCapability bdCap = new TwCapability(TwCap.BitDepth, (short)1, TwType.UInt16);
rc = DScap( appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, bdCap);
if( rc != TwRC.Success)
{
CloseSrc();
return false;
}
...Thanks…
-aaron
I’d try setting those caps in reverse order, mostly because http://twain.org/docs/CapOrderForWeb.PDF says that ICAP_BITDEPTH comes before ICAP_XRESOLUTION & ICAP_YRESOLUTION
Then I’d run the Get triplet after all of my Set triplets to check that the device accepted my Sets (it’s odd but it’s twain)
Then I’d check the twImageInfo while the image was coming down.
If none of that worked I’d test my code against a known device – like a Kodak i30 in simulation mode.
Failing that… yeah I dunno, if none of that works post back.
EDITED: and i’d test this in the twain toolkit to see if the toolkit reproduced my problem, because if the toolkit reproduces your problem it’s more likely a problem with the driver. And with HP devices I would look for the ‘commercial’ or ‘minimal’ driver on the Cd – it isn’t the default and it works better.
.
Thanks for the reply, gabe. The scanner was apparently getting set properly. The problem I had was that the image was being saved via a method that uses the gdiplus.dll (from the NETMaster twain project) which, apparently, saves the image in a 24 bpp format regardless of the scanner settings. I modified the software to import the image into a .NET Image object and conducted the save from the .NET environment without altering the image. Problem solved.
Thanks again.
-aaron
I thought I’d post the working SaveDIBAs method code for reference if anyone else runs across this issue.
public static bool SaveDIBAs( string fileName, IntPtr bminfo, IntPtr pixdat )
{
Guid clsid;
if( ! GetCodecClsid( fileName, out clsid ) )
{
MessageBox.Show( "Unknown picture format for extension " + Path.GetExtension( fileName ), "Image Codec", MessageBoxButtons.OK, MessageBoxIcon.Information );
return false;
}
IntPtr img = IntPtr.Zero;
int st = GdipCreateBitmapFromGdiDib( bminfo, pixdat, ref img );
if( (st != 0) || (img == IntPtr.Zero) )
return false;
// obtain managed bitmap image from gdiplus dll
MethodInfo mi = typeof(Bitmap).GetMethod("FromGDIplus", BindingFlags.Static | BindingFlags.NonPublic);
Bitmap bmp = (Bitmap)mi.Invoke(null, new object[] {img});
bmp.Save(fileName);
bmp.Dispose();
// causing the image to be saved in a default 24 bpp inflated format
//st = GdipSaveImageToFile( img, fileName, ref clsid, IntPtr.Zero );
GdipDisposeImage( img );
return st == 0;
}
- AuthorPosts