How to load an image in a HTML string

pitkin

Well-known member
I cannot seem to load an image in a HTML string.

html= html + "<IMG SRC=butontmin.tga width=30 height=30>" ;

img src seems to work find in a HTML file, but all I get if I build a string is a black square of the right size without any content.

I am using browser.LoadHTMLString rather than an HTML file because what is displayed changes with some other booleans in the script.

HTMLWindow.MakeImage has the same result.

The documentation for the minibrowser says this first parameter is to be a string, whatever that means. All the examples I can find use MakeImage with a KUID instead of a graphics file. Maybe this just cannot be done?

Thanks,

Paul
 
HI Paul,
Have you had a response to this post, or found a solution elsewhere? I seem to be having the same problem.
It might be more fruitful to raise this issue in the 'Content Creation Support' area of the forum.
Cheers

Richard
 
Here is the info from the TRS2004 helpfile for scripting:
Code:
[B]Images[/B]
To display a graphical bitmapped image, the <img> tag is used. Formats supported include .jpg, .bmp and .tga.
Use of an alpha channel is only supported with 32-bit .tga files. An image can be clickable and encapsulated in a <a href="URL..."> link.
To specify what to use for the image and how to display it, the following options are supported: 

Parameter  Value           Description
------------------------------------------------------------------
src        string        Localized filename of the image to display.  
kuid       KUID string   KUID of the asset to display the icon of.  
width      integer       Width in pixels to display the image in.  
height     integer       Height in pixels to display the image in.  
mouseover  string        Filename of image to display when the mouse cursor is above the image.  


When using the src parameter to specify the image file to use, do not use a full path. Use a localized file name instead.
Trainz will use the asset passed into either LoadHTMLString() or LoadHTMLFile() to determine where to find the image file.
A quick example that uses the mouseover option is: 


  <a href="live://setbutton">
    <img src="button.jpg" mouseover="button-hilite.jpg" width=250 height=100>
  </a>


As an alternative to img, the kuid option is provided. It allows the icon associated with the asset of the specified KUID to be displayed.
This parameter must be specified in the KUID string format. For example, the following HTML code will display the icon for the coal product
(which is a 64x64 .tga file specified in the coal product's config.txt file): 


  <img kuid="KUID:44179:60013" width=64 height=64><br>


Trainz relies on the KUID being valid as well as the corresponding asset having an icon associated with it to be able to display an image
when this option is used. The kuid parameter is obviously not standard HTML and is for use in TRS2004 only.
Peter
 
Paul,

I write html code in work and call images like this <IMG SRC="butontmin.tga" width=30 height=30> You need to have your quote marks around the image file

Regards, Andy
 
Thanks for that information.

It seems that my problem is not related to the HTML, but rather how to get the asset to be passed into the LoadHTMLString command.
I am trying to get a Driver Command, called 'Traffic Notice' to display a HTML message. The 'TrafficNoticeCustomCommand' Class keeps throwing up a 'mull reference' error.

I can probably get around the problem by just using text in the HTML string and not using images! :)
 
Have a look at Mick Berg's Battery Loco Manual Buffers asset <kuid:240029:100028>. It contains a script I wrote for him called bl_script.gs.

That scripts build and updates a browser using an html string with images. I recall I had some difficulties getting the alignment correct. The images change depending on the position of the loco's rotating buffers.

That may help.
 
ps. That version doesn't use the LoadHTMLString method but rather the SetElementProperty method. Earlier versions did use LoadHTMLString but I preferred the alternative method of "printing" to an HTML buffer.
 
Thanks Paul,

That is a really fascinating script with some really interesting ways to do things; easy to understand and so easy to follow.
And the end result on the loco is really spectacular!

I can think of a few applications for a similar technique.

Anyway, I don't think that is where my problem is. I think my problem is just that I don't properly understand how to use the GetAsset() function in a CustomCommand Class in a schedulecommand.gs file for a Driver Command! More study required.

Regards
Richard
 
The black rectangles mean that TRS understands your call but can't find the image. The minibrowser is very touchy about html syntax and you can have problems with escaping double quotes.

Assuming that the images you are trying to load are in the same asset as the script you just need to call:

Code:
browser.LoadHTMLString(GetAsset(),htmlstring);

If the images are in a subfolder within the asset you must include the subfolder name in the html string:

Code:
string image = "subfolder/image.tga";
string htmlstring = ....
+ ....
+ "[COLOR=#333333]<img src='" + image + "' width=30 height=30>[/COLOR]"
+ ....;
browser.LoadHTMLString(GetAsset(),htmlstring);

Declaring the image name as a constant or variable can help to deal with the quote mark issue.

If the images are in a different asset you substitute GetAsset() with a reference to the asset that contains the images. All of the images you are using must be in the same asset.
 
thanks for all the replies. I had to leave my coding efforts for awhile. I decided to build the html string inline since I wanted certain parts of the interface to appear or not depending on a boolean, and the html string could not recognize if statements.

Pitkin
 
Back
Top