?
Solved

Resizing CBitmap

Posted on 2004-11-23
12
Medium Priority
?
2,153 Views
Last Modified: 2012-08-13
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!
0
Comment
Question by:jurij
  • 6
  • 6
12 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 12655505
                             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
 

Author Comment

by:jurij
ID: 12658848
It did not help.
I'm still getting a black bitmap.
0
 

Author Comment

by:jurij
ID: 12659707
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 48

Expert Comment

by:AlexFM
ID: 12662766
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
 

Author Comment

by:jurij
ID: 12664494
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 12664551
Do you mean that image is distorted? Use SetStretchBltMode before StretchBlt.
0
 

Author Comment

by:jurij
ID: 12664691
The interesting thing is that if i use another bitmat, it works just fine.. hmmm.. something wrong with a bitmap?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12664707
Please describe exactly what is the problem. What does this mean "not correctly stretched"?
0
 

Author Comment

by:jurij
ID: 12665011
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 12665119
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
 

Author Comment

by:jurij
ID: 12665454
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
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 12665857
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

809 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