LoginSignup
3
2

More than 5 years have passed since last update.

wxRubyでJPGなどの画像を表示

Posted at

wxRubyでFileDialogを使った選択した画像をそのまま表示するサンプル。

Wx::Frame.refresh
Wx::Frame.update
の両方を呼ばないとダメらしい。

open_image.rb
require 'wx'

class MyApp < Wx::App
  def on_init
    MyFrame.new.show
  end
end

class MyFrame < Wx::Frame
  def initialize
    super(nil, -1, "Image Test")
    menu_bar = Wx::MenuBar.new
    file_menu = Wx::Menu.new
    file_menu.append(Wx::ID_OPEN, '&Open', 'Open Image')
    menu_bar.append(file_menu, '&File')
    set_menu_bar(menu_bar)
    evt_menu(Wx::ID_OPEN) {|event| menu_open(event)}
  end
  def menu_open(event)
    dlg = Wx::FileDialog.new(self, "Choose a file", Dir.getwd(), "")
    if dlg.show_modal() == Wx::ID_OK
      file = dlg.get_paths[0]
      img = Wx::Image.new(file)
      @bitmap.destroy unless @bitmap.nil?
      @bitmap = Wx::StaticBitmap.new( self, -1, img.convert_to_bitmap, [0,0], [img.width, img.height ] )
      self.refresh
      self.update
    end
  end
end

MyApp.new.main_loop

※再描写がおかしい?

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2