For my 2D painting application, I wouls like to save the screen to an image.
I have tried to use DrawingBuffer(), but I think I have an error, but I don't know how to resolv it :
Code: Select all
W = 1024
H = 768
UsePNGImageDecoder()
UsePNGImageEncoder()
file$ = OpenFileRequester("Open Image", "","png|*.png",0)
If file$ <> ""
LoadImage(0,file$)
OpenWindow(0, 0, 0, W,H, "Screen to Image", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
InitSprite()
OpenWindowedScreen( WindowID(0),0,0,W,H)
ClearScreen(RGB(200,200,200))
If StartDrawing(ScreenOutput())
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawAlphaImage(ImageID(0),0,0)
StopDrawing()
EndIf
; FlipBuffers()
W = ScreenWidth()
H = ScreenHeight()
Dim ColorR(W*H)
Dim ColorG(W*H)
Dim ColorB(W*H)
If StartDrawing(ScreenOutput())
DrawingMode(#PB_2DDrawing_AlphaBlend)
Buffer = DrawingBuffer()
If Buffer <> 0
pixelFormat = DrawingBufferPixelFormat()
lineLength = DrawingBufferPitch();Longueur d'une ligne
;If pixelFormat = #PB_PixelFormat_32Bits_BGR | #PB_PixelFormat_ReversedY
For i = 0 To W - 1
For j = 0 To H - 1
ColorR(i*j) = PeekA(Buffer + 4 * i + j * lineLength)
ColorG(i*j) = PeekA(Buffer + 4 * i + j * lineLength + 1)
ColorB(i*j) = PeekA(Buffer + 4 * i + j * lineLength + 2)
Next j
Next i
;EndIf
EndIf
StopDrawing()
EndIf
CreateImage(1,W,H,32,#PB_Image_Transparent)
If StartDrawing(ImageOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
Buffer = DrawingBuffer()
If Buffer <> 0
pixelFormat = DrawingBufferPixelFormat()
lineLength = DrawingBufferPitch()
If pixelFormat = #PB_PixelFormat_32Bits_BGR | #PB_PixelFormat_ReversedY
For i = 0 To W - 1
For j = 0 To H - 1
PokeA(Buffer + 4 * i + j * lineLength, ColorB(i*j))
PokeA(Buffer + 4 * i + j * lineLength + 1, ColorG(i*j))
PokeA(Buffer + 4 * i + j * lineLength + 2, ColorR(i*j))
Next j
Next i
EndIf
EndIf
StopDrawing()
EndIf
savefile$ = SaveFileRequester("Save image","", "png|*.png",0)
If savefile$ <> ""
If SaveImage(1,savefile$,#PB_ImagePlugin_PNG)
EndIf
EndIf
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
Thank you.


