Notifications
Clear all

[Closed] How to exchange image data via the dotNet clipboard WITH alpha channel?

This snippet will copy a bitmap from MAXScript to dotNet and back:

b = render()
-- send to dotNet...
setclipboardBitmap b
clipboardClass = dotNetClass "System.Windows.Forms.Clipboard"
theImage = clipboardClass.getImage()
-- ...and back
clipboardClass.setImage theImage
dnBitmap = getclipboardBitmap()
display dnBitmap

but unfortunately the alpha channel gets lost in between, so the result always has solid alpha channel.

Any ideas how to get this to work with the alpha channel?

Thanks very much in advance
– MartinB

7 Replies
 JHN

 (
 	b = render vfb:false
 	c = copy b
 
 	-- send to dotNet...
 	setclipboardBitmap c
 	
 	-- set clipboard stripped the alpha channel!!!
 	display c
 	
 	clipboardClass = dotNetClass "System.Windows.Forms.Clipboard"
 	theImage = clipboardClass.getImage()
 	-- ...and back
 	clipboardClass.setImage theImage
 	dnBitmap = getclipboardBitmap()
 	
 	-- copy to a bitmap that has alpha
 	newBm = copy dnBitmap c
 
 	fn pasteAlpha src1 p1 src2 p2 = color src2.r src2.g src2.b src1.a
 	pasteBitmap b newBm (box2 0 0 b.width b.height) [0,0] type:#function function:pasteAlpha	
 
 	display newBm
 	ok
 )
 

Cheers ;),
-Johan

Edit:

Nevermind…

Thanks! So you’re transferring the alpha channel from the original bitmap, using MAXScript. That’s of course a solution but the alpha channel will not reflect any changes made to the bitmap in dotNet. Of course I could process RGB and Alpha in two passes and then reassemble them, but I wonder whether there isn’t a dotNet native way of doing this?

Thanks again
– MartinB

there seems to be something funky going on with the Clipboard class. I guess it has something to do with the note on the MSDN documentation for the Image.FromFile class:

so the solution seems to be saving to a temporary PNG and loading in .NET and vice versa. the following code seems to do the trick:

-- temporary filename in the system's temp dir
  filename = sysinfo.tempDir + "tempRender.png"
  deleteFile filename -- delete if exists
  
  -- set PNG settings
  pngio.setType #true24
  pngio.setAlpha true
  pngio.setInterlaced false
  
  -- render and save to temporary tif
  b = render()
  b.filename = filename
  save b quiet:true
  close b
  
  -- load the tif as .NET bitmap
  ImageClass = dotnetclass "System.Drawing.Image"
  theImage = ImageClass.FromFile filename true
  
  -- resave as a png. must be a different filename
  modifiedFilename = sysinfo.tempDir + "tempRenderModified.png"
  ImageFormat = dotnetclass "System.Drawing.Imaging.ImageFormat"
  theImage.Save modifiedFilename ImageFormat.png
  
  -- open and display in max
  dnBitmap = openBitmap modifiedFilename
  display dnBitmap

not as clean as using the clipboard but it works!

a better solution would be a simple maxscript extension plugin with 2 new functions, one for maxscript bmp to .NET, and 1 for .NET to maxscript bmp, but then you’d have another external dependency to worry about…

Hi Martin,

It appears the problem is that setClipboardBitmap in max transforms the image into 24bbp image. So you go down to 8 bit per channel and lose the alpha channel.


(
	local b1 = render()
	
	setClipboardBitmap b1
	
	local b2 = getClipboardBitmap()
	
	display b2
	
	format "b1 has alpha: %
" (b1.hasAlpha)
	format "b2 has alpha: %
" (b2.hasAlpha)
)

So, I guess you could either use the method Gravey posted (using images saved to the disk) or ask Autodesk (or a plugin developer) really nicely

Cheers,
o

Hello chaps,

The clipboard always seemed to lose the transparency of a 32bit image, even using Third party image processors. The way I have used in the past is identical to Joel’s. It seemed plenty fast enough for when I wanted to swap TGAs from a Maxbitmap onto a picturebox control.

The other option might be to convert to a Base64 encoded string and bypass using the clipboard altogether.

I have a utility on LR.net to let you try this out, with the maxscript download

http://lonerobot.net/?p=314

Thanks Joel and Ofer!

I would guess that saving and loading a file is also slower than the clipboard, but I need to benchmark this. In the end, I need this to work as fast as possible.

Cheers!
– MartinB