Tag Archives: .NET

Convert Byte Array To Short Array C#

Convert Byte Array To Short Array C#.

/// <summary>
/// This Function Converting byte array to short Array
/// </summary>
/// <param name="source">Source byte array</param>
/// <returns>short Array</returns>
public short[] ConvertByteArrayToShortArray(byte[] source)
{
	short[] destination = new short[source.Length / 2];
	Buffer.BlockCopy(source, 0, destination, 0, destination.Length);
	return destination;
}

Calculate Gray (Mono) Color From RGBA Color

Calculate Gray (Mono) Color From RGBA Color.This function calculating gray mono color.Importance coefficients of RGBA for gray color:
Msdn Importance coefficients of Rgb

public class Color
{
	public float R { get; set; }
	public float G { get; set; }
	public float B { get; set; }
	public float A { get; set; }
}

/// <summary>
/// This function return Mono(Gray Color)
/// </summary>
/// <param name="rgb">RBGA color</param>
/// <returns>Return 4 byte float data</returns>
public float GetGrayColor(Color rgb)
{
     return (0.299f * rgb.R + 0.587f * rgb.G + 0.114f * rgb.B);
}

Call C++ Function From .Net(C#)

Call C++ Function From .Net(C#)

Example of call c++ function from .Net

CppExample.cpp
extern "C" {
	__declspec(dllexport) void CppFunction();
}

extern __declspec(dllexport) void CppFunction()
{
	//This Function can be called from .Net
}

CppExample.dll must be same location with CsClass.exe

CsClass.cs

class CsClass
{
	[DllImport(@"CppExample.dll", CallingConvention = CallingConvention.Cdecl)]
	public static extern void CppFunction();

	public static void Main()
	{
		CppFunction()
	}
}

Operator Overloading C#

Operator Overloading C#

Example is added two ColorRgb.

private class ColorRgb
{
	public ColorRgb() { }
	public ColorRgb(float r, float g, float b)
	{
		R = r;
		G = g;
		B = b;
	}

	public float R { get; set; }
	public float G { get; set; }
	public float B { get; set; }

	public static ColorRgb operator +(ColorRgb colorRgb1, ColorRgb colorRgb2)
	{
		return new ColorRgb(colorRgb1.R + colorRgb2.R, colorRgb1.G + colorRgb2.G, colorRgb1.B + colorRgb2.B);
	}

	public override string ToString()
	{
		return string.Format("R:{0} G:{1} B:{2}", R, G, B);
	}
}

private void OperatorOverloadingForm_Load(object sender, EventArgs e)
{
	ColorRgb color1 = new ColorRgb(10,20,30);
	ColorRgb color2 = new ColorRgb(3, 5, 7);
	ColorRgb result = color1 + color2;
	MessageBox.Show(result.ToString());
}

Obsolete to Function or Class in C#

Obsolete to Function or Class in C#
This Properties is used for showing to new method or class.

[System.Obsolete("Use NewClass Instead of OldClass")]
public class OldClass
{
    public void Method() { }
}
public class NewClass
{
	//If param2 is setted true .Visual Studio will show Error.
	//If param2 is setted false.Visual Studio will show Warning
	
    [System.Obsolete("Use NewMethod", true)]
    public void OldMethod()  { }
    public void NewMethod()  { }
}

Max SDK Save (Write) Image (jpg,png,dds etc.) File with C#

Max SDK Save (Write) Image (jpg,png,dds etc.) File with C#

This function writing Image File.Max SDK.Autodesk.Max.dll must be added to references.

/// <summary>
/// This function writing bitmap File
/// </summary>
/// <param name="bitmap">Bitmap properties</param>
/// <param name="global">Your Global</param>
/// <param name="outputPath">File writing location</param>
public void SaveBitmap(IBitmap bitmap,IGlobal global,string outputPath)
{
	IBitmapInfo bitmapInfo = global.BitmapInfo.Create();
	bitmapInfo.SetName(outputPath);
	
	//Image file format can set using this function
	/*bitmapInfo.SetType(17);
	bitmapInfo.SetDevice("DDS Image File"); */
	
	bitmap.OpenOutput(bitmapInfo);
	bitmap.Write(bitmapInfo, 0);
	bitmap.Close(bitmapInfo, 0);
	global.TheManager.DelBitmap(bitmap);
}

Max SDK Load Bitmap(jpg,png,dds etc.) with .NET(C#)

Max SDK Load Images(jpg,png,dds etc.) with .NET(C#)

This Function is loading textures and showing image height.

/// <summary>
/// This Function is loading textures.
/// </summary>
/// <param name="path">File Location</param>
/// <param name="global">Your IGlobal param.</param>
public void LoadBitmap(IGlobal global,string path)
{
	IBitmap bitmap = global.TheManager.NewBitmap;
	IBitmapInfo bitmapInfo = global.BitmapInfo.Create();
	bitmapInfo.SetName(path);
	global.TheManager.LoadInto(bitmapInfo, bitmap, false);

	MessageBox.Show("Height of image:" + bitmap.Height.ToString());
}