Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

23yrold3yrold

More fun with Win32

This topic is 5914 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m making a little MSPaint clone (just for learning) and I''m having trouble with resizing the image. Ideally, resizing creates a new bitmap with the new dimensions, clears it, blits the old bitmap to it, selects the new bitmap into the device context and destroys the old one. It seems to hang on to the old one for some reason. Can someone show me my error in this code, or maybe where I can check return values for helpful errors here? Thanks. This is a member function. hdc, hbitmap, width and height are member variables that are hopefully self explanitory.
  
      void ResizeBitmap(int w, int h)
      {
         HDC     temphdc    = CreateCompatibleDC(hdc);
         HBITMAP tempbitmap = CreateCompatibleBitmap(temphdc, w, h);
         SelectObject(temphdc, tempbitmap);
         ::PatBlt(temphdc, 0, 0, w, h, WHITENESS);

         BitBlt(temphdc, 0, 0, width, height, hdc, 0, 0, SRCCOPY);

         DeleteObject(hbitmap);
         hbitmap = tempbitmap;
         SelectObject(hdc, hbitmap);
         DeleteObject(temphdc);

         width  = w;
         height = h;
      }
  
Chris Barry (crbarry at mts.net) My Personal Programming Depot

Share this post


Link to post
Share on other sites
Advertisement
An object selected into a device context cannot be deleted. You need to call SelectObject on temphdc with the return value of the original SelectObject call - all prior to calling DeleteObject on it. ie

HBITMAP hbmOrg = SelectObject( temphdc, tempbitmap );
...
SelectObject( temphdc, hbmOrg );
DeleteObject( temphdc );
...


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
I''m having trouble getting the dc''s to cough up their hbitmaps, though. It works, but must I declare new hbitmaps (and create bitmaps for them to point at, else it doesn''t work) just to pass to SelectObject just to get the hbitmaps free just so I can swap them? Here''s my code, which, again, works, but I''m curious if there''s a more effecient way. Don''t laugh ......


        void ResizeBitmap(int w, int h)
{
HDC temphdc = CreateCompatibleDC(hdc);
HBITMAP tempbitmap = CreateCompatibleBitmap(temphdc, w, h);
SelectObject(temphdc, tempbitmap);
::PatBlt(temphdc, 0, 0, w, h, WHITENESS);

BitBlt(temphdc, 0, 0, width, height, hdc, 0, 0, SRCCOPY);

HBITMAP temp1 = CreateCompatibleBitmap(hdc, 1, 1), temp2 = CreateCompatibleBitmap(hdc, 1, 1);
hbitmap = (HBITMAP)SelectObject(temphdc, temp1);
tempbitmap = (HBITMAP)SelectObject(hdc, temp2);
SelectObject(hdc, hbitmap);

DeleteObject(temphdc);
DeleteObject(tempbitmap);

width = w;
height = h;
}


Chris Barry (crbarry at mts.net)
My Personal Programming Depot

Share this post


Link to post
Share on other sites
Try this:

      
HGDIOBJ hOldObject = SelectObject(hdc, hJustCreatedObject);
// Use hJustCreatedObject

SelectObject(hdc, hOldObject);
Deleteobject(hJustCreatedObject);

As a side note, shouldn't you be using StretchBlt instead of PatBlt & BitBlt?

Edit: I forgot to mention that your code has a resource leak.

Edit: Fixed DeleteObject call.

[edited by - IndirectX on May 5, 2002 12:41:59 AM]

Share this post


Link to post
Share on other sites
For the resource leak, I assume you mean temp1 and temp2. I''ll get to it And I could stretch it; I just wanted it working now.

DeleteObject() takes two variables??

It''s late and I''m going to bed, so I''ll check your idea tomorrow. Thanks.

Chris Barry (crbarry at mts.net)
My Personal Programming Depot

Share this post


Link to post
Share on other sites
Before you call this line:

DeleteObject(temphdc);

Aren''t you supposed to call ReleaseDC(temphdc);, or something like that?

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

Share this post


Link to post
Share on other sites
quote:
Original post by CaptainJester
Before you call this line:

DeleteObject(temphdc);

Aren''t you supposed to call ReleaseDC(temphdc);, or something like that?


No. DCs are released with either ReleaseDC or DeleteDC, wich are mutually exclusive. Each one has its scope (ie, releases particular types of DCs). You don''t use DeleteObject on a DC.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!