tmyroadctfig / twaindotnet

MIT License
179 stars 117 forks source link

Memory problems #27

Open gigios opened 8 years ago

gigios commented 8 years ago

I'm trying to create a bridge application (external exe) to allow the acquisition of images from a 64bit application by a 32bit twain driver. My first problem is that the acquired image is very big (7700x7200 pixels) and the size is about 166MB. The problem is that during the full scan sequence the memory reach 700MB and more and I'm not able to save the image on disk (GDI+ raise a generic exception but I'm sure that it is related to the memory). I don't know it there is a possibility to reduce this memory usage, what I have seen is that there are three allocation of 166MB: the acquisition made by the twain driver (DsImageTransfer), the render of the bitmap (RenderToBitmap), and the last is in my code, the convertion in a buffer to allow the send to the main application.

I have found a small workaround by moving the call of the event TransferImage (in the DataSourceManager) outside the block using (using (var renderer = new BitmapRenderer(hbitmap))) in this way the framework is able to reuse the memory allocated with the RenderToBitmap method. The code changed is in the DataSourceManager.cs at line 235:

                    TransferImageEventArgs args;
                    using (var renderer = new BitmapRenderer(hbitmap))
                    {
                        args = new TransferImageEventArgs(renderer.RenderToBitmap(), pendingTransfer.Count != 0);
                    }
                    TransferImage(this, args);
                    if (!args.ContinueScanning)
                        break;

Any other suggestions are appreciated

jeske commented 7 years ago

I just created a branch which adds an incremental MemoryXfer mode which transfers scan images in chunks instead of all at once. Currently it allocates a large bitmap to hold the whole image, but it could be adjusted to pass off partial bitmap buffers instead.

See: https://github.com/jeske/twaindotnet/tree/incremental_scanning

gigios commented 7 years ago

Ok! Thanks for the support. I will test your version as soon as I have the opportunity to work again ont the project in question.

Mark1975 commented 5 years ago

I just created a branch which adds an incremental MemoryXfer mode which transfers scan images in chunks instead of all at once. Currently it allocates a large bitmap to hold the whole image, but it could be adjusted to pass off partial bitmap buffers instead.

See: https://github.com/jeske/twaindotnet/tree/incremental_scanning

Hi Jeske, do you know why your pull request has not been merged with the main branch? I need to implement scanning of 16 bit grayscale images; and a way to achieve that is by using Memory transfer; so your contribution would be a good starting point for me...