// =====================================================================================
// Copyright � 2005 by Shahed Khan. All rights are reserved.
//
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
//
// website , email shahed.khan@gmail.com
// =====================================================================================
using System;
using System.Messaging;
using SmartJobManager;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace ClientApplication
{
class QueuedClient
{
static string QueuePath = @".\Private$\jobmanagerTest";
public string ErrorMessage = string.Empty;
public bool IsErrorExists = false;
public void QueueSimpleJob()
{
Job job = new Job(0,0,DateTime.Now,DateTime.Now,"SimpleJobManagerProvider");
JobDetailCollection jobDetails = new JobDetailCollection();
JobDetail jobDetail = new JobDetail(0,0,"Copyright","Use at your own risk.");
jobDetails.Add(jobDetail);
jobDetail = new JobDetail(0,0,"PersonName","Shahed Khan");
jobDetails.Add(jobDetail);
jobDetail = new JobDetail(1,0,"Email","shahed.khan@gmail.com");
jobDetails.Add(jobDetail);
job.JobDetailCollection = jobDetails;
QueueJob(job, QueuePath);
}
public void QueueScreenCaptureImageJob()
{
Job job = new Job(0,0,DateTime.Now,DateTime.Now,"ScreencaptureJobManagerProvider");
JobDetailCollection jobDetails = new JobDetailCollection();
JobDetail jobDetail = new JobDetail(1,0,"CaptureImage",PerformCapture());
jobDetails.Add(jobDetail);
job.JobDetailCollection = jobDetails;
QueueJob(job, QueuePath);
}
public void QueueTextToFileJob(string text)
{
if (text.Length < 1)
text = "No Text Entered";
Job job = new Job(0,0,DateTime.Now,DateTime.Now,"TextToFileJobManagerProvider");
JobDetailCollection jobDetails = new JobDetailCollection();
JobDetail jobDetail = new JobDetail(0,0,"TextToWrite",text);
jobDetails.Add(jobDetail);
job.JobDetailCollection = jobDetails;
QueueJob(job, QueuePath);
}
public void QueueTextToImageJob(string text)
{
if (text.Length < 1)
text = "No Text Entered";
Job job = new Job(0,0,DateTime.Now,DateTime.Now,"TextToImageJobManagerProvider");
JobDetailCollection jobDetails = new JobDetailCollection();
JobDetail jobDetail = new JobDetail(0,0,"TextToWrite",text);
jobDetails.Add(jobDetail);
job.JobDetailCollection = jobDetails;
QueueJob(job, QueuePath);
}
private void QueueJob(Job job, string destinationQueue)
{
try
{
// open the queue
MessageQueue mq = new MessageQueue(destinationQueue);
// set the message to durable.
mq.DefaultPropertiesToSend.Recoverable = true;
// set the formatter to Binary, default is XML
//mq.Formatter = new BinaryMessageFormatter();
// send the job object
mq.Send(job, "Job Message Test");
mq.Close();
}
catch (Exception e)
{
//Console.WriteLine(e.ToString());
this.ErrorMessage = e.ToString();
this.IsErrorExists = true;
}
}
#region Perform ScreenCapture
//Originally written by Michael Gold 04/15/2002
//http://www.c-sharpcorner.com//Code/2002/April/ScreenCaptureUtility.asp
//I modified a bit to meet my needs
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreateDC(
string lpszDriver, // driver name
string lpszDevice, // device name
string lpszOutput, // not used; should be NULL
IntPtr lpInitData // optional printer data
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
private byte[] PerformCapture()
{
//use the GDI call and create a DC to the whole display
IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
//create a Graphics object for the screen dc
Graphics g1 = Graphics.FromHdc(dc1);
// create a compatible bitmap the size of the entire screen
//MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
Bitmap image = new Bitmap(300,300,g1);
// use the bitmap to create another Graphics surface so we can BitBlast into the bitmap
Graphics g2 = Graphics.FromImage(image);
// Now go retrace our steps and get the device contexts for both the bitmap and the screen
// Note: Apparently you have to do this, and can't go directly from the aquired dc or exceptions are thrown
// When you try to release the dcs
dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
// Bit Blast the screen into the Bitmap
BitBlt(dc2, 0, 0, 300, 300, dc1, 0, 0, 13369376);
// Remember to release the dc's, otherwise problems down the road
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
//Converting to byte[]
MemoryStream memStream = new MemoryStream();
image.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);
int ImageSize = Convert.ToInt32(memStream.Length);
byte[] ImageBytes = new byte[ImageSize];
memStream.Position=0;
memStream.Read(ImageBytes, 0, ImageSize);
memStream.Close();
return ImageBytes;
}
#endregion Perform ScreenCapture
}
}