Convert image to usable byte array in C?

Does anyone know how to open an image, in particular jpg, in an array of bytes in C or C ++? Any form of help is appreciated.

Thank!

+3
source share
6 answers

The ImageMagick library can also do this, although often it provides sufficient image manipulation functions that you can do a lot without having to convert the image to an array of bytes and process it yourself.

+2
source
+1

wxImage wxWidgets GUI Framework. , , * nix.

GNU Jpeg.

+1

, netpbm , C, , .. , , JPEG, PBM, Unix. djpeg , JPEG Club. , .

0

Here is how I would do it using the GDIPlus Bitmap.LockBits method defined in the GdiPlusBitmap.h header:

    Gdiplus::BitmapData bitmapData;
    Gdiplus::Rect rect(0, 0, bitmap.GetWidth(), bitmap.GetHeight());

    //get the bitmap data
    if(Gdiplus::Ok == bitmap.LockBits(
                        &rect, //A rectangle structure that specifies the portion of the Bitmap to lock.
                        Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeWrite, //ImageLockMode values that specifies the access level (read/write) for the Bitmap.            
                        bitmap.GetPixelFormat(),// PixelFormat values that specifies the data format of the Bitmap.
                        &bitmapData //BitmapData that will contain the information about the lock operation.
                        ))
    {
         //get the lenght of the bitmap data in bytes
         int len = bitmapData.Height * std::abs(bitmapData.Stride);

         BYTE* buffer = new BYTE[len];
         memcpy(bitmapData.Scan0, buffer, len);//copy it to an array of BYTEs

         //... 

         //cleanup
         pBitmapImageRot.UnlockBits(&bitmapData);       
         delete []buffer;
    }
0
source

Source: https://habr.com/ru/post/1739922/


All Articles