Click here to Skip to main content
11,803,861 members (78,734 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VC7.1 C++
Hi Experts,

How to do this??

Posted 23-May-12 22:33pm
Code-o-mat at 24-May-12 4:41am
your question is a bit vague, give some more details about what you want to do, otherwise we can only try and guess...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

1. Load image using package of choice - GDI+, CxImage, etc
2. Create HBITMAP from image
3. Call GetDIBits on the HBITMAP

When inserting image data into PDFs, one must perform the same steps. Here's the code I use for this task: (Note: you also have to add code to initialize and shutdown GDI+)

// BMP, GIF, JPEG, PNG, TIFF, Exif, WMF, and EMF
HBITMAP mLoadImg(WCHAR *szFilename)
    HBITMAP result=NULL;
    Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(szFilename,false);
    bitmap->GetHBITMAP(NULL, &result);
    delete bitmap;
    return result;
// Returns the DI (Device Independent) bits of the Bitmap
// Here I use 24 bit since it's suppported in pdf
// result is width*height*3 bytes (24 bit)
char unsigned *myGetDibBits24(HBITMAP hBmpSrc)
    BITMAP bm;
    BOOL bRes;
    char unsigned *buf, *result;
    long outIndex, inIndex;
    long width, height, x, y;
    HDC memDC;
    HBITMAP oldBmp;
    memDC = CreateCompatibleDC(NULL);
    oldBmp = (HBITMAP)SelectObject(memDC, hBmpSrc);
    GetObject(hBmpSrc, sizeof(bm), &bm);
    width = bm.bmWidth;
    height = bm.bmHeight;
    bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
    bi.bmiHeader.biWidth = width;
    bi.bmiHeader.biHeight = -height;
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 32;
    bi.bmiHeader.biCompression = BI_RGB;
    bi.bmiHeader.biSizeImage = 0;//bm.bmWidth * 4 * bm.bmHeight;
    bi.bmiHeader.biClrUsed = 0;
    bi.bmiHeader.biClrImportant = 0;
    buf = new unsigned char[width * 4 * height];
    bRes = GetDIBits(memDC, hBmpSrc, 0, bm.bmHeight, buf, &bi, DIB_RGB_COLORS);
    SelectObject(memDC, oldBmp);
    if (!bRes)
        buf = NULL;
    result = new unsigned char[width*height*3];
    outIndex = 0;
    inIndex = 0;
    for (y=0; y<height;>    {
        inIndex = y * bm.bmWidthBytes;
        for (x=0; x<width;>        {
            result[outIndex++] = buf[inIndex+2];
            result[outIndex++] = buf[inIndex+1];
            result[outIndex++] = buf[inIndex];
            inIndex += 4;
    return result;
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Technically speaking a jpeg image is just an array of bytes (like any other file).
However, I suppose, you actually want to gain access to pixel values. I think the easy way is loading the image using the CImage class and then using the CImage::GetBits[^] method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 450
1 CPallini 320
2 F-ES Sitecore 240
3 Sergey Alexandrovich Kryukov 234
4 CHill60 220
0 OriginalGriff 3,000
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,755
4 Richard MacCutchan 1,187

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 24 May 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100