J
J
justslipknot2021-09-04 00:23:05
.NET
justslipknot, 2021-09-04 00:23:05

How to create a large Bitmap in C#?

I'm trying to create a 30k by 30k bitmap, but an ArgumentException is thrown . The ImageSharp library solved the problem , but with it, my images do not load completely and there are artifacts. How to create a large bitmap without additional. libraries and an array of bitmaps (one of the solutions that I found on SO) ?
If there is a way to create a large canvas for placing pictures on it, then I'm ready to consider another way.

What I tried: I wrote gcallowverylargeobjects = true
Application: Console, .NET 6 (I also tried .NET 5 )
The computer is powerful, the RAM should be enough for sure (64GB)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-09-04
@justslipknot

Well, first of all, you definitely won’t be able to use the standard Bitmap.
You should not store such a large object on the heap either, because it will 100% go to LOH
+ in .NET there is a 2GB limit on objects in the managed heap.
The image you describe just does not fit into this limit.
There are two and a half options left:
1. Divide a huge image into smaller chunks (either by pixels, or by channels, or both)
2. Store the image in unmanaged memory, allocate and free memory manually.
In .net 6, they just added the Native alloc API for this.
3. Perhaps there is some native library for working with large images.
There may even already be a wrapper to work with it.
In all cases, you will have to abandon Bitmap and use other approaches

without additional libraries and bitmap array

Will not work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question