This actually requires you to get the file name of the picture. Again its probably not the best way but it'll do it.
module Picture
def self.dump_picture(filename)
f = File.open(filename, 'r')
data = f.read
f.close
encrypted_string = Zlib::Deflate.deflate(data)
return encrypted_string
end
def self.load_picture(string)
data = Zlib::Inflate.inflate(string)
f = File.open("temp.png", 'w')
f.write(data)
f.close
bitmap = Bitmap.new("temp.png")
return bitmap
end
end
So to save it in a serialized file do this.
f = File.open("test.rxdata", 'wb')
picture = Picture.dump_picture("Graphics/Pictures/test.png")
Marshal.dump(picture, f)
f.close
And to load the picture
f = File.open("test.rxdata", 'rb')
bitmap = Picture.load_picture(Marshal.load(f))
f.close
EDIT:
And if its still not working, just dump the contents of "temp.png" without compressing it.
def self.dump_picture(filename)
f = File.open(filename, 'r')
data = f.read
f.close
return data
end
def self.load_picture(string)
f = File.open("temp.png", 'w')
f.write(string)
f.close
bitmap = Bitmap.new("temp.png")
return bitmap
end
I never had much success with opening an image, using "file.write(line)" and then trying to reopen it. The data gets all screwed up when you do this. You can even do this:
lines = IO.readlines("picture.png")
file = File.open('another_pic.png', 'wb')
lines.each {|line| file.write(line) }
file.close
Now that would appear to simply create another copy of the picture, but it doesn't. I suspect that it has something to do with newlines or special characters or something, I never looked too much into it...
One way I successfully Marshalled an image was a by use of Blizz's Bitmap2Code. I would compress and Marshal the string that created the bitmap, then when you load it just run an "eval" on it. This isn't really saving the actual image to file, obviously, just the method to create it, but I suppose you end up with the same result.
I don't recommend this at all, though, it is horribly inefficient and worthless to actually include any such type of non-sense in a game.
Try treating the file as binary. I.e. use 'rb' and 'wb' instead of 'r' and 'w'.
Also I like how the encryption you use is the DEFLATE compression XD