public void RecevieStart()
{
while (true)
{
if (Server == null) continue;
if (Server.Connected == false) continue;
// 데이터 받을 변수
byte[] receiveData = ReceiveData(Server);
// 백그라운드워커는 한번에 하나씩만 실행 가능. 따라서 이벤트 핸들러를 새로 만들어 백그라운드워커 돌림
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(bgw1_RunWorkerCompleted);
worker.RunWorkerAsync(receiveData);
}
}
여기서 Server는 보내는 쪽과 연결되어 있는 소켓
Socket Server = null; 로 선언.
여기서 ReceiveData() 함수는 개인적으로 입맛에 맞게 만든 함수.
Server.ReceiveBegin( ......) 을 사용하는것과 크게 다를것이 없다.
여기서 백그라운드 워커는 받을 때 object sender 로 받는게 아니라 DoWorkEventArgs e.Argument 로 받는다.
2011년 12월 6일 화요일
2011년 6월 12일 일요일
2011년 6월 3일 금요일
[C#] Process Kill
PKILL = 프로세스 이름
ex) notepad
// 프로세스 킬
public void PKILL(string PKILL)
{
ProcessStartInfo cmd = new ProcessStartInfo();
Process process = new Process();
cmd.FileName = @"cmd";
cmd.WindowStyle = ProcessWindowStyle.Hidden;
cmd.CreateNoWindow = true;
cmd.UseShellExecute = false;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardInput = true;
cmd.RedirectStandardError = true;
process.EnableRaisingEvents = false;
process.StartInfo = cmd;
process.Start();
string cmd1 = "\"IMAGENAME eq ";
process.StandardInput.Write(@"C:\windows\system32\taskkill.exe /fi " + cmd1 + PKILL + "*\"" + Environment.NewLine);
process.StandardInput.Close();
process.Close();
}
ex) notepad
// 프로세스 킬
public void PKILL(string PKILL)
{
ProcessStartInfo cmd = new ProcessStartInfo();
Process process = new Process();
cmd.FileName = @"cmd";
cmd.WindowStyle = ProcessWindowStyle.Hidden;
cmd.CreateNoWindow = true;
cmd.UseShellExecute = false;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardInput = true;
cmd.RedirectStandardError = true;
process.EnableRaisingEvents = false;
process.StartInfo = cmd;
process.Start();
string cmd1 = "\"IMAGENAME eq ";
process.StandardInput.Write(@"C:\windows\system32\taskkill.exe /fi " + cmd1 + PKILL + "*\"" + Environment.NewLine);
process.StandardInput.Close();
process.Close();
}
라벨:
C#,
ProcessKill
[C#] Mouse Capture -> 화면캡쳐에 적용
입력 = int, int
출력 = Bitmap
using System.Runtime.InteropServices;
private Bitmap CaptureCursor(ref int x, ref int y)
{
Bitmap bmp;
IntPtr hicon;
Win32Stuff.CURSORINFO ci = new Win32Stuff.CURSORINFO();
Win32Stuff.ICONINFO icInfo;
ci.cbSize = Marshal.SizeOf(ci);
if (Win32Stuff.GetCursorInfo(out ci))
{
if (ci.flags == Win32Stuff.CURSOR_SHOWING)
{
hicon = Win32Stuff.CopyIcon(ci.hCursor);
if (Win32Stuff.GetIconInfo(hicon, out icInfo))
{
x = ci.ptScreenPos.x - ((int)icInfo.xHotspot);
y = ci.ptScreenPos.y - ((int)icInfo.yHotspot);
Icon ic = Icon.FromHandle(hicon);
bmp = ic.ToBitmap();
return bmp;
}
}
}
return null;
}
캡쳐된 화면에 그리는법
// 마우스 커서 캡쳐
int iCurX = 0;
int iCurY = 0;
Bitmap Cur = CaptureCursor(ref iCurX, ref iCurY);
Rectangle curRec = new Rectangle(iCurX, iCurY, Cur.Width, Cur.Height); // 마우스넣을 사각형
g.DrawImage(Cur, curRec); // 캡쳐한 화면(g)에 마우스 그려넣기
출력 = Bitmap
using System.Runtime.InteropServices;
private Bitmap CaptureCursor(ref int x, ref int y)
{
Bitmap bmp;
IntPtr hicon;
Win32Stuff.CURSORINFO ci = new Win32Stuff.CURSORINFO();
Win32Stuff.ICONINFO icInfo;
ci.cbSize = Marshal.SizeOf(ci);
if (Win32Stuff.GetCursorInfo(out ci))
{
if (ci.flags == Win32Stuff.CURSOR_SHOWING)
{
hicon = Win32Stuff.CopyIcon(ci.hCursor);
if (Win32Stuff.GetIconInfo(hicon, out icInfo))
{
x = ci.ptScreenPos.x - ((int)icInfo.xHotspot);
y = ci.ptScreenPos.y - ((int)icInfo.yHotspot);
Icon ic = Icon.FromHandle(hicon);
bmp = ic.ToBitmap();
return bmp;
}
}
}
return null;
}
캡쳐된 화면에 그리는법
// 마우스 커서 캡쳐
int iCurX = 0;
int iCurY = 0;
Bitmap Cur = CaptureCursor(ref iCurX, ref iCurY);
Rectangle curRec = new Rectangle(iCurX, iCurY, Cur.Width, Cur.Height); // 마우스넣을 사각형
g.DrawImage(Cur, curRec); // 캡쳐한 화면(g)에 마우스 그려넣기
[C#] Win32Stuff Class
using System.Runtime.InteropServices;
class Win32Stuff
{
#region Class Variables
public const int SM_CXSCREEN = 0;
public const int SM_CYSCREEN = 1;
public const Int32 CURSOR_SHOWING = 0x00000001;
[StructLayout(LayoutKind.Sequential)]
public struct ICONINFO
{
public bool fIcon; // Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies
public Int32 xHotspot; // Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot
public Int32 yHotspot; // Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot
public IntPtr hbmMask; // (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon,
public IntPtr hbmColor; // (HBITMAP) Handle to the icon color bitmap. This member can be optional if this
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public Int32 x;
public Int32 y;
}
[StructLayout(LayoutKind.Sequential)]
public struct CURSORINFO
{
public Int32 cbSize; // Specifies the size, in bytes, of the structure.
public Int32 flags; // Specifies the cursor state. This parameter can be one of the following values:
public IntPtr hCursor; // Handle to the cursor.
public POINT ptScreenPos; // A POINT structure that receives the screen coordinates of the cursor.
}
#endregion
#region Class Functions
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr ptr);
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int abc);
[DllImport("user32.dll", EntryPoint = "GetWindowDC")]
public static extern IntPtr GetWindowDC(Int32 ptr);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
public static extern bool GetCursorInfo(out CURSORINFO pci);
[DllImport("user32.dll", EntryPoint = "CopyIcon")]
public static extern IntPtr CopyIcon(IntPtr hIcon);
[DllImport("user32.dll", EntryPoint = "GetIconInfo")]
public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
#endregion
}
class Win32Stuff
{
#region Class Variables
public const int SM_CXSCREEN = 0;
public const int SM_CYSCREEN = 1;
public const Int32 CURSOR_SHOWING = 0x00000001;
[StructLayout(LayoutKind.Sequential)]
public struct ICONINFO
{
public bool fIcon; // Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies
public Int32 xHotspot; // Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot
public Int32 yHotspot; // Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot
public IntPtr hbmMask; // (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon,
public IntPtr hbmColor; // (HBITMAP) Handle to the icon color bitmap. This member can be optional if this
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public Int32 x;
public Int32 y;
}
[StructLayout(LayoutKind.Sequential)]
public struct CURSORINFO
{
public Int32 cbSize; // Specifies the size, in bytes, of the structure.
public Int32 flags; // Specifies the cursor state. This parameter can be one of the following values:
public IntPtr hCursor; // Handle to the cursor.
public POINT ptScreenPos; // A POINT structure that receives the screen coordinates of the cursor.
}
#endregion
#region Class Functions
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr ptr);
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int abc);
[DllImport("user32.dll", EntryPoint = "GetWindowDC")]
public static extern IntPtr GetWindowDC(Int32 ptr);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
public static extern bool GetCursorInfo(out CURSORINFO pci);
[DllImport("user32.dll", EntryPoint = "CopyIcon")]
public static extern IntPtr CopyIcon(IntPtr hIcon);
[DllImport("user32.dll", EntryPoint = "GetIconInfo")]
public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
#endregion
}
[C#] GZipCompress GZip압축클래스
입력 = Byte[]
출력 = Byte[]
using System.IO.Compression;
public static class GZipCompress
{
public static byte[] Compress(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
zip.Write(buffer, 0, buffer.Length);
zip.Close();
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return gzBuffer;
}
public static byte[] Decompress(byte[] gzBuffer)
{
MemoryStream ms = new MemoryStream();
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
byte[] buffer = new byte[msgLength];
ms.Position = 0;
GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);
zip.Read(buffer, 0, buffer.Length);
return buffer;
}
}
출력 = Byte[]
using System.IO.Compression;
public static class GZipCompress
{
public static byte[] Compress(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
zip.Write(buffer, 0, buffer.Length);
zip.Close();
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return gzBuffer;
}
public static byte[] Decompress(byte[] gzBuffer)
{
MemoryStream ms = new MemoryStream();
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
byte[] buffer = new byte[msgLength];
ms.Position = 0;
GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);
zip.Read(buffer, 0, buffer.Length);
return buffer;
}
}
[C#] Byte + Byte 함수
public static byte[] byte_merge(byte[] arg1, byte[] arg2)
{
byte[] tmp = new byte[arg1.Length + arg2.Length];
for (int i = 0; i < arg1.Length; i++)
{
tmp[i] = arg1[i];
}
for (int j = 0; j < arg2.Length; j++)
{
tmp[arg1.Length + j] = arg2[j];
}
return tmp;
}
{
byte[] tmp = new byte[arg1.Length + arg2.Length];
for (int i = 0; i < arg1.Length; i++)
{
tmp[i] = arg1[i];
}
for (int j = 0; j < arg2.Length; j++)
{
tmp[arg1.Length + j] = arg2[j];
}
return tmp;
}
[C#] 화면캡쳐 메모리에 넣기
Size sz = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Bitmap bt = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bt);
g.CopyFromScreen(0, 0, 0, 0, sz); // 화면캡쳐
MemoryStream ms = new MemoryStream(); // 캡쳐한 화면저장할 메모리
ms.Position = 0;
// ms버퍼에 그림을 넣는다.
bt.Save(ms, ImageFormat.Jpeg);
Bitmap bt = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bt);
g.CopyFromScreen(0, 0, 0, 0, sz); // 화면캡쳐
MemoryStream ms = new MemoryStream(); // 캡쳐한 화면저장할 메모리
ms.Position = 0;
// ms버퍼에 그림을 넣는다.
bt.Save(ms, ImageFormat.Jpeg);
2011년 6월 2일 목요일
[C#] 더블버퍼링 소스
폼안 이미지의 크기변화 위치변화등
그리는 함수가 연속적으로 불려 뚝뚝 끈기는 현상이나 깜빡임 현상이 보일 때
더블버퍼링 설정
대부분 생성자
InitializeComponent();
다음 라인에 넣는다.
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
그리는 함수가 연속적으로 불려 뚝뚝 끈기는 현상이나 깜빡임 현상이 보일 때
더블버퍼링 설정
대부분 생성자
InitializeComponent();
다음 라인에 넣는다.
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
2011년 6월 1일 수요일
[C#] float To byte, byte To float
C#으로 원격제어 프로그램을 만들던 중
float 형식을 byte형으로 바꿔 버퍼에 넣어야 하는 경우가 생겼다.
float -> byte
float a = 0.3333F;
byte[] FloatToByte = System.BitConverter.GetBytes(a);
배열이 4개인 byte가 생성된다.
byte -> float
float ByteToFloat = BitConverter.ToSingle(FloatToByte, 0);
다시 byte형식을 float로 바꾸는 방법이다.
float 형식을 byte형으로 바꿔 버퍼에 넣어야 하는 경우가 생겼다.
float -> byte
float a = 0.3333F;
byte[] FloatToByte = System.BitConverter.GetBytes(a);
배열이 4개인 byte가 생성된다.
byte -> float
float ByteToFloat = BitConverter.ToSingle(FloatToByte, 0);
다시 byte형식을 float로 바꾸는 방법이다.
피드 구독하기:
글 (Atom)