Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » save display window as image(png,jpg)

This thread is locked; no one can reply to it. rss feed Print
save display window as image(png,jpg)
Rialesto
Member #16,582
October 2016

hello everyone I have project with c and I must program paint's program and I want to save Allegro's window as image(png,jpg) so How can I save Allegro's window(display window) Like as png or jpg file in Allegro 5 ?
I search so much but I can not find anything useful :( :-/ any body know how I can save it ?
I wrote something like this but it does not work

#SelectExpand
1#include <stdio.h> 2#include <iostream> 3using namespace std; 4#include <allegro5/allegro.h> 5#include "allegro5/allegro_image.h" 6#include <allegro5/allegro_primitives.h> 7BITMAP *buffer; 8int main(int argc, char **argv){ 9 int width=640; 10 int height=480; 11 bool done=false; 12 int imageRad=20; 13 14 ALLEGRO_DISPLAY *display = NULL; 15 ALLEGRO_EVENT_QUEUE *event_queue=NULL; 16 ALLEGRO_BITMAP* image=NULL; 17 18 if(!al_init()) { 19 fprintf(stderr, "failed to initialize allegro!\n"); 20 return -1; 21 } 22 23 display = al_create_display(width, height); 24 if(!display) { 25 fprintf(stderr, "failed to create display!\n"); 26 return -1; 27 } 28 al_install_keyboard(); 29 al_init_primitives_addon(); 30 image=al_create_bitmap(imageRad*2,imageRad*2); 31 32 al_set_target_bitmap(image); 33 34 al_draw_filled_rectangle(imageRad,imageRad-9,imageRad+10,imageRad-7,al_map_rgb(255,0,0)); 35 al_draw_filled_rectangle(imageRad,imageRad+9,imageRad+10,imageRad+7,al_map_rgb(255,0,0)); 36 al_draw_filled_triangle(imageRad-12,imageRad-17,imageRad+12,imageRad,imageRad-12,imageRad+17,al_map_rgb(0,255,0)); 37 al_draw_filled_rectangle(imageRad-12,imageRad-2,imageRad+15,imageRad+2,al_map_rgb(0,0,255)); 38 39 al_set_target_bitmap(al_get_backbuffer(display)); 40 41 event_queue=al_create_event_queue(); 42 al_register_event_source(event_queue,al_get_keyboard_event_source()); 43 while(!done){ 44 ALLEGRO_EVENT ev; 45 al_wait_for_event(event_queue,&ev); 46 if(ev.type==ALLEGRO_EVENT_KEY_DOWN){ 47 switch(ev.keyboard.keycode){ 48 case ALLEGRO_KEY_ESCAPE: 49 done=true; 50 break; 51 } 52 } 53 al_draw_bitmap(image,width/2,height/2,0); 54 al_flip_display(); 55 } 56 al_save_bitmap("foto.jpg",image); 57 58 al_clear_to_color(al_map_rgb(0,0,0)); 59 al_destroy_bitmap(image); 60 al_destroy_display(display); 61 return 0; 62}

Edgar Reynaldo
Member #8,592
May 2007
avatar

You should put your code in <code>code goes here...</code> tags for easier reading. You can edit your post to use them.

First, what version of Allegro are you using?

Second, what isn't working? You should be checking the return value of al_save_bitmap as well as al_create_bitmap and see what they return. Does the bitmap save? Does it not show what you want? What happens in your program now?

Third, your call to al_clear_to_color should be inside your drawing loop before you draw your bitmap image so that the backbuffer gets cleared before you draw.

Rialesto
Member #16,582
October 2016

Thanks for fast replying .. I am using Allegro 5 and yeah I khnow that I should be checking return value of al_save_bitmap and of al_create_bitmap as well ... I solved it I made small mistakes in code ... So Thank you very much

Go to: