Working with Images and Screen Graphics

Just starting out? Need help? Post your questions and find answers here.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Working with Images and Screen Graphics

Post by chris319 »

What's the best way to do this?

I am working with an image which is 1920 x 1080. At the same time, I need to be able to see this image on a monitor of lower resolution: 1365x768. I can draw directly to the image, but can I copy the image directly to the screen, and will a straight copy scale the image to the screen resolution or do I need to resize the image to the lower screen resolution? Do I use ScreenOutput() for this? At the moment I am drawing into a windowed screen. Ultimately the image in memorey will be saved to a .bmp file at 1920 x 1280.

OS is Windows 7.

Thanks in advance.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Working with Images and Screen Graphics

Post by netmaestro »

Good choice, the windowed screen. With a windowed screen set to AutoStretch, you can forget about the fiddly details of scaling. Here's a sample where I create an image 1920x1080, the native size of your image, and I display it in a windowed screen 320x240. Because the AutoStretch flag for the windowedscreen is set, the image is automatically scaled for me. Quality is excellent and so is speed. It's a resizable window, so play with the size a bit to see the results. Using a sprite as an alternative may provide better performance, not sure.

Code: Select all

InitSprite()
OpenWindow(0,0,0,320,240,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_SizeGadget)
OpenWindowedScreen(WindowID(0),0,0,1920,1080,1,0,0,#PB_Screen_WaitSynchronization)
CreateImage(0,1920,1080)
StartDrawing(ImageOutput(0))
  Box(0,0,1920,1080,RGB(200,200,200))
  Box(0,0,40,40,#Red)
  Box(1880,0,40,40,#Red)
  Box(1880,1040,40,40,#Red)
  Box(0,1040,40,40,#Red)
StopDrawing()

Repeat
  Repeat
    ev=WindowEvent()
    If ev=#PB_Event_CloseWindow
      End
    EndIf
  Until ev=0
  StartDrawing(ScreenOutput())
    DrawImage(ImageID(0),0,0)
  StopDrawing()
  FlipBuffers()
ForEver
BERESHEIT
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Working with Images and Screen Graphics

Post by chris319 »

Thanks for the advice. I will try it forthwith.

Fortunately, for my immediate needs, speed and performance aren't really a consideration.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Working with Images and Screen Graphics

Post by chris319 »

Your code works. My code fails when I try to draw to the screen.

Code: Select all

StartDrawing(ScreenOutput())
 
The above line gives "[ERROR] The specified output is NULL (0 value)
heaven6502
User
User
Posts: 20
Joined: Mon Jan 26, 2009 1:43 pm

Re: Working with Images and Screen Graphics

Post by heaven6502 »

I was struggling yesterday with similar problem...

try InitSprite() before opening screen...

InitSprite()

CreateImage(0, 800, 480)
OpenWindow(0,0,0,800,480,"Fractal Lines",#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,800,480)
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Working with Images and Screen Graphics

Post by chris319 »

I don't know exactly what I did or why it works, but I finally got it to run without errors.

Thanks again.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Working with Images and Screen Graphics

Post by chris319 »

Code: Select all

With a windowed screen set to AutoStretch, you can forget about the fiddly details of scaling. Here's a sample where I create an image 1920x1080, the native size of your image, and I display it in a windowed screen 320x240. Because the AutoStretch flag for the windowedscreen is set, the image is automatically scaled for me.
No joy. It doesn't scale the image at all; it crops it.

If I use resize image, it resets the image handle and the program thinks the image is not initialized.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Working with Images and Screen Graphics

Post by netmaestro »

Remember you have to create the windowed screen at the native size of your image. This is going to be larger than the window. Refer to my sample code, it doesn't get cropped. All four corners are always visible.
BERESHEIT
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Working with Images and Screen Graphics

Post by chris319 »

Ah, OK, that did it. Thank you very much for your help.
User avatar
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: Working with Images and Screen Graphics

Post by graph100 »

very nice tips netmaestro ! Didn't though this way (openwindow < openwindowedscreen ) was a way to go !
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
Post Reply