Resizing CBitmap

I want to resize (from 128x128 to 50x50 pixels) my CBitmap..

My code:
                                                // loading bmp from a file
                                                CBitmap bmp;
                                    bmp.Attach(LoadImage(NULL, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));

                                    CDC dcMem;
                                    dcMem.CreateCompatibleDC(NULL);
                                    dcMem.SelectObject(bmp);

                                    CDC b;
                                    b.CreateCompatibleDC(NULL);
                                    b.StretchBlt(0, 0, 50, 50, &dcMem, 0, 0, 128,128, SRCCOPY);

                                    CBitmap n;
                                    n.CreateCompatibleBitmap(&b,50,50);

The problem: My new CBit (n) is all black. What am i doing wrong?
Thank you!
jurijAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
                             CBitmap bmp;
                              bmp.Attach(LoadImage(NULL, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));

                              CDC dcMem;
                              dcMem.CreateCompatibleDC(NULL);
                              dcMem.SelectObject(&bmp);

                              CDC b;
                              b.CreateCompatibleDC(NULL);
                              CBitmap n;
                              n.CreateCompatibleBitmap(&b,50,50);
                              b.SelectObject(&n);
                              b.StretchBlt(0, 0, 50, 50, &dcMem, 0, 0, 128,128, SRCCOPY);


0
jurijAuthor Commented:
It did not help.
I'm still getting a black bitmap.
0
jurijAuthor Commented:
I think the problem is in the line where I create a compatible bitmap. It seems my new bitmap n is not compatible with the old one? or is it? hmm..
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

AlexFMCommented:
The problem is device context. If you have pointer to device context pDC, use this code:

    CBitmap bmp;
    bmp.Attach(LoadImage(NULL, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION));

    CDC dcMem;
    dcMem.CreateCompatibleDC(pDC);
    dcMem.SelectObject(&bmp);

    CDC b;
    b.CreateCompatibleDC(pDC);
    CBitmap n;
    n.CreateCompatibleBitmap(pDC,50,50);
    b.SelectObject(&n);
    b.StretchBlt(0, 0, 50, 50, &dcMem, 0, 0, 128,128, SRCCOPY);

If there is't DC pointer, use this code:

    CWnd* pDesktopWindow = GetDesktopWindow();
    CDC* pDesktopDC = pDesktopWindow->GetDC();

    CBitmap bmp;
    bmp.Attach(LoadImage(NULL, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION));

    CDC dcMem;
    dcMem.CreateCompatibleDC(pDesktopDC);
    dcMem.SelectObject(&bmp);

    CDC b;
    b.CreateCompatibleDC(pDesktopDC);
    CBitmap n;
    n.CreateCompatibleBitmap(pDesktopDC,50,50);
    b.SelectObject(&n);
    b.StretchBlt(0, 0, 50, 50, &dcMem, 0, 0, 128,128, SRCCOPY);

    pDesktopWindow->ReleaseDC(pDesktopDC);
0
jurijAuthor Commented:
It almost works :) Now i can see the picture, but it is not correctly stretched.

If I use:
n.CreateCompatibleBitmap(pDesktopDC,128,128);
...
 b.StretchBlt(0, 0, 128, 128, &dcMem, 0, 0, 128,128, SRCCOPY);

the picture is correct, but of course not stretched.

Thank you!
0
AlexFMCommented:
Do you mean that image is distorted? Use SetStretchBltMode before StretchBlt.
0
jurijAuthor Commented:
The interesting thing is that if i use another bitmat, it works just fine.. hmmm.. something wrong with a bitmap?
0
AlexFMCommented:
Please describe exactly what is the problem. What does this mean "not correctly stretched"?
0
jurijAuthor Commented:
The picture is a bit distorted (2 black diagonal lines running from upper right to lower left corner ).
Although the picture dimensions are correct. Seems like problems with streach?

0
AlexFMCommented:
Did you add SetStretchBltMode before StretchBlt?

int nOldStretchMode = b.SetStretchBltMode(HALFTONE);
b.StretchBlt(0, 0, 50, 50, &dcMem, 0, 0, 128,128, SRCCOPY);
b.SetStretchBltMode(nOldStretchMode);
0
jurijAuthor Commented:
Yes I did :/

And I discovered that if I use:

                                    CWnd* pDesktopWindow = CWnd::GetDesktopWindow();
                                    CDC* pDesktopDC = pDesktopWindow->GetDC();

                                    CBitmap bmp;
                                    bmp.Attach(LoadImage(NULL, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION));

                                    CDC dcMem;
                                    dcMem.CreateCompatibleDC(pDesktopDC);
                                    dcMem.SelectObject(&bmp);

                                    CDC b;
                                    b.CreateCompatibleDC(pDesktopDC);
                                    CBitmap n;
                                    n.CreateCompatibleBitmap(pDesktopDC,64,64);
                                    b.SelectObject(&n);

                                    int nOldStretchMode = b.SetStretchBltMode(HALFTONE);
                                    b.StretchBlt(0, 0, 64, 64, &dcMem, 0, 0, 128, 128, SRCCOPY);
                                    b.SetStretchBltMode(nOldStretchMode);

                                    pDesktopWindow->ReleaseDC(pDesktopDC);

or instead of 64, 64 for instance 32,32 the stretch function forks fine. But when I tre 50,50 my image becomes distorted.
0
AlexFMCommented:
This is my test. I created SDI application and added this code to OnDraw function:

void CtestView::OnDraw(CDC* pDC)
{
    CtestDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    CWnd* pDesktopWindow = GetDesktopWindow();
    CDC* pDesktopDC = pDesktopWindow->GetDC();

    CBitmap bmp;
    bmp.Attach(LoadImage(NULL, _T("test.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION));

    BITMAP bitmap;
    bmp.GetBitmap(&bitmap);

    CDC dcMem;
    dcMem.CreateCompatibleDC(pDesktopDC);
    dcMem.SelectObject(&bmp);

    CDC b;
    b.CreateCompatibleDC(pDesktopDC);
    CBitmap n;
    n.CreateCompatibleBitmap(pDesktopDC,50,50);
    b.SelectObject(&n);
    int nOldStretchMode = b.SetStretchBltMode(HALFTONE);
    b.StretchBlt(0, 0, 50, 50, &dcMem, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
    b.SetStretchBltMode(nOldStretchMode);

    pDC->BitBlt(0, 0, 50, 50, &b, 0, 0, SRCCOPY);

    pDesktopWindow->ReleaseDC(pDesktopDC);
}

Bitmap is properly resized to (50, 50) dimensions and shown on the screen. Works with any bitmap size in test.bmp.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.