Loading and drawing an image from a stream

BlitzMax Forums/BlitzMax Programming/Loading and drawing an image from a stream

Twinprogrammer(Posted 2012) [#1]
Hello ,

I've been recently trying to make a little piece of code that allows you to draw an image from a RequestFile(). I wrote it, and it works. But when I want to select another image , It ignores my request , and draws the selected image.

Graphics 640 , 480 , 0  

#SelectFile
  Global File:String = RequestFile ( "Load your PNG" , "PNG" )
  Global FileToRead:TStream = ReadFile ( File )
  Global Image:TImage = LoadImage ( File )


While Not KeyDown ( Key_Escape )
Cls
DrawImage Image , 0 , 0
If KeyDown ( Key_L ) Goto SelectFile
Flip
Wend



Twinprogrammer


col(Posted 2012) [#2]
Hiya,

Goto and Gosub are considered as bad programming etiquette nowadays and are easily replaced with Functions to make the code easier to read.

Your code runs once on my machine - Sony Vaio Win7 x64, and I don't get the file requester open when I press 'L'. When using LoadImage, you don't need to use ReadFile, unless youre using it for something else in your code of course :)

With this in mind, I've restructured it a little to take advantage of using a function and just for thought.. it's always a good idea to check the validity of variables especially when you're giving control to the user ( via a Requester ), that is to assume that it might not always be valid. Say for example someone presses 'L' then cancels the requester! If this isn't catered for then you could introduce some bugs and more likely a crash - in Release mode this could be an EAV.

As usual there are tons of different ways to achieve this and this is one simple clear cut way while keeping 'Global Image:Timage'....



Last edited 2012


Twinprogrammer(Posted 2012) [#3]
Thanks , Col, it works !

Twinprogrammer


col(Posted 2012) [#4]
Cool ;)