New topic: 

Example code to download image and display it.

<http://forums.realsoftware.com/viewtopic.php?t=34405>

         Page 1 of 1
   [ 1 post ]                 Previous topic | Next topic          Author  
Message        basil.bourque          Post subject: Example code to download 
image and display it.Posted: Fri Jun 25, 2010 9:32 pm                         
Joined: Thu Apr 15, 2010 10:41 pm
Posts: 38
Location: Seattle-area                I'm sharing some code that shows:
• Downloading an image file from a web server
• Saving that image to a disk file in a temporary location
• Opening that image file as a Picture object
• Display that image in a Canvas object in a window.

It would be more convenient to skip the temporary file, and instead download 
the image directly to memory. But apparently that is not possible with REAL 
Studio's built-in features. Plugins might be able to do that.

This took me a while to put these pieces together, so I thought this source 
code might be useful to others. Please post any criticism or improvements.

Two chunks of code, both on the Canvas object in a window: 
(1) 'Open' event handler
(2) 'Paint' event handler

Code: // Open event handler of Canvas object that displays the ad. © 2010 
Basil Bourque.
  // This source code may be used freely by anyone taking full responsibility 
for doing so.
  
  // Purpose: Load an advertisement image from web server, and display on 
screen in a Canvas.
  
  dim adFile as FolderItem = SpecialFolder.Temporary.Child("com_example_ad.gif")
  if( adFile <> nil) then
  dim url as String = "http://www.example.com/ad.gif";
  Dim socket As new HTTPSocket // this has the HTTP socket info (like 4D HTTP 
connection session ID
  dim timeoutInSeconds as integer = 5
  dim fileSaved as Boolean = socket.Get(url, adFile, timeoutInSeconds)
  if( not(fileSaved) ) then // For debugging.
  dim fileSystemErrorCode as Integer = Socket.LastErrorCode
  break
  end if
  socket.Close
  end if
  
  // If the ad file exists and is valid, display on screen in this Canvas 
object.
  if( adFile.Exists ) then
  if (adFile.Length > 0) then // If the file has contents.
  dim image as Picture = adFile.OpenAsPicture
  if ( image <> nil) then // If the image file successfully opened and loaded 
into memory
    self.adImage = image // Hand over this image to a property on this window.  
<-- GOAL
    // The Paint method of me (this Canvas object) will handle actually drawing 
that property's image onscreen.
  end if
  end if
  end if
  
  // fin

Code:  // 'Paint' event handler on Canvas object. © 2010 Basil Bourque.
  // This source code may be used freely by anyone taking full responsibility 
for doing so.
  
  // 'adImage' is a property on this window. Of type "Picture".
  g.drawpicture self.adImage,0,0 
//,self.width,self.height,0,0,lock.width,lock.height
  
  // Add a border
  g.forecolor = &cBBBBBB
  g.penheight = 1
  g.penwidth = 1
  g.drawrect 0,0,self.adImage.width,self.adImage.height
  
  // fin      
_________________
--Basil Bourque

(Using REAL Studio 2010 release 2.1 with REAL Server 2009r1.2 on Mac OS X 
10.6.2 & .3)  
                             Top            Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 1 post ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to