Simple Bitblt_

Just starting out? Need help? Post your questions and find answers here.
Garfield9992003
User
User
Posts: 15
Joined: Tue Apr 26, 2005 8:13 am

Simple Bitblt_

Post by Garfield9992003 »

Why are the 2 jpg´s from the code different?
They should be identical.
Its only a sample code from my project.

Code: Select all

  hBMP.l=CreateImage(0, 200, 400) 
  dcv.l=StartDrawing(ImageOutput()) 
  Box(50,50,100,300,RGB(255,255,255)) 
  StopDrawing() 
  
  MemDC.l = CreateCompatibleDC_(dcv) 
  BitmapID.l = CreateImage(1,200,400) 
  SelectObject_(MemDC, BitmapID) 
  BitBlt_(MemDC, 0, 0, 200, 400, dcv , 0, 0, #SRCCOPY) 
  DeleteDC_(MemDC) 
  
  UseJPEGImageEncoder() 
  SaveImage(0, "0.jpg",#PB_ImagePlugin_JPEG) 
  SaveImage(1, "1.jpg",#PB_ImagePlugin_JPEG)
Regards

Frank
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Hiya,

you just need to reorder the statements a little as the original

Code: Select all

BitmapID.l = CreateImage(1,200,400) 
line was destroying the original device context. Also had to move the StopDrawing() command.

Code: Select all

BitmapID.l = CreateImage(1,200,400) 
hBMP.l=CreateImage(0, 200, 400) 
dcv.l=StartDrawing(ImageOutput()) 
Box(50,50,100,300,RGB(255,255,255)) 

MemDC.l = CreateCompatibleDC_(dcv) 
SelectObject_(MemDC, BitmapID) 
BitBlt_(MemDC, 0, 0, 200, 400, dcv , 0, 0, #SRCCOPY) 
DeleteDC_(MemDC) 
StopDrawing() 
 
UseJPEGImageEncoder() 
SaveImage(0, "0.jpg",#PB_ImagePlugin_JPEG) 
SaveImage(1, "1.jpg",#PB_ImagePlugin_JPEG)
Regards.
I may look like a mule, but I'm not a complete ass.
Garfield9992003
User
User
Posts: 15
Joined: Tue Apr 26, 2005 8:13 am

Post by Garfield9992003 »

Thanx a lot, it works fine!

But now, i´ve my second problem with image-handles :(

I want to copy 2 images into a new one:

Images 0 and 1 are given. How can i retrieve a handle on it?
Image 2 keeps black :(

What´s wrong?

Code: Select all

  id1=UseImage(0) ;image 200x200 pixel stored in image #1
  id2=UseImage(1) ;image 200x200 pixel stored in image #2
  img=CreateImage(2, 200, 200)
  handle=StartDrawing(ImageOutput()) 
  mem = CreateCompatibleDC_(handle)   
  SelectObject_(mem, img)
  BitBlt_(mem, 0,0, 200,200, id2, 0,0, #SRCCOPY)
  BitBlt_(mem, 30,30, 140,140, id1, 30,30, #SRCCOPY)
  DeleteDC_(mem)
  StopDrawing()
Regards

Frank
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

UseImage() does not return a device context as required by BitBlt. It actually returns a handle to the underlying bitmap itself.

I'll have a little tinker.
I may look like a mule, but I'm not a complete ass.
Garfield9992003
User
User
Posts: 15
Joined: Tue Apr 26, 2005 8:13 am

Post by Garfield9992003 »

Damn, i thought it :(

And how could i receive a dc from an image?

I had no results with GetDC.

Is there another way?


Thx.

Frank
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

The following seems to work okay, though there is probably a better way of doing this.

The problem with StartDrawing() StopDrawing() is that the hdc's are released automatically before we have time to pass them to the BitBlt function.

Try this.

Code: Select all

img0=UseImage(0) 
img1=UseImage(1) 
CreateImage(2, 200, 200) 
hdc2=StartDrawing(ImageOutput()) 
hdc0 = CreateCompatibleDC_(hdc2)    
hdc1 = CreateCompatibleDC_(hdc2)    

SelectObject_(hdc0, img0) 
SelectObject_(hdc1, img1)

BitBlt_(hdc2, 0,0, 200,200, hdc0, 0,0, #SRCCOPY) 
BitBlt_(hdc2, 30,30, 140,140, hdc1, 0,0, #SRCCOPY) 
StopDrawing()

DeleteDC_(hdc0) 
DeleteDC_(hdc1) 
I tested it with the following:

Code: Select all

img0=CreateImage(0, 200, 200) 
img1=CreateImage(1, 200, 200) 

UseImage(0) ;image 200x200 pixel stored in image #1 
id=StartDrawing(ImageOutput()) 
Box(0,0,200,200,RGB(255,255,255)) ; White box.
StopDrawing()
UseImage(1) ;image 200x200 pixel stored in image #2 
StartDrawing(ImageOutput()) 
Box(0,0,200,200,RGB(255,255,0)) ; Yellow box.
StopDrawing()


img0=UseImage(0) 
img1=UseImage(1) 
CreateImage(2, 200, 200) 
hdc2=StartDrawing(ImageOutput()) 
hdc0 = CreateCompatibleDC_(hdc2)    
hdc1 = CreateCompatibleDC_(hdc2)    

SelectObject_(hdc0, img0) 
SelectObject_(hdc1, img1)

BitBlt_(hdc2, 0,0, 200,200, hdc0, 0,0, #SRCCOPY) 
BitBlt_(hdc2, 30,30, 140,140, hdc1, 0,0, #SRCCOPY) 
StopDrawing()

DeleteDC_(hdc0) 
DeleteDC_(hdc1) 

UseJPEGImageEncoder() 
SaveImage(0, "0.jpg",#PB_ImagePlugin_JPEG) 
SaveImage(1, "1.jpg",#PB_ImagePlugin_JPEG)
SaveImage(2, "2.jpg",#PB_ImagePlugin_JPEG)
I may look like a mule, but I'm not a complete ass.
Garfield9992003
User
User
Posts: 15
Joined: Tue Apr 26, 2005 8:13 am

Post by Garfield9992003 »

Wow, it work great!

Thanks you a lot!


Frank
Post Reply