2016년 8월 10일 수요일

C# 레지스트리에 설정파일 저장 변경 로드 테스트

먼저 Regedit 에서

HKEY_CURRENT_USER\SOFTWARE\ZTest 로 키를 생성

(키 생성하면 폴더가 생성됨)

안에 DogName 와 Age 를 스트링으로 생성한다.


void Load()
{
String Keypath = "Software\\ZTest";
RegistryKey regkey = Registry.CurrentUser.OpenSubKey(Keypath);
if (regkey != null)
{
Path = regkey.Name;
item1 = (string)regkey.GetValue("DogName");
item2 = (string)regkey.GetValue("Age");
}
}


void Save()
{
String Keypath = "Software\\ZTest";
RegistryKey regkey = Registry.CurrentUser.OpenSubKey(Keypath);
if (regkey != null)
{
Path = regkey.Name;
regkey.SetValue("DogName","Not mimi");
regkey.SetValue("Age","not 12");
}
}

2016년 8월 4일 목요일

C# for loop with no int i

Decimal.Round() -> decimal point 2




string 보너스

doublet = 10234.234;
Console.WriteLine(t.ToString("#,#.###"));


소수점에 대한 글

http://www.mkexdev.net/Article/Content.aspx?parentCategoryID=2&categoryID=9&ID=98

2016년 8월 3일 수요일

c# PictureBox Real Position on Not Scaled Image

Point controlRelative = pictureBox1.PointToClient(MousePosition);
Size imageSize = pictureBox1.Image.Size;
Size boxSize = pictureBox1.Size;

Point imagePosition = new Point((imageSize.Width / boxSize.Width) * controlRelative.X,
(imageSize.Height / boxSize.Height) * controlRelative.Y);

2016년 8월 2일 화요일

c# Task Func Return Value Net4.5 Style




NET 4.5

The recommended way in .NET 4.5 is to use Task.FromResult, Task.Run or Task.Factory.StartNew:

--FromResult:

public async Task DoWork()
{
int res = await Task.FromResult(GetSum(4, 5));
}

private int GetSum(int a, int b)
{
return a + b;
}


Please check out Stefan’s comments on the usage of FromResult in the comments section below the post.

--Task.Run:


public async Task DoWork()
{
Func function = new Func(() => GetSum(4, 5));
int res = await Task.Run(function);
}

private int GetSum(int a, int b)
{
return a + b;
}


--Task.Factory.StartNew:


public async Task DoWork()
{
Func function = new Func(() => GetSum(4, 5));
int res = await Task.Factory.StartNew(function);
}

private int GetSum(int a, int b)
{
return a + b;
}


=========================== My Code ===================
Action actone;
Func funcone;
Action acttwo;


private async void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
int posX = e.X;
int posY = e.Y;

actone = ActMethod;
funcone = ActMethod3;
acttwo = ActMethod2;

await Task.Factory.StartNew(() => actone());
await Task.Factory.StartNew(actone);
await Task.Factory.StartNew(() => acttwo(posX, posY));

string reu = await Task.FromResult(funcone(posX, posY));

string result = await Task.Run(() => funcone(posX, posY));
}

void ActMethod()
{
Console.WriteLine("done");
}

void ActMethod2(int posX, int posY)
{
Console.WriteLine("done");
}

string ActMethod3(int posX, int posY)
{
Console.WriteLine("done");
return "func done";
}

c# 비동기 Task.Factory.StartNew 사용

1. 메소드 만듬

2. 매소드를 가지고 Action 만듬

3. 테스크에 Action 집어넌다.

= 이건 Method 에 Input Parameter 가 있을때

Action acttwo;

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
int posX = e.X;
int posY = e.Y;


acttwo = ActMethod2;
Task.Factory.StartNew(() => acttwo(posX,posY));
}

void ActMethod2(int posX, int posY)
{
Color inten = bitmp.GetPixel(posX, posY);
byte bt1 = inten.B;
for (int i = posX; i < posX + 200; i++) { linelist.Add(bitmp.GetPixel(i, posY).R); } Console.WriteLine("done"); } = 이건 Input Parameter 가 없을때 Action actone; private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { int posX = e.X; int posY = e.Y; actone = ActMethod; Task.Factory.StartNew(actone); // 이거나 Task.Factory.StartNew(() => actone()); // 이거 쓴다
}

void ActMethod()
{

Console.WriteLine("done");
}

Fast Method of Bitmap To GrayScale



public static Bitmap MakeGrayscale3(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);

//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);

//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});

//create some image attributes
ImageAttributes attributes = new ImageAttributes();

//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);

//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

//dispose the Graphics object
g.Dispose();
return newBitmap;
}

c# Wait 관련

1. 테스크를 병렬로 사용한다
Task.WaitAll(
Task.Factory.StartNew(()=>DoSomething()),
Task.Factory.StartNew(()=>Thread.Sleep(200))
);

2.스톱워치 사용 : 매우 정확함. 스톱워치 시작, 작업, 대기 ->
private void Wait(double milliseconds)
{
long initialTick = stopwatch.ElapsedTicks;
long initialElapsed = stopwatch.ElapsedMilliseconds;
double desiredTicks = milliseconds / 1000.0 * Stopwatch.Frequency;
double finalTick = initialTick + desiredTicks;
while (stopwatch.ElapsedTicks < finalTick)
{

}
}

private void btnRight_Click(object sender, RoutedEventArgs e)
{
currentPulseWidth = 0;

//The stopwatch will be used to precisely time calls to pulse the motor.
stopwatch = Stopwatch.StartNew();

GpioController controller = GpioController.GetDefault();

servoPin = controller.OpenPin(13);
servoPin.SetDriveMode(GpioPinDriveMode.Output);


// Here is how you would move the motor. Call a funciton like this when ever you are ready to move motor
MoveMotor(ForwardPulseWidth);

Wait(3000); //wait three seconds

//move it backwards
MoveMotor(BackwardPulseWidth);

}