Notifications
Clear all

[Closed] The problem with the dotNet open picture

Image img = Image.FromFile( @“d:\psb.jpg”);
this.button1.Image= img;

I use these code to display a image on a button . BUT . when running this Application. I Can not change “d:\psb.jpg ” file name . has a exception “this file is used by…”

But when use the Maxscript to display a picture on a max’s button… I can change this file and delete it…

How to solve this in dotNet ??

7 Replies
 lo1

Use this method:

fn nonLockingBitmapOpen fName =
(
	if not doesFileExist fName do return undefined
	local fs = dotnetObject "System.IO.FileStream" fName (dotnetClass "System.IO.FileMode").open
	local tempBmp = dotnetObject "System.Drawing.Bitmap" fs
	local resultBmp = tempBmp.Clone()
	tempBmp.Dispose();
	fs.Close();
	resultBmp
)

that’s a nice method using the filestream

mine is a variation of this, using gdi+ to paint the image into a new bitmap. Same theory as Rotem’s and goes something like this :

fn dotnetBitmap fName =
(
	if doesFileExist fName then
	(
	local img = (dotnetclass "System.Drawing.Image").fromfile fName 
	local retBmp = dotnetObject "System.Drawing.Bitmap" img.Width img.height
	local g = (dotnetclass "system.drawing.graphics").fromImage retBmp
	g.drawimage img 0 0 	
	img.Dispose();
	retBmp
	)
	else return undefined
)
 lo1

Does that method preserve the alpha channel?

1 Reply
(@lonerobot)
Joined: 10 months ago

Posts: 0

see attached:

I have this slove method:


Fn GetImage fileName = ( 
    bytes = (dotNetClass "System.IO.File").ReadAllBytes fileName 
    ms = (dotNetObject "System.IO.MemoryStream") bytes 
    bmp = (dotNetObject "System.Drawing.Image") ms 
)

But this MemoryStream need to Dispose ?

 lo1

I love your maxscript color theme!

1 Reply
(@lonerobot)
Joined: 10 months ago

Posts: 0

heh, here it is if anyone wants it