FastBitmap | A fast C # Bitmap wrapping layer | Computer Vision library
kandi X-RAY | FastBitmap Summary
kandi X-RAY | FastBitmap Summary
FastBitmap is a bitmap wrapper class that intends to provide fast bitmap read/write operations on top of a safe layer of abstraction. It provides operations for setting/getting pixel colors, copying regions accross bitmaps, clearing whole bitmaps, and copying whole bitmaps.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of FastBitmap
FastBitmap Key Features
FastBitmap Examples and Code Snippets
Community Discussions
Trending Discussions on FastBitmap
QUESTION
I'm working with System.Drawing.Bitmap
, and need access to individual pixels, but GetPixel()
and SetPixel()
are too slow for me.
Using the technique described in this answer, I'm allocating a byte[]
, pinning it in memory, copying my original Bitmap
into it, and unpinning it. I then work with the byte[]
, and later I pin it, construct a new Bitmap
with the buffer, and save it.
I calculate the size of the buffer in bytes as width * height * BytesPerPixel
. I'm using PixelFormat.Format32bppArgb
, so BytesPerPixel == 4
.
This all works fine and dandy, except I apparently allocate a buffer that's too small and cause an access violation when copying the bitmap. I have to add extra space (a lot) to avoid the access violation, and everything appears to work.
How do I calculate the proper size I need for _buffer
?
EDIT: I just discovered that the original image is PixelFormat.Format24bppRgb
. However, that's 3 bytes per pixel and my buffer should be plenty large enough I would think.
Here is a complete program that demonstrates the problem. ExtraSpace
controls the extra bytes I have to allocate...:
ANSWER
Answered 2017-Mar-17 at 02:41Keep in mind that Windows bitmap objects require the stride to be DWORD (4-byte) aligned. 24 bit images with a width not a multiple of 4 will have a stride different from 4 * width.
Also note that the stride is in bytes, not bits, so your calculation is wrong in any case. You're telling the Bitmap
class it has a stride of 32 * width
, which is 8 times as large as it should be, so the GDI+ will run out of valid memory address possibly as early as 1/8th into the image (if you're lucky…if you're not, it'll write random data into somewhere important).
Here is what the calculation should look like:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install FastBitmap
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page