[Closed] Saving Bitmaps in Custom Attributes
Does anybody know if you can save a bitmap into a file using custom attributes? I know there is a #bitmap parameter but that can only save a bitmap filename which max loads when the file loads. I’d like the bitmap to be distributed with the max file.
The only alternative I can think of is saving the bitmap as an array of colours in a #colortab. But that wastes quite a bit of space. Unless I do some sort of RLE encoding.
hi, I don’t know how to do this with custom attributes, but you can save the bitmap into an AppData string of the object. You just have to convert the binary bitmap data to a string and store it in the object with SetAppData. When reading out, convert back to binary. This might not be a very clean solution, but it works Saving the bitmap take a little time, but reading it back out is surprisingly fast…
here is a little script that stores the viewport to the selected object:
rollout ro_StorePic "StorePic"
(
button ui_Store "Store VP into Object"
button ui_Get "Get VP Image from Object"
local picID = 46333 --- AppDataID, can be any number
local _tempPicName = "c:\ empPic.jpg" --- Tempfile
on ui_Get pressed do
(
theObj = (getCurrentSelection())[1]
if theObj != undefined then
(
picFile = fOpen _tempPicName "wb" --- create temp binary file
if picFile != undefined then
(
theStr = getAppdata theObj picID --- get the String containing the imagedata from object
hexList = filterString theStr "&" --- filter the string to get a list of all hex-values
for f=1 to hexList.count do
(
theByte = 0
if hexList[f][1] == "+" then theByte = (trimLeft hexList[f] "+") as integer --- get positive hexValue
else theByte = -((trimLeft hexList[f] "-") as integer) --- get negative hexValue
writeByte picFile theByte --- write hexValue as Byte
)
fClose picFile
)
else messageBox "Error open tempfile!"
theBM = openBitmap _tempPicName --- open the temp Bitmap
display theBM
deleteFile _tempPicName --- delete tempfile
gc()
)
else messageBox "No object selected!"
)
on ui_Store pressed do
(
theObj = (getCurrentSelection())[1]
if theObj != undefined then
(
setWaitCursor()
vpPic = gw.getViewportDib() --- get current viewport
vpPic.filename = _tempPicName --- and save it to tempfile
save vpPic
close vpPic
fSize = getFileSize _tempPicName --- get the filesize in bytes
hexDataStr = "" --- Hex Datastring
bmFile = fopen _tempPicName "rb" --- open the bitmap as binary file
if bmFile != undefined then
(
for b=1 to fSize do
(
theByte = readByte bmFile --- read byte from file
hexStr = ""
if theByte >= 0 then hexStr = "&+0x" + (bit.IntAsHex theByte) --- convert Byte to hex value
else hexStr = "&-0x" + (bit.IntAsHex (abs theByte)) --- keep sign
hexDataStr += hexStr --- append hex value to datastring
)
fclose bmFile
)
else messageBox "Error open tempfile!"
deleteFile _tempPicName --- Delete tempfile
setAppData theObj picID hexDataStr --- Store Datastring into object
gc()
setArrowCursor()
)
else messageBox "No object selected!"
)
)
createDialog ro_StorePic
hope this helps you!