Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TWAIN.cs Native Transfer Memory Cloning Not Necessary? #34

Open
mattzirkle opened this issue Jun 3, 2019 · 1 comment
Open

TWAIN.cs Native Transfer Memory Cloning Not Necessary? #34

mattzirkle opened this issue Jun 3, 2019 · 1 comment

Comments

@mattzirkle
Copy link

If you look at the function TWAIN::NativeToBitmap, you will find the following code:

// So we make a copy (ick)...
Bitmap bitmap;
switch (bitmapinfoheader.biBitCount)
{
	default:
	case 24:
		bitmap = bitmapStream.Clone(new Rectangle(0, 0, bitmapStream.Width, bitmapStream.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
		break;
	case 8:
		bitmap = bitmapStream.Clone(new Rectangle(0, 0, bitmapStream.Width, bitmapStream.Height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
		break;
	case 1:
		bitmap = bitmapStream.Clone(new Rectangle(0, 0, bitmapStream.Width, bitmapStream.Height), System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
		break;
}

// Fix the resolution...
bitmap.SetResolution((int)(bitmap.HorizontalResolution + 0.5), (int)(bitmap.VerticalResolution + 0.5));

As you can tell by the comments, it looks like the intention here is to make sure the Bitmap has the correct PixelFormat. However, from my testing, it looks like the Bitmap in the variable bitmapStream that is created from the byte[] already has the correct PixelFormat for what was selected (full color [PixelFormat.Format24bppRgb], grayscale [PixelFormat.Format8bppIndexed], or black and white [PixelFormat.Format1bppIndexed]).

Is there a reason that the PixelFormat would come back incorrect that the copy is necessary? Or is this a relic of something that was not handled in C++ but is handled in the .NET Framework (GDI+)?

@mattzirkle
Copy link
Author

Also, right now this is tied directly to returning a Bitmap object. Something that would be more performant is to return a MemoryStream. A MemoryStream is already getting created in this case, so it might not be too much work to change all of the transfer methods to use this as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant