어부지망생
어부지망생
CC#
Created by 어부지망생 on 7/10/2023 in #help
❔ Capture screen(video codec) and send to client simultaneously using socket
I have already made send image code (capture image and send to client continuously. But it causes lag. So i want to try new method. Code:
private void Screen()
{
try
{
listener_3.Start();
eventLog1.WriteEntry("Waiting for connection...");

while (true)
{
Socket client = listener_3.AcceptSocket();
Console.WriteLine("Client connected");

while (client.Connected)
{
// Capture the screen
Bitmap screen = CaptureScreen();

int newWidth = screen.Width / 4; // Reduce width by half
int newHeight = screen.Height / 4; // Reduce height by half

Bitmap resizedImage = new Bitmap(newWidth, newHeight);

// Use Graphics.DrawImage to resize the image
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
graphics.DrawImage(screen, 0, 0, newWidth, newHeight);
}

// Convert the resized image to a byte array and send it to the client
byte[] imageData = ImageToByteArray(resizedImage);
client.Send(imageData);

// Delay between sending each frame (adjust as needed)
Thread.Sleep(20);
}

eventLog1.WriteEntry("Connection terminated");
client.Close();
}
}
catch (Exception e)
{
listener_3.Stop();
listener_3 = new TcpListener(IPAddress.Any, 50523);
eventLog1.WriteEntry("Error: " + e.Message);
}
}
private void Screen()
{
try
{
listener_3.Start();
eventLog1.WriteEntry("Waiting for connection...");

while (true)
{
Socket client = listener_3.AcceptSocket();
Console.WriteLine("Client connected");

while (client.Connected)
{
// Capture the screen
Bitmap screen = CaptureScreen();

int newWidth = screen.Width / 4; // Reduce width by half
int newHeight = screen.Height / 4; // Reduce height by half

Bitmap resizedImage = new Bitmap(newWidth, newHeight);

// Use Graphics.DrawImage to resize the image
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
graphics.DrawImage(screen, 0, 0, newWidth, newHeight);
}

// Convert the resized image to a byte array and send it to the client
byte[] imageData = ImageToByteArray(resizedImage);
client.Send(imageData);

// Delay between sending each frame (adjust as needed)
Thread.Sleep(20);
}

eventLog1.WriteEntry("Connection terminated");
client.Close();
}
}
catch (Exception e)
{
listener_3.Stop();
listener_3 = new TcpListener(IPAddress.Any, 50523);
eventLog1.WriteEntry("Error: " + e.Message);
}
}
7 replies
CC#
Created by 어부지망생 on 4/19/2023 in #help
❔ why this code works temporary?
This code works fine for a while in a windows service, but after some time it doesn't work. So, i stopped the Windows service and run it again, the Notify icon looks fine, but the icon does not appear in the icon tray after rebooting. And i copied this code in website so i didn't understand this code. So whoever knows they way this code is working , please tell me specifically.
2 replies
CC#
Created by 어부지망생 on 4/15/2023 in #help
❔ execute winform application by using windows service
i have already made winform application and it works properly but i want to make winform program executed by windows service is it possible ? and if it is impossible then please tell me the reason why.
21 replies
CC#
Created by 어부지망생 on 3/31/2023 in #help
❔ windows service returns error code 5
public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;

[DllImport("user32.dll")]
static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);

[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public uint type;
public InputUnion U;
}

[StructLayout(LayoutKind.Explicit)]
struct InputUnion
{
[FieldOffset(0)]
public MOUSEINPUT mi;

[FieldOffset(0)]
public KEYBDINPUT ki;

[FieldOffset(0)]
public HARDWAREINPUT hi;
}

[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}

static void SendMediaKey(ushort wScanCode)
{
INPUT input = new INPUT();
input.type = 1;
input.U.ki.wScan = wScanCode;
input.U.ki.dwFlags = 0x0008; // KEYEVENTF_SCANCODE
SendInput(1, new INPUT[] { input }, Marshal.SizeOf(typeof(INPUT)));
}

public static void Example()
{
SendMediaKey(VK_MEDIA_NEXT_TRACK);
}
public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;

[DllImport("user32.dll")]
static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);

[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public uint type;
public InputUnion U;
}

[StructLayout(LayoutKind.Explicit)]
struct InputUnion
{
[FieldOffset(0)]
public MOUSEINPUT mi;

[FieldOffset(0)]
public KEYBDINPUT ki;

[FieldOffset(0)]
public HARDWAREINPUT hi;
}

[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}

static void SendMediaKey(ushort wScanCode)
{
INPUT input = new INPUT();
input.type = 1;
input.U.ki.wScan = wScanCode;
input.U.ki.dwFlags = 0x0008; // KEYEVENTF_SCANCODE
SendInput(1, new INPUT[] { input }, Marshal.SizeOf(typeof(INPUT)));
}

public static void Example()
{
SendMediaKey(VK_MEDIA_NEXT_TRACK);
}
2 replies
CC#
Created by 어부지망생 on 3/25/2023 in #help
❔ keyboard input not working in windows service
keyboard input error
6 replies
CC#
Created by 어부지망생 on 3/1/2023 in #help
❔ Allow service to interact with desktop
public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;

[DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero); // PREV
public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;

[DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero); // PREV
this code works properly in windows form but it doesn't in windows service so i found i need to allow service to interact with desktop so i tried but it doesn't work neither
87 replies
CC#
Created by 어부지망생 on 2/26/2023 in #help
❔ media key press
37 replies
CC#
Created by 어부지망생 on 2/20/2023 in #help
❔ windows service socket error
socket accept works successfully in IDE, but not in win service.
10 replies
CC#
Created by 어부지망생 on 2/14/2023 in #help
❔ Win service error..
public static String GetPcName()
{
string hostName = System.Net.Dns.GetHostName();

return hostName;

}

public static String GetMacAddr()
{
string MacAddr = (from nic in NetworkInterface.GetAllNetworkInterfaces() where nic.OperationalStatus == OperationalStatus.Up select nic.GetPhysicalAddress().ToString()).FirstOrDefault();

return MacAddr;
}
public static String GetPcName()
{
string hostName = System.Net.Dns.GetHostName();

return hostName;

}

public static String GetMacAddr()
{
string MacAddr = (from nic in NetworkInterface.GetAllNetworkInterfaces() where nic.OperationalStatus == OperationalStatus.Up select nic.GetPhysicalAddress().ToString()).FirstOrDefault();

return MacAddr;
}
public static String GetPcName()
{
string hostName = System.Net.Dns.GetHostName();

return hostName;

}

public static String GetMacAddr()
{
string MacAddr = (from nic in NetworkInterface.GetAllNetworkInterfaces() where nic.OperationalStatus == OperationalStatus.Up select nic.GetPhysicalAddress().ToString()).FirstOrDefault();

return MacAddr;
}
public static String GetPcName()
{
string hostName = System.Net.Dns.GetHostName();

return hostName;

}

public static String GetMacAddr()
{
string MacAddr = (from nic in NetworkInterface.GetAllNetworkInterfaces() where nic.OperationalStatus == OperationalStatus.Up select nic.GetPhysicalAddress().ToString()).FirstOrDefault();

return MacAddr;
}
i wonder why win32 service does not work well.. this code works well in not service i don't know what's the problem
67 replies