Notifications
Clear all

[Closed] Closing/Re-Saving a Bitmap?

Hi,

I’ve been struggling with this for a while… and I can’t seem to get this to work, at least consistently.

I am using trying to take a .TGA file and save as a .BMP to a temp directory so I can display that new bimap in a Dotnet Button, since .TGA files can’t be read in normally with DotNet.

I can get it all to work fine, but what I am trying to do, is basically create the temp bitmap on rollout open and display it, and then on rollout Closed, delete the temp bitmap and then when I open the Rollout again, it will re-create the temp bitmap again in the same location and display that.

Basically just a loop of creating/deleting the bitmap, since I don’t know any other way to go about working with .TGA files.

So the issue is that my Bitmap never gets closed in memory or something, because on trying to Re-Save the bitmap, I get the Image I/O Error.

Here is my bitmap creation code snippet…

-

-If File is a .TGA, make a temp bitmap so we can display i
 if (fileTypeBump == ".tga") then 
 (
																				
				  tempName = filenameFromPath   normalMapExtract.filename
																					
				   local tempBMPExt = getFilenameFile   normalMapExtract.filename
																			
					  --print (GetDir #image +"\\" + tempBMPExt + ".bmp") 
																			
																				
						  tempBmp = openBitmap normalMapExtract.filename
							 local iconBmp = bitmap 160 120
																				

						   for v = 0 to 119 do(

							   setPixels iconBmp [0,v] (getPixels tempBmp [(1-1)*160, v] 160)
								 )

							  --iconBmp.filename = (normalMapExtract.filename + "_temp")

								  iconBmp.filename = (GetDir #image +"\\" + tempBMPExt + ".bmp")

																				
									save iconBmp 1
									 close iconBmp 
									   close tempBmp
									  tempBmpGlobal = iconBmp

									  bitmap_dir_file = iconBmp.filename 
																	
										 --	bitmap_dir = GetDir #image +"/_temp.bmp"
																	
										  --print (filenameFromPath bitmap_dir_file)
																	
										  totalBitmaps +=1
	)
.... More code after this... 
																			 

Then when I closing the Rollout in the first place I am trying this…


	if tempBmpGlobal != undefined then
					(
						print (getBitmapInfo tempBmpGlobal as string)
						close tempBmpGlobal
						print tempBmpGlobal
						gc lite:true
					)
					
					local del_files =#();
					local tempDir = GetDir #image +"\\" 
					local files = getFiles (tempDir +"*.bmp");--delete files inside this folder before we can delete the folder
					

				--	print (usedMaps() as string)
					for fl in files do (
						append del_files fl;
					)
					--print (del_files as string)

					for s in del_files do 
						(
							
							 deleteFile s;
						
						)		

Any Ideas?? …

Thanks ALOT

3 Replies

As the MAXScript Help explains, there are two types of bitmap values – open for input and open for output (load-only and save-only)

If a bitmap is loaded from disk using openBitmap() or selectBitmap(), you CANNOT save back to it!

You have to create a temporary bitmap value of the same size using a bitmap() constructor which is by default “save-only”, copy the content of the “load-only” bitmap into it, assign a new .filename to the temporary bitmap and save it to disk, then call close() on it to close it.

Matthew,

another option is to open the tga as a max bitmap, and place it in the clipboard via the MXS setcliboardbitmap method. You can then use the dotnet clipboard.getimage() call to retrieve it. This is performing the same task but in memory, so there’s no need to save a temp file. I use this method in my hitchhiker control ( http://lonerobot.net/?page_id=582 ) for passing max-only image format to and from a dotnet assembly.

Awesome, THANK YOU Lone Robot, and also Bobo.

I setup the copy to clipboard method, and that solved my image scaling issues as well, YAY!

So far it seems to work perfect… so hopefully it stays that way…