Solved

How do I read a bitmap or jpeg file into a byte array in Microsoft unmanaged C++?

Posted on 2014-12-14
3
973 Views
Last Modified: 2014-12-24
Hi:

It's been many years since I last did any C++ coding.
I'm writing in VS unmanaged C++.

This is probably a pretty basic question.

How do I read either a bitmap or jpeg file into a byte array?
(Byte is typedef'd as "unsigned char")

Thanks,
JohnB
0
Comment
Question by:jxbma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 40499393
Since you'll end up having to you functions outside the 'std' namespace to obtain e.g. the file's size, why now using the Windows API directly? E.g.

#include <windows.h>
#include <tchar.h>

//...

HANDLE hFile = CreateFile(_T("image.jpg"), FILE_READ,0,NULL, OPEN_EXISTING,0,NULL);

DWORD dwSize = GetFileSize(hFile,NULL);

BYTE* buf = new BYTE[dwSize];

DWORD dwRead = 0;
ReadFile(hFile,(LPVOID)buffer,dwSize,&dwRead,NULL);

CloseHandle(hFile);

Open in new window


(error echecking omitted for brevity)

Using STL classes, that could be like

#include <fstream>
#include <sys/stat.h> // for '_stat()'

using namespace std;

//  ...

ifstram is("image.jpg",ios_base::binary);

struct _stat status;

_stat("image.jpg",&status);

BYTE* buf = new BYTE[status.st_size];

is.read(buf,status.st_size);

Open in new window


Of course, you could still do that the C way, but since using 'fopen()/fread()' was your goal, you'd have asked in the C area (and it is basically similar to the above)
0
 
LVL 34

Accepted Solution

by:
sarabande earned 250 total points
ID: 40501010
the second sample of jkr has a typo and the read statement will not compile because ifstream::read expects a char buffer rather than a byte buffer. normally, you also would call stat function before opening the stream. so when turning jkr's code snippet to a function, it would look like

bool readBinaryFile(const std::string & strFilePath, std::vector<unsigned char>& byteArr)
{
       struct stat filestat = { 0 };
       if (stat(strFilePath.c_str(), &filestat) != 0)
            return false;    // error, probably 'file not found'.  use errno or GetLastError() to know more

       std::ifstream ifs(strFilePath.c_str(), std::ios::binary | std::ios::in);

       if (!ifs)
            return false; // error probably 'access denied'
       // allocate memory
       byteArr.resize(filestat.st_size);

       if (!ifs.read((char*)&byteArr[0], filestat.st_size))
            return false; // the read rarely fails beside you remove the stick while reading
       ifs.close();
       return true;
}

Open in new window


you would call the function like

std::string filename = "c:\\temp\\bitmap.bmp";
std::vector<unsigned char> buffer;

if (readBinaryFile(filename, buffer) && !buffer.empty())
{
        size_t size = buffer.size();
        showBitmap(&buffer[0], size);  // you could pass byte vector as BYTE* to other functions
        ...

Open in new window


Sara
0
 
LVL 1

Author Closing Comment

by:jxbma
ID: 40516914
Thanks man!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question