StretchBlt with images?

Just starting out? Need help? Post your questions and find answers here.
hapkidoyoka
New User
New User
Posts: 3
Joined: Sun Dec 05, 2021 1:29 am

StretchBlt with images?

Post by hapkidoyoka »

Hi everyone.

I'm trying to use StretchBlt for resizing an image, but I'm missing something.
I believe that StartDrawing returns a hDC and ImageID returns a HBITMAP, could anyone tell me why the following code doesn't work?

Code: Select all

Procedure SizeImage(image, w, h) 
	Protected newimg, srcobject, dstobject, srcDC, dstDC

	newimg = CreateImage(#PB_Any, w, h) ;create the new image at the requested size

	;device contexts
	dstDC = StartDrawing(ImageOutput(newimg))
	srcDC = CreateCompatibleDC_(dstDC)
	
	;select bitmaps
	srcobject = SelectObject_(srcDC, ImageID(image))
	;dstobject = SelectObject_(dstDC, ImageID(newimg))
	
	StretchBlt_(dstDC, 0, 0, w, h, srcDC, 0, 0, ImageWidth(image), ImageHeight(image), #SRCCOPY)
	
	;SelectObject_(dstDC, dstobject)
	SelectObject_(srcDC, srcobject)
	
 	StopDrawing()
 	DeleteDC_(srcDC)

	ProcedureReturn newimg
EndProcedure
There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: StretchBlt with images?

Post by BarryG »

This is your first post, so I just want to check: did you know there's a ResizeImage() command? I don't want you to reinvent the wheel.
hapkidoyoka
New User
New User
Posts: 3
Joined: Sun Dec 05, 2021 1:29 am

Re: StretchBlt with images?

Post by hapkidoyoka »

Fair point Barry, but yes, I am aware of the ResizeImage command.
I was trying for a little more performance, and thought it would be a good learning exercise too.
There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: StretchBlt with images?

Post by JHPJHP »

Hi hapkidoyoka,

See Windows Services & Other Stuff\Other_Stuff\ResizeImageFast\ResizeImageFast.pb
Last edited by JHPJHP on Mon Dec 06, 2021 4:26 pm, edited 1 time in total.
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: StretchBlt with images?

Post by Joris »

It might also be worthwhile to take a look at these functions GrabImage and GrabDrawingImage.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
hapkidoyoka
New User
New User
Posts: 3
Joined: Sun Dec 05, 2021 1:29 am

Re: StretchBlt with images?

Post by hapkidoyoka »

Thank you JHPJHP, that works and is significantly faster.

Out of curiosity: any idea why it creates a seperate device context and bitmap to operate with, and then copy it to a PB image?
It seems that StartDrawing and ImageID should provide the necessary hDC and HBITMAP, does CreateImage not create a compatible bitmap?
There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.
Post Reply