bitMapEx error

Saj

Active Member
Licensed User
In order to prevent OutOfMemoryException errors I want to load large images as needed, rather than loading all into an ImageList array at the start. So i'm initializing a BitMapEx object as follows in App_Start:
bitmapMapEx.New1 (AppPath & "\blank.jpg")

Then I tried executing the following:
Sub LoadImage
s = imageFileArr(imageIndex).imagename 'string
bitmapMapEx.Value = (AppPath & "\" & s)
End Sub


The result is that I get an error message saying the Input string was not in the correct format.

Is my thinking correct - will it prevent the OutOfMemeoryException error? and what is causing the 'Input String' error?

Saj.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should change this line:
B4X:
[I]bitmapMapEx.Value = (AppPath & "\" & s)
[/I]

to:
B4X:
[I]bitmapMapEx.Value = AppPath & "\" & s
[/I]

Your approach seems to be correct assuming that not all images will be eventually required. You should take care not to reload the same image multiple times as it will increase the memory required.
 

Saj

Active Member
Licensed User
Thanks Erel,

Is it possible to discard the current image to free up memory before loading a new image - that way the same image can be loaded/unloaded many times?

The next line of code I have is as follows:
bitmapMapEx.Value = AppPath & "\" & s
MapWidth=bitmapMapEx.Width
MapHeight=bitmapMapEx.Height


which gives the following error: Object reference not set to an instance of an object. Can't figure out this error since bitmapMapEx.New1 has been executed in App_Start?
 
Last edited:

agraham

Expert
Licensed User
Longtime User
bitmapMapEx.Value = AppPath & "\" & s
This is not valid. You can't assign a string to the Value property, it only accepts a reference to an existing image. To create a BitmapEx from a path you need to use New1 again. It may not be necessary but to be safe I would Dispose of the old BitmapEx first.

B4X:
BitmapEx.Dispose
BitmapEx.New1(AppPath & "\" & s)
 

Saj

Active Member
Licensed User
Adding in BitmapEx.Dispose before BitmapEx.New1(AppPath & "\" & s) causes the following error on the New1 line:
Object reference not set to an instance of an object.
 

Saj

Active Member
Licensed User
That will be because it hasn't been Newed before.

I was already initialising bitMapEx in App_Start. The code wasn't failing on the Dispose method, but on the New1 after it. The code I have is as follows:

Sub App_Start
bitMapEx.New1(AppPath & "\blank.jpg")
End Sub

Sub LoadImage
s = mapIniFileArr(mapIndex).imagename
bitMapEx.Dispose
bitMapEx.New1(AppPath & "\" & s)
End Sub
 

Saj

Active Member
Licensed User
Ah, i see, it goes more than clearing the instance of the object, kills it too.

Just tried it on an old device with not much memory and its fixed the memory error.:sign0060:

Many Thanks
 
Top