--- a/lib/image_size.rb
+++ b/lib/image_size.rb
@@ -44,16 +44,20 @@ class ImageSize
     @img_type   = nil
 
     if @img_data.is_a?(IO)
+      RUBY_VERSION >= '1.9.0' and @img_data.set_encoding('ascii-8bit')
       img_top = @img_data.read(1024)
       img_io = def_read_o(@img_data)
     elsif @img_data.is_a?(StringIO)
+      RUBY_VERSION >= '1.9.1' and @img_data.set_encoding('ascii-8bit')
       img_top = @img_data.read(1024)
       img_io = def_read_o(@img_data)
     elsif @img_data.is_a?(String)
-      img_top = @img_data[0, 1024]
 #      img_io = StringIO.open(@img_data){|sio| io = def_read_o(sio); io }
-      img_io = StringIO.open(@img_data)
+      img_io = StringIO.open(@img_data, 'r:ascii-8bit')
       img_io = def_read_o(img_io)
+      img_top = img_io.read(1024)
+      img_io.rewind
+      RUBY_VERSION > '1.8.999' && RUBY_VERSION < '1.9.1' and img_top.force_encoding('ascii-8bit')
     else
       raise "argument class error!! #{img_data.type}"
     end
@@ -115,6 +119,7 @@ class ImageSize
       self.seek(offset, 0) if offset
       ret = self.read(length)
       raise "cannot read!!" unless ret
+      RUBY_VERSION >= '1.9.0' and ret.force_encoding('ascii-8bit')
       ret
     end
     io
@@ -132,7 +137,7 @@ class ImageSize
     elsif img_top =~ /\/\* XPM \*\//                 then Type::XPM
     elsif img_top[0, 4] == "8BPS"                    then Type::PSD
     elsif img_top[1, 2] == "WS"                      then Type::SWF
-    elsif img_top[0] == 10                           then Type::PCX
+    elsif img_top[0, 1] == "\x0a"                    then Type::PCX
     else Type::OTHER
     end
   end
@@ -271,7 +276,7 @@ class ImageSize
       raise("This file is not SWF.")
     end
 
-    bit_length = Integer("0b#{header.unpack('@8B5')}")
+    bit_length = Integer("0b#{header.unpack('@8B5').join('')}")
     header << img_io.read_o(bit_length*4/8+1)
     str = header.unpack("@8B#{5+bit_length*4}")[0]
     last = 5
