Thread: Scaling window to height of screen....

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    3

    Question Scaling window to height of screen....

    HI..

    I am currently learning to code in C & have managed to code a super simple image viewer. It does what it is supposed to, but if I open any image that is larger than my screens resolution then parts of the open image get cut off. I also cannot resize images. Could someone please help me ?? I would like to code the window so that it if the image is larger than my resolution (1920x1080) it is automatically scaled down to either fit the height or width. I'd also like images to scale to the window size if possible also. I've attached my code below. Sorry if this is a silly question, but I'm just starting out and slowly learning. Many thanks....


    Code:
    #include <gtk/gtk.h>
    
    void destroy(void) {
      gtk_main_quit();
    }
    
    int main (int argc, char** argv) {
      GtkWidget* window;
      GtkWidget* image;
    
      gtk_init (&argc, &argv);
    
    
      window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
      image  = gtk_image_new_from_file(argv[1]);
    
      gtk_signal_connect(GTK_OBJECT (window), "destroy",
                 GTK_SIGNAL_FUNC (destroy), NULL);
    
      gtk_container_add(GTK_CONTAINER (window), image);
    
      gtk_widget_show_all(window);
    
      gtk_main();
    
      return 0;
    }
    Last edited by unknownvoid001; 3 Weeks Ago at 07:48 AM.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    631
    Did you try to add a call to gtk_window_maximize() between lines 14 and 15 (or at line 21, before showing the window)?

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    631
    Here:
    Code:
    // test.c
    // Tested with:
    //
    //  $ cc -O2 `pkg-config --cflags gtk+-3.0` \
    //       -o test test.c \
    //       `pkg-config --libs gtk+-3.0`
    //  $ ./test pic.jpg
    //
    #include <gtk/gtk.h>
    
    int main ( int argc, char **argv )
    {
      GtkWidget *window;
      GtkWidget *image;
    
      gtk_init ( &argc, &argv );
    
      window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
      image  = gtk_image_new_from_file ( argv[1] );
    
      // g_signal_connect() and G_OBJECT, not gtk_ and GTK_!
      g_signal_connect ( G_OBJECT ( window ), "destroy",
                         gtk_main_quit, NULL );
    
      gtk_container_add ( GTK_CONTAINER ( window ), image );
    
      // Here we go...
      gtk_window_set_decorated( GTK_WINDOW( window ), 0 ); // turn decoration off.
      gtk_window_maximize ( GTK_WINDOW( window ) );        // maximize window.
    
      gtk_widget_show_all ( window );
    
      gtk_main();
    
      return 0;
    }
    Last edited by flp1969; 3 Weeks Ago at 08:45 AM.

  4. #4
    Registered User
    Join Date
    Jan 2020
    Posts
    3
    Quote Originally Posted by flp1969 View Post
    Did you try to add a call to gtk_window_maximize() between lines 14 and 15 (or at line 21, before showing the window)?
    No I had not tried that, but it does work perfectly. Thank you very much for the code & for the help.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    631
    Quote Originally Posted by unknownvoid001 View Post
    No I had not tried that, but it does work perfectly. Thank you very much for the code & for the help.
    If you want the image to fill the entire window, you have some options: Get the maximized Window width and height and resize the image object accordingly... Notice if your image have a different aspect ratio than your window client area, you'll have to offset the image object (horizontally or vertically) to centralize it.

    But, I think you be better if using a library like SDL, instead of GTK+.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    631
    Here's a small example using SDL2:
    Code:
    // sdl.c
    //
    //   $ cc -O2 `pkg-config --cflags sdl2 SDL2_image` \
    //        -o sdl sdl.c \
    //        `pkg-config --libs sdl2 SDL2_image`
    //   
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <SDL.h>
    #include <SDL_image.h>
    
    int main( int argc, char *argv[] )
    {
      SDL_Window *wnd;
      SDL_Renderer *render;
      SDL_Surface *img;
      SDL_Texture *tex;
    
      if ( argc != 2 )
      {
        fputs( "Usage: sdl <imagefile>\n", stderr );
        return EXIT_FAILURE;
      }
    
      if ( access( argv[1], R_OK ) )
      {
        fprintf( stderr, "Error: Cannot open '%s'.\n", argv[1] );
        return EXIT_FAILURE;
      }
    
      if ( SDL_Init( SDL_INIT_VIDEO ) )
      {
        fputs( "SDL_Init() error.\n", stderr );
        return EXIT_FAILURE;
      }
    
      // Assuming your system supports 1920x1080!
      wnd = SDL_CreateWindow( "Hello", 0, 0, 1920, 1080, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS );
      if ( ! wnd )
      {
        fputs( "SDL_CreateWindow error.\n", stderr );
        SDL_Quit();
        return EXIT_FAILURE;
      }
    
      render = SDL_CreateRenderer( wnd, -1, SDL_RENDERER_ACCELERATED );
      if ( ! render )
      {
        fputs( "SDL_CreateRenderer erro.\n", stderr );
        SDL_DestroyWindow( wnd );
        SDL_Quit();
        return EXIT_FAILURE;
      }
    
      img = IMG_Load( argv[1] );
      if ( ! img )
      {
        fputs( "SDL_Load() error.\n", stderr );
        SDL_DestroyRenderer( render );
        SDL_DestroyWindow( wnd );
        SDL_Quit();
        return EXIT_FAILURE;
      }
    
      tex = SDL_CreateTextureFromSurface( render, img );
      SDL_FreeSurface( img );
      if ( ! tex )
      {
        fputs( "SDL_CreateTextureFromSurface() error.\n", stderr );
        SDL_DestroyRenderer( render );
        SDL_DestroyWindow( wnd );
        SDL_Quit();
        return EXIT_FAILURE;
      }
    
      SDL_ShowCursor(0);  // hide cursor.
    
      SDL_RenderClear( render );
      SDL_RenderCopy( render, tex, NULL, NULL );
      SDL_RenderPresent( render );
    
      while ( 1 )
      {
        SDL_Event ev;
    
        if ( SDL_PollEvent( &ev ) )
        {
          if ( ev.type == SDL_KEYDOWN )
            break;
        }
      }
    
      SDL_DestroyTexture( tex );
      SDL_DestroyRenderer( render );
      SDL_DestroyWindow( wnd );
      SDL_Quit();
    
      return EXIT_SUCCESS;
    }
    This code will NOT calculate the correct aspect for the image... And you'll need to install libsdl2-dev and libsdl2-image-dev packages.

  7. #7
    Registered User
    Join Date
    Jan 2020
    Posts
    3
    Quote Originally Posted by flp1969 View Post
    Here's a small example using SDL2:
    Code:
    // sdl.c
    //
    //   $ cc -O2 `pkg-config --cflags sdl2 SDL2_image` \
    //        -o sdl sdl.c \
    //        `pkg-config --libs sdl2 SDL2_image`
    //   
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <SDL.h>
    #include <SDL_image.h>
    
    int main( int argc, char *argv[] )
    {
      SDL_Window *wnd;
      SDL_Renderer *render;
      SDL_Surface *img;
      SDL_Texture *tex;
    
      if ( argc != 2 )
      {
        fputs( "Usage: sdl <imagefile>\n", stderr );
        return EXIT_FAILURE;
      }
    
      if ( access( argv[1], R_OK ) )
      {
        fprintf( stderr, "Error: Cannot open '%s'.\n", argv[1] );
        return EXIT_FAILURE;
      }
    
      if ( SDL_Init( SDL_INIT_VIDEO ) )
      {
        fputs( "SDL_Init() error.\n", stderr );
        return EXIT_FAILURE;
      }
    
      // Assuming your system supports 1920x1080!
      wnd = SDL_CreateWindow( "Hello", 0, 0, 1920, 1080, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS );
      if ( ! wnd )
      {
        fputs( "SDL_CreateWindow error.\n", stderr );
        SDL_Quit();
        return EXIT_FAILURE;
      }
    
      render = SDL_CreateRenderer( wnd, -1, SDL_RENDERER_ACCELERATED );
      if ( ! render )
      {
        fputs( "SDL_CreateRenderer erro.\n", stderr );
        SDL_DestroyWindow( wnd );
        SDL_Quit();
        return EXIT_FAILURE;
      }
    
      img = IMG_Load( argv[1] );
      if ( ! img )
      {
        fputs( "SDL_Load() error.\n", stderr );
        SDL_DestroyRenderer( render );
        SDL_DestroyWindow( wnd );
        SDL_Quit();
        return EXIT_FAILURE;
      }
    
      tex = SDL_CreateTextureFromSurface( render, img );
      SDL_FreeSurface( img );
      if ( ! tex )
      {
        fputs( "SDL_CreateTextureFromSurface() error.\n", stderr );
        SDL_DestroyRenderer( render );
        SDL_DestroyWindow( wnd );
        SDL_Quit();
        return EXIT_FAILURE;
      }
    
      SDL_ShowCursor(0);  // hide cursor.
    
      SDL_RenderClear( render );
      SDL_RenderCopy( render, tex, NULL, NULL );
      SDL_RenderPresent( render );
    
      while ( 1 )
      {
        SDL_Event ev;
    
        if ( SDL_PollEvent( &ev ) )
        {
          if ( ev.type == SDL_KEYDOWN )
            break;
        }
      }
    
      SDL_DestroyTexture( tex );
      SDL_DestroyRenderer( render );
      SDL_DestroyWindow( wnd );
      SDL_Quit();
    
      return EXIT_SUCCESS;
    }
    This code will NOT calculate the correct aspect for the image... And you'll need to install libsdl2-dev and libsdl2-image-dev packages.
    Thank you so very much for the comment & for the code. I'm going to give it a try asap....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get window handle or screen DC of logon screen
    By Elkvis in forum Windows Programming
    Replies: 1
    Last Post: 01-21-2014, 01:47 PM
  2. Screen Width and Height
    By Benji Wiebe in forum Windows Programming
    Replies: 1
    Last Post: 06-11-2011, 03:06 AM
  3. scaling controls on window resize or font change?
    By Viper187 in forum Windows Programming
    Replies: 2
    Last Post: 07-13-2009, 01:14 PM
  4. Docking a Window to screen
    By LIJI in forum Windows Programming
    Replies: 2
    Last Post: 06-02-2007, 01:26 PM
  5. Replies: 3
    Last Post: 07-16-2002, 02:55 PM

Tags for this Thread