saving image

Blitz3D Forums/Blitz3D Programming/saving image

Oiduts Studios(Posted 2008) [#1]
How can i save a screenshot of the program window by pressing a key? i have made a paint program and i need to save and load images. When i load them i need to be able to edit them.please help
'
Graphics 1028,960
Global sx=50
Global sy=50
Global r=255
Global g=255
Global b=255
Global save=0
Global load=0
Global width=GraphicsWidth:height=GraphicsHeight
Global image = CreateImage(width, height)
Goto main

.size
Locate 0,60
sx=Input("size x")
sy=Input("size y")
Goto main

.i
Locate MouseX(),MouseY()
Global d1$=Input$("")
Goto main



.save
If save=1
Text 0,60,"Saving.."
SaveBuffer(ImageBuffer(image),"imageFile.bmp")
Delay 100
Cls
Text 0,60,"Done"
Delay 100
Cls
End If
Goto main

.load
If load=1
Text 0,60,"Loading.."
image = LoadImage("imagefile.bmp")
Delay 100
Text 0,80,"Done"
Delay 100
End If
Goto main



.main
While Not KeyDown(1)
If KeyHit(29) Then Goto size
If KeyHit (62) Then save=1:Goto save
If KeyHit (61) Then load=1:Goto load
If KeyHit(19)r=255:g=0:b=0
If KeyHit(34)r=0:g=255:b=0
If KeyHit(48)r=0:g=0:b=255
If KeyHit(17)r=255:g=255:b=255
If KeyHit(25)r=111:g=0:b=111
If KeyHit(21)r=255:g=255:b=0
If KeyHit(20)r=0:g=255:b=255
If KeyHit(24)r=249:g=81:b=0
If KeyHit(31)r=98:g=98:b=0
If KeyHit(210) Goto i
If MouseDown(1)
Color r,g,b
Plot MouseX(),MouseY()
Plot MouseX()+1,MouseY()
Plot MouseX(),MouseY()+1
EndIf
If MouseDown(2)
Color 0,0,0
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
EndIf
If KeyHit(211)
Cls
EndIf
If MouseDown(3)
Color r,g,b
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
EndIf
Text 0,20,"Keys are S,R,G,B,T,Y,O,P,NUM=1,2,3,0,END"
Wend


_33(Posted 2008) [#2]
SaveBuffer FrontBuffer(),fn$

fn$ contains the filename of the screen capture to save onto.

EDIT: Oh, looking at your code, it seems that your logic with the load=1, save=1 and all those gotos is rather troublesome. I'd suggest handling this otherwise. Also what is this odd resolution: Graphics 1028,960 ??? Good luck!


Gabriel(Posted 2008) [#3]
You appear to be creating an image which is 0 pixels wide and 0 pixels tall, as GraphicsHeight and GraphicsWidth are undefined. If you meant to use the inbuilt functions, you have to use parentheses to get return values.

EG, this:

Global width=GraphicsWidth:height=GraphicsHeight


needs to be this :

Global width=GraphicsWidth():height=GraphicsHeight()



Oiduts Studios(Posted 2008) [#4]
my new code


Graphics 1028,960
Global sx=50
Global sy=50
Global r=255
Global g=255
Global b=255
Global save=0
Global load=0
Global width=GraphicsWidth():height=GraphicsHeight()
Global image = CreateImage(width, height)
Goto main

.size
Locate 0,60
sx=Input("size x ")
sy=Input("size y ")
Goto main

.colorsel
Locate 0,60
r=Input("red ")
g=Input("green ")
b=Input("blue ")
Goto main

.i
Locate MouseX(),MouseY()
Global d1$=Input$("")
Goto main



.save
Text 0,60,"Saving.."
SaveBuffer(ImageBuffer(image),"imageFile.bmp")
Delay 100
Text 0,60,"Done"
Delay 100
Goto main

.load
Text 0,60,"Loading.."
imagel$ = Input$("")
image = LoadImage(""+imagel$+"")
Text 0,80,"Done"
Delay 100
Goto main



.main
While Not KeyDown(1)
If KeyDown(46) Then Goto Colorsel
If KeyDown(56) Then r=0:g=0:b=0
If KeyDown(59) Then
Color r,g,b
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
EndIf
If KeyHit(29) Then Goto size
If KeyHit(62) Then Goto save
If KeyHit(61) Then Goto load
If KeyHit(19)r=255:g=0:b=0
If KeyHit(34)r=0:g=255:b=0
If KeyHit(48)r=0:g=0:b=255
If KeyHit(17)r=255:g=255:b=255
If KeyHit(25)r=111:g=0:b=111
If KeyHit(21)r=255:g=255:b=0
If KeyHit(20)r=0:g=255:b=255
If KeyHit(24)r=249:g=81:b=0
If KeyHit(31)r=98:g=98:b=0
If KeyHit(210) Goto i
If MouseDown(1)
Color r,g,b
Plot MouseX(),MouseY()
Plot MouseX()+1,MouseY()
Plot MouseX(),MouseY()+1
Plot MouseX()-1,MouseY()
Plot MouseX(),MouseY()-1
EndIf
If MouseDown(2)
Color 0,0,0
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
EndIf
If KeyHit(211)
Cls
EndIf
If MouseDown(3)
Color r,g,b
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
EndIf
Color 0,0,0
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Color 255,255,255
Text 0,20,"Keys are S,R,G,B,T,Y,O,P,END,INSERT,F1,F4,F5,DELETE,Left CONTROL,left ALT,C"
Wend

but, when i save it, it doesnt save a file..
And i need to somhow be able to load and image.
also how can i make the background white instead of black?


Snarkbait(Posted 2008) [#5]
I'm not going to look at your code until you stop using 'goto'


Oiduts Studios(Posted 2008) [#6]
i dont know how to not use goto


Ked(Posted 2008) [#7]
Functions are an example.


Oiduts Studios(Posted 2008) [#8]
Graphics 1028,960
SaveBuffer FrontBuffer(),fn$
Global sx=50
Global sy=50
Global r=255
Global g=255
Global b=255
Global save=0
Global load=0
Global width=GraphicsWidth():height=GraphicsHeight()
Global image = CreateImage(width, height)
Goto main

.size
Locate 0,60
sx=Input("size x ")
sy=Input("size y ")
Goto main

.colorsel
Locate 0,60
r=Input("red ")
g=Input("green ")
b=Input("blue ")
Goto main

.i
Color r,b,g
Locate MouseX(),MouseY()
Global d1$=Input$("")
Goto main



.save
Text 0,60,"Saving.."
fn$ = Input$("")
SaveBuffer(ImageBuffer(image),""+fn$+"")
Delay 100
Text 0,60,"Done"
Delay 100
Goto main

.load
Cls
Text 0,60,"Loading.."
imagel$ = Input$("")
image = LoadImage(""+imagel$+"")
Text 0,80,"Done"
Delay 100
Goto main



.main
While Not KeyDown(1)
If KeyDown(46) Then Goto Colorsel
If KeyDown(56) Then r=0:g=0:b=0
If KeyDown(59) Then
Color r,g,b
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
EndIf
If KeyHit(29) Then Goto size
If KeyHit(62) Then Goto save
If KeyHit(61) Then Goto load
If KeyHit(19)r=255:g=0:b=0
If KeyHit(34)r=0:g=255:b=0
If KeyHit(48)r=0:g=0:b=255
If KeyHit(17)r=255:g=255:b=255
If KeyHit(25)r=111:g=0:b=111
If KeyHit(21)r=255:g=255:b=0
If KeyHit(20)r=0:g=255:b=255
If KeyHit(24)r=249:g=81:b=0
If KeyHit(31)r=98:g=98:b=0
If KeyHit(210) Goto i
If MouseDown(1)
Color r,g,b
Plot MouseX(),MouseY()
Plot MouseX()+1,MouseY()
Plot MouseX(),MouseY()+1
Plot MouseX()-1,MouseY()
Plot MouseX(),MouseY()-1
EndIf
If MouseDown(2)
Color 0,0,0
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
EndIf
If KeyHit(211)
Cls
EndIf
If MouseDown(3)
Color r,g,b
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
EndIf
Color 0,0,0
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Color 255,255,255
Text 0,20,"Keys are S,R,G,B,T,Y,O,P,END,INSERT,F1,F4,F5,DELETE,Left CONTROL,left ALT,C"
Wend

this is my new new new code but it still doesnt work


_33(Posted 2008) [#9]
The pixel resolution 1028 by 960 is imho unknown to this world. Try 1024 by 768, or 1280 by 960, or maybe smaller like 800 by 600.


Oiduts Studios(Posted 2008) [#10]
ok.. but it still is not working.. it wont save or load?
can somone look at my code


Neochrome(Posted 2008) [#11]
Thats some messy Code...

FAST but messy!

kinda reminds me back in the "JSR 00" ASM stuff!

bit retro that. ;) keep up the good work


Neochrome(Posted 2008) [#12]
OK worked it out!

in IMAGE, you didn't declear it as image%

the % means INTEGER a pointer to the IMAGE it self in memory!

looks like your attempting to write a "DELUXE PAINT 8" program ;)


Oiduts Studios(Posted 2008) [#13]
thanks every1

Ok im new (And 13 years old) sooooo can somone give me the whole code or that part of the code i cant get it in th right places please


mtnhome3d(Posted 2008) [#14]
well i inplemented functions for you cause i'm feeling nice and am bored.
so here it is
Graphics 1280,960
SetBuffer FrontBuffer()
Global sx=50
Global sy=50
Global r=255
Global g=255
Global b=255
Global save=0
Global load=0
Global width=GraphicsWidth():height=GraphicsHeight()
Global image = CreateImage(width, height)
Global d1$=Input$("")
 main()

Function size()
Locate 0,60
sx=Input("size x ")
sy=Input("size y ")
End Function

Function colorsel()
Locate 0,60
r=Input("red ")
g=Input("green ")
b=Input("blue ")
End Function

Function not_sure_what_to_call_this_one()
Color r,b,g
Locate MouseX(),MouseY()
;moved the global that was here to the top of the source.
End Function


Function save()
Text 0,60,"Saving.."
fn$ = Input$("")
SaveBuffer(ImageBuffer(image),""+fn$+"")
Delay 100
Text 0,60,"Done"
Delay 100
End Function

Function load()
Cls
Text 0,60,"Loading.."
imagel$ = Input$("")
image = LoadImage(""+imagel$+"")
Text 0,80,"Done"
Delay 100
End Function



Function main()
While Not KeyDown(1)
If KeyDown(46) Then  Colorsel()
If KeyDown(56) Then r=0:g=0:b=0
If KeyDown(59) Then 
Color r,g,b
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
Plot Rnd(width),Rnd(height)
EndIf
If KeyHit(29) Then size()
If KeyHit(62) Then save()
If KeyHit(61) Then load()
If KeyHit(19)r=255:g=0:b=0
If KeyHit(34)r=0:g=255:b=0
If KeyHit(48)r=0:g=0:b=255
If KeyHit(17)r=255:g=255:b=255
If KeyHit(25)r=111:g=0:b=111
If KeyHit(21)r=255:g=255:b=0
If KeyHit(20)r=0:g=255:b=255
If KeyHit(24)r=249:g=81:b=0
If KeyHit(31)r=98:g=98:b=0
If KeyHit(210) not_sure_what_to_call_this_one()
If MouseDown(1)
Color r,g,b
Plot MouseX(),MouseY()
Plot MouseX()+1,MouseY()
Plot MouseX(),MouseY()+1
Plot MouseX()-1,MouseY()
Plot MouseX(),MouseY()-1
EndIf
If MouseDown(2)
Color 0,0,0
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
EndIf
If KeyHit(211)
Cls
EndIf
If MouseDown(3)
Color r,g,b
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
Plot MouseX()+Rnd(sx),MouseY()+Rnd(sy)
EndIf
Color 0,0,0
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Plot -1+Rnd(100),35+Rnd(100)
Color 255,255,255
Text 0,20,"Keys are S,R,G,B,T,Y,O,P,END,INSERT,F1,F4,F5,DELETE,Left CONTROL,left ALT,C"
Wend
End Function




Oiduts Studios(Posted 2008) [#15]
ok i made the code as clear as i can now. It will still not save...or load... uh... i dont know what to doooo.... somone help me..




tonyg(Posted 2008) [#16]
 fn% = Input$("save as: ")

You seem to be taking text from the user and turning it into an integer. That can't be right!


Shifty Geezer(Posted 2008) [#17]
Having users type out filenames including folders is asking for trouble! You really want a file requester. I'm using this command...

save_file$ = WB3D_SaveFileRequest("Save File","", ".png image" +Chr(0)+ "*.png" +Chr(0), this_file$)

...which calls up a Windows file requester, and you can deck it out with which file types you want. Note that this is only the names of the files, not the actual format you need to save the file as!

I think it uses WinBlitz3D, but a quick search hasn't found me WinBlitz3D for you to grab. TBH I drop in and out of coding at such intervals that I get lost where I'm up too in developments. All I know is my code is working - I don't really know what libs I'm using!!!

I think a lot of other file-requester code options you have are part of full GUI libraries. Which to be honest you probably want to look into. I gave up on them and wrote my own because I find it harder to learn a million gadgets and function calls than write my own simplified interface! However a full-blown interface as you'd want with a graphics application is no small undertaking, so learning a GUI library would serve you well.

As an alternative option, if you're only keeping it simple to learn, you could just save the file in a standard folder with a standard name and copy/move it from there in Windows.

13 year olds writing programs is cool! the downside is things are way more complicated than back in the 8 bit era when more kids were programming and no-one had disks, let alone hard0drives with epic file structures! Creating games is far easier now. Creating applications that need graphical interfaces and mouse control and file access is a headache :(