Click here to Skip to main content
15,072,391 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,

How to do this??

Thanks
Posted
Comments
Code-o-mat 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...

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+)

C++
// 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)
{
    BITMAPINFO bi;
    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)
    {
        delete(buf);
        buf = NULL;
    }
    DeleteDC(memDC);

    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;
        }
    }
    delete(buf);

    return result;
}
   
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)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900