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;
}
More fun with Win32
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.
Chris Barry (crbarry at mts.net)
My Personal Programming Depot
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
[ 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!
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!
Good to know, but still not working. Out of curiosity, if an object is selected into one device context, it can''t be selected into another, can it? If that''s the case, I think I found my problem ......
Chris Barry (crbarry at mts.net)
My Personal Programming Depot
Chris Barry (crbarry at mts.net)
My Personal Programming Depot
quote:Original post by 23yrold3yrold
Out of curiosity, if an object is selected into one device context, it can''t be selected into another, can it?
No, it can''t. If that''s your bug, excellent.
[ 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!
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 ......
Chris Barry (crbarry at mts.net)
My Personal Programming Depot
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
Try this:
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]
HGDIOBJ hOldObject = SelectObject(hdc, hJustCreatedObject);// Use hJustCreatedObjectSelectObject(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]
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
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
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
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
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement