Notifications
Clear all

[Closed] dotnet troubles

 lo1

there were a few things wrong with the code, this code seems to work for me. I am in quite a rush so can’t explain all the points now but please go over it and feel free to ask the reasons for whatever is not clear:

fn myCreateArrAssembly =
  (
  	source = ""
  	source += "using System;
"
  	source += "using System.Collections;
"
  	source += "using System.Drawing; 
"
  	source += "using System.Drawing.Drawing2D; 
"
  	source += "using System.Drawing.Imaging; 
"
  	source += "using System.Text; 
"
  	source += "namespace testTools
"
  	source += "{
"
  	source += "	public class testToolsClass
"
  	source += "	{
"
  	
  	source += "		public Bitmap testimage (Bitmap bmp)
"
  	source += "		{
"
  	
  	source += "			Bitmap result = new Bitmap(bmp);
"
  	source += "			Rectangle rect = new Rectangle(0, 0, result.Width, result.Height); 
 "
  	source += "			BitmapData bmpData = result.LockBits(rect, ImageLockMode.ReadWrite,result.PixelFormat); 
"
  					  
	source += "			IntPtr ptr = bmpData.Scan0; 
"  		
 
  	source += "			int bytes = Math.Abs(bmpData.Stride) * bmpData.Height; 
"
  	source += "			byte[] rgbValues = new byte[bytes]; 
"
	
  	source += "			System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); 
"
  
	source += "			int bytesPerPixel = Image.GetPixelFormatSize(result.PixelFormat) / 8; 
"
  	source += "			for (int counter = 0; counter < rgbValues.Length; counter+=bytesPerPixel) 
"
  	source += "				rgbValues[counter] = 255; 
"
  
  	source += "			System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); 
"
  
  	source += "			result.UnlockBits(bmpData); 
"
  		   
 
  	source += " 		return result; 
 "		
  	source += "		}
"
  	source += "	}
"
  	source += "}
"
  
  	csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
  	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
  
  	compilerParams.ReferencedAssemblies.AddRange #("System.dll")
  	compilerParams.ReferencedAssemblies.AddRange #("System.Drawing.dll")
  	compilerParams.ReferencedAssemblies.AddRange #("System.Windows.Forms.dll")
  
  	compilerParams.GenerateInMemory = on
  	compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
  	
  			if (compilerResults.Errors.Count > 0 ) then
  			(
  				errs = stringstream ""
  				for i = 0 to (compilerResults.Errors.Count-1) do
  				(
  					err = compilerResults.Errors.Item[i]
  					format "Error:% Line:% Column:% %
" err.ErrorNumber err.Line \											  
  														 err.Column err.ErrorText to:errs 
  				)
  				MessageBox (errs as string) title: "Errors encountered while compiling C# code"
  				format "%
" errs
  				return undefined
  			)
  	
  	assembly = compilerResults.CompiledAssembly
  	assembly.CreateInstance "testTools.testToolsClass"
  )
  
  global mytestClass = myCreateArrAssembly()
  clipboardObj = dotNetClass "System.Windows.Forms.Clipboard"
  bm = getlastrenderedImage()
  SetClipboardBitmap  bm
  imgObj = clipboardObj.GetImage()
  newimg = mytestClass.testimage imgObj
  clipboardObj.setImage(newimg)
  -- Get the image from the clipboard back into maxscript
  newBm = getClipboardBitmap()
  display newBm

p.s. – the blue channel is changed, not the red, because dotnet bitmaps are stored as BGRA not RGBA.

Thank you sooo much!!! it works beautifully. Just what I needed. You are awesome.

With this structure I am finally able to make the image manipulations that I had in mind.

 lo1

Sure thing

I also recommend this article on LoneRobot.net, dealing with color manipulation in dotnet via the colorMatrix class.

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

Will this work on any computer, or do I have to make a precompiled .dll when everything works?

 lo1

as far as I know it should work on any machine where a dll would work (any machine where a sufficiently high dotnet version is installed).

For small assemblies I usually leave it as dynamic compilation but if it’s something massive a precompiled dll is less of a hassle.

perfect. thanks.

Page 2 / 2