[Closed] Updating a bitmap
Hi everyone,
I have an issue with saving and loading bitmaps with maxscript.
I’m grabing the viewport and saving it to a bitmap.
Later I load the saved bitmap and show it as a thumbnail in my UI.
I allow updating the thumbnail by grabing the viewport again and saving it over the existing file.
Now I want to refresh the thumbnail in the UI by reloading the bitmap from the file.
The problem is it still shows the first bitmap. If I open the saved file, I can see the new bitmap, but in the UI I see the old bitmap. The new bitmap is shown only after a Max restart.
Here is a small snipet that shows the problem:
(
global roll_Test
try (destroyDialog roll_Test) catch ()
rollout roll_Test "Test"
(
local thumbFileName
local BITMAP_SIZE = [200, 120]
-- User Interface
------------------------------------------
imgTag imtThumb "" width:BITMAP_SIZE.x height:BITMAP_SIZE.y align:#center style:#bmp_center transparent:red
button btnUpdate "Update"
-- Functions
------------------------------------------
fn grabThumbnail =
(
local grab = gw.getViewportDib()
local thumbnail = bitmap BITMAP_SIZE.x BITMAP_SIZE.y filename:thumbFileName
copy grab thumbnail
save thumbnail quiet:true
close thumbnail
local preview = openBitMap thumbFileName
imtThumb.bitmap = preview
)
fn openDialog =
(
createDialog roll_Test width:(BITMAP_SIZE.x + 20)
)
fn init =
(
thumbFileName = getDir #temp + "\\Test_Bitmap_1.jpg"
grabThumbnail()
)
fn done =
(
deleteFile thumbFileName
)
-- Event Handlers
------------------------------------------
on roll_Test open do init()
on roll_Test close do done()
on btnUpdate pressed do grabThumbnail()
on imtThumb dblclick do HiddenDosCommand ("\"" + thumbFileName + "\"")
)
roll_Test.openDialog()
)
Notice that when you hit the ‘Update’ button it will grab a new viewport snapshot and save it in the same file. Then it will attempt to update the thumbnail from the file (it must be from the file and not from the gw grab).
Double clicking the thumbnail will open the image in the default image viewer, where you can see the file was updated sucessfully. But somehow max ignores the update.
Any idea why or how to fix it?
For a quick fix try this:
fn grabThumbnail =
(
local grab = gw.getViewportDib()
local thumbnail = bitmap BITMAP_SIZE.x BITMAP_SIZE.y filename:thumbFileName
copy grab thumbnail
save thumbnail quiet:true
close thumbnail
imtThumb.bitmap = ()
freescenebitmaps()
gc light:on
local preview = openBitMap thumbFileName
imtThumb.bitmap = preview
)
In earlier Max versions (probably <2014) this “fix” shouldn’t be needed.
do it simple:
fn grabThumbnail =
(
local grab = gw.getViewportDib()
local thumbnail = bitmap BITMAP_SIZE.x BITMAP_SIZE.y filename:thumbFileName
copy grab thumbnail
save thumbnail quiet:true
close thumbnail
imtThumb.bitmap = thumbnail
)
Hey Denis,
Thanks for the reply, however as I said, I can’t just use the bitmap from the capture.
The snipet code I posted was just to ilustrate the problem with a small code, but my scenario is a bit more complex. I switch between lots of thumbnail, that are loaded from different files. The problem happens when I try to load a file that got overritten after it was loaded, not necesserily imidiatly after.
I guess I could create my own bitmap caching system to avoid this issue, but my question is how can we fix it without saving in memory every bitmap.
ok. i had to read it more carefull. my bad.
so. openbitmap searches first if a specified file bitmap was already opened. if it was the system uses the opened one.
but you have to force create new bitmap instead of using the current (recently open) . it’s not about update or redraw
i need to think a little how to do it. the first idea that comes is to use new bitmaptexture
freescenebitmaps was first that came to mind but i forgot gc…
all early versions need it as well.
Max 2011-2012 do not need any fix
Max 2014-2015-2016 only need:
imtThumb.bitmap = ()
gc light:on
But I like to keep freescenebitmaps() there, just in case.
it depends probably on a scope where you opened previously the bitmap.
i’ve looked in my old archive (definitely < 2014) . i used freescenebitmaps together with gc to reload bitmaps … it should be a reason.
I also use freescenebitmaps() with gc light:on in early versions, but for this specific task it seems it is not required. It won’t hurt to keep it there though.
I don’t remember what I use it for in earlier versions, but it might be related to materials/textures or when you need to update the preview image in the open image dialog box when you are working with the same file and overwrite it. I think in this case the last preview does not want to go away so easily sometimes. But I really don’t remember right now.
Thanks guys,
I’ll have to wait until tomorrow to test your solution but I’m sure it will solve the problem. I’ll write again if it doesn’t.