Notifications
                
            
                Clear all
    
            
        [Closed] The problem with the dotNet open picture
May 13, 2012 6:54 am
                      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                        
                    May 13, 2012 6:54 am
                      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
)
May 13, 2012 6:54 am
                      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
)
                        
                        1 Reply
                    
                    May 13, 2012 6:54 am
                      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 ?
                        
                        1 Reply