public void Close()
The cache file will be saved when this function is called.
Use this function to stop the JpipRasterImageViewer object while it is loading or decoding.
Call this function when one is through using the JpipRasterImageViewer in order to free the resources.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Jpip;
using Leadtools.Jpip.Client.WinForms;
using Leadtools.Jpip.Client.InteractiveDecoder;
using Leadtools.Jpip.Server;
using Leadtools.Drawing;
private const string SERVER_NAME = "LEAD JPIP Server - Test Server";
private const int ENUMERATION_SERVER_PORT = 109;
private const int JPIP_SERVER_PORT = 107;
private const string LOCAL_IP_ADDRESS = "127.0.0.1";
private string CLIENT_CACHE_DIRECTORY = Path.Combine(LEAD_VARS.ImagesDir, "jpeg2000");
private string SERVER_CACHE_DIRECTORY = Path.Combine(LEAD_VARS.ImagesDir, "jpeg2000");
private string SERVER_IMAGES_FOLDER = Path.Combine(LEAD_VARS.ImagesDir, "jpeg2000");
private const string IMAGE_FILE_EXTENSIONS = "*.j2k;*.jp2;*.jpx";
private string CLIENT_SAVED_FILE_NAME = Path.Combine(LEAD_VARS.ImagesDir, "test.bmp");
private JpipServer _jpipServer;
private List<string> _imageFileExtensions;
private HttpListener _listener;
public List<string> _serverImagesList = new List<string>();
public JpipClientExample()
{
Leadtools.Examples.Support.SetLicense();
_imageFileExtensions = new List<string>();
_jpipServer = new JpipServer();
}
public void ClientRequestReceived(IAsyncResult ar)
{
HttpListenerContext context;
if (null == _listener || !_listener.IsListening)
{
return;
}
try
{
context = _listener.EndGetContext(ar);
}
catch
{
return;
}
try
{
string formattedImages;
byte[] sendBuffer;
_listener.BeginGetContext(ClientRequestReceived, null);
formattedImages = GetFormattedServerImagesString();
sendBuffer = Encoding.ASCII.GetBytes(formattedImages);
context.Response.OutputStream.Write(sendBuffer, 0, sendBuffer.Length);
context.Response.Close();
}
catch (Exception)
{
if (null != context)
{
context.Response.Close();
}
}
}
public string GetFormattedServerImagesString()
{
string serverImageFile;
string formattedServerImagesString = string.Empty;
List<string> searchImages = new List<string>();
List<string> serverImages = new List<string>();
foreach (string extension in _imageFileExtensions)
{
searchImages.AddRange(Directory.GetFiles(_jpipServer.Configuration.ImagesFolder,
extension,
SearchOption.AllDirectories));
}
foreach (string file in searchImages)
{
serverImageFile = file.Replace(_jpipServer.Configuration.ImagesFolder,
string.Empty);
serverImageFile = serverImageFile.TrimStart('\\');
formattedServerImagesString += serverImageFile + ";";
}
searchImages.Clear();
foreach (KeyValuePair<string, string> aliasFolder in _jpipServer.Configuration.AliasFolders)
{
if (!Directory.Exists(aliasFolder.Value))
{
continue;
}
foreach (string extension in _imageFileExtensions)
{
searchImages.AddRange(Directory.GetFiles(aliasFolder.Value,
extension,
SearchOption.AllDirectories));
}
foreach (string imageFile in searchImages)
{
serverImageFile = imageFile.Replace(aliasFolder.Value, aliasFolder.Key);
serverImageFile = serverImageFile.TrimStart('\\');
formattedServerImagesString += serverImageFile + ";";
}
searchImages.Clear();
}
return formattedServerImagesString.TrimEnd(';');
}
public void StartFileEnumerationServer()
{
try
{
_imageFileExtensions.Clear();
_imageFileExtensions.AddRange(IMAGE_FILE_EXTENSIONS.Split(';'));
if (_listener != null)//if _listner is already created, then do not create again
return;
_listener = new HttpListener();
_listener.Prefixes.Add(string.Format("http://{0}:{1}/", LOCAL_IP_ADDRESS, ENUMERATION_SERVER_PORT));
_listener.Start();
_listener.BeginGetContext(ClientRequestReceived, null);
}
catch (Exception ex)
{
Debug.Fail("Error in server enumeration: \n" + ex.Message);
}
}
public void StopFileEnumerationServer()
{
try
{
if (_listener != null)
_listener.Stop();
}
catch (Exception ex)
{
Debug.Fail("Error in server enumeration: \n" + ex.Message);
}
}
public void GetEnumeratedFiles(string filename)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("http://{0}:{1}/", LOCAL_IP_ADDRESS, ENUMERATION_SERVER_PORT));
if (null != request.Proxy)
{
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
}
request.UseDefaultCredentials = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.Stream receivedStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(receivedStream);
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = reader.Read(read, 0, 256);
string imageNames = "";
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.
String temp = new String(read, 0, count);
imageNames += temp;
count = reader.Read(read, 0, 256);
}
// Releases the resources of the response.
response.Close();
// Releases the resources of the Stream.
reader.Close();
string[] imageFileNames = imageNames.Split(';');
int counter = 0;
foreach (string image in imageFileNames)
{
counter++;
Debug.Assert(image.Trim().Length > 0, "There is no image to enumerate");
Debug.WriteLine("Image " + counter + ": " + image);
_serverImagesList.Add(image);
}
File.WriteAllLines(filename, _serverImagesList.ToArray());
}
public void StartJpipServer()
{
if (!_jpipServer.IsRunning)
{
//Server Settings
_jpipServer.Configuration.ServerName = SERVER_NAME;
_jpipServer.Configuration.IPAddress = LOCAL_IP_ADDRESS;
_jpipServer.Configuration.Port = JPIP_SERVER_PORT;
_jpipServer.Configuration.ImagesFolder = SERVER_IMAGES_FOLDER;
_jpipServer.Configuration.CacheFolder = SERVER_CACHE_DIRECTORY;
_jpipServer.Configuration.MaxServerBandwidth = _jpipServer.Configuration.MaxServerBandwidth;
_jpipServer.Configuration.CacheSize = 200;
_jpipServer.Configuration.DivideSuperBoxes = true;
_jpipServer.Configuration.ChunkSize = 512;
_jpipServer.Configuration.MaxClientCount = 5;
_jpipServer.Configuration.ConnectionIdleTimeout = new TimeSpan(0, 0, 9100);
_jpipServer.Configuration.MaxSessionLifetime = new TimeSpan(0, 0, 9900);
_jpipServer.Configuration.MaxConnectionBandwidth = _jpipServer.Configuration.MaxConnectionBandwidth;
//Communication Settings
_jpipServer.Configuration.MaxTransportConnections = 15;
_jpipServer.Configuration.HandshakeTimeout = new TimeSpan(0, 0, 9600);
_jpipServer.Configuration.RequestTimeout = new TimeSpan(0, 0, 920);
_jpipServer.Configuration.ChunkSize = 512;
//Images Settings
_jpipServer.Configuration.ImageParsingTimeout = new TimeSpan(0, 0, 9180);
_jpipServer.Configuration.PartitionBoxSize = 40;
_jpipServer.Configuration.DivideSuperBoxes = true;
//Logging Settings
_jpipServer.Configuration.LogInformation = false;
_jpipServer.Configuration.LogWarnings = false;
_jpipServer.Configuration.LogDebug = false;
_jpipServer.Configuration.LogErrors = false;
//Start server and delegates
_jpipServer.Start();
Debug.Assert(_jpipServer.IsRunning, "Server did not start.");
}
}
public void StopJpipServer()
{
if (_jpipServer.IsRunning)
_jpipServer.Stop();
}
public void SetJpipViewer(JpipRasterImageViewer viewer)
{
// Initialize JPIP viewer
viewer.CacheDirectoryName = CLIENT_CACHE_DIRECTORY;
viewer.PortNumber = JPIP_SERVER_PORT;
viewer.IPAddress = LOCAL_IP_ADDRESS;
viewer.PacketSize = 16384;
viewer.RequestTimeout = new TimeSpan(0, 1, 0);
viewer.ChannelType = JpipChannelTypes.HttpChannel;
}
public void SaveJpipViewerImageToFile(JpipRasterImageViewer viewer, string fileName)
{
RasterCodecs rc = new Leadtools.Codecs.RasterCodecs();
rc.Save(viewer.Image, fileName, RasterImageFormat.Bmp, 24);
}
private void OnClientError(object sender, ErrorEventArgs e)
{
Debug.WriteLine(e.GetException().Message);
}
private void OnBytesLoaded(object sender, TotalBytesLoadedEventArgs e)
{
Debug.WriteLine(" Total Bytes: " + e.ByteCount.ToString());
}
public void ClientExample()
{
/*Server side*/
StartJpipServer();
/*Client requesting enumerated images from server*/
StartFileEnumerationServer();
GetEnumeratedFiles(Path.Combine(LEAD_VARS.ImagesDir, "Client_Enumerated_Images.txt"));
StopFileEnumerationServer();
JpipRasterImageViewer jpipViewer = new JpipRasterImageViewer();
SetJpipViewer(jpipViewer);
jpipViewer.StreamingError += OnClientError;
jpipViewer.TotalBytesLoaded += OnBytesLoaded;
jpipViewer.FileOpened += new EventHandler(jpipViewer_FileOpened);
Debug.WriteLine("\n Open File: " + _serverImagesList[0]);
jpipViewer.Open(_serverImagesList[0]);
}
void jpipViewer_FileOpened(object sender, EventArgs e)
{
JpipRasterImageViewer jpipViewer = (JpipRasterImageViewer)sender;
/*Client requests an image, copies to local file, then deletes cached files prior to given date from CACHE_DIRECTORY*/
DateTime deleteCacheFilePriorTo = new DateTime(2008, 8, 8);
Debug.WriteLine("\n FullImageSize: " + jpipViewer.FullImageSize.ToString());
Debug.WriteLine(" NumberOfColorComponents: " + jpipViewer.NumberOfColorComponents.ToString());
Debug.WriteLine(" NumberOfResolutions: " + jpipViewer.NumberOfResolutions.ToString());
Debug.WriteLine(" InteractiveMode: " + jpipViewer.InteractiveMode.ToString());
Debug.WriteLine(" PaintProperties: " + jpipViewer.PaintProperties.ToString());
Debug.WriteLine(" current CodeStreamIndex " + jpipViewer.CodeStream);
Debug.WriteLine(" NumberOfCodeStreams " + jpipViewer.CodeStreamCount);
Debug.WriteLine(" Available Resolutions");
for (int i = 0; i < jpipViewer.NumberOfResolutions; i++)
{
Debug.WriteLine(" Resolution (" + i.ToString() + ") Size" + jpipViewer.GetResolutionSize(i).ToString());
}
Debug.WriteLine("\nOpen Image");
Debug.WriteLine(" CurrentImageSize: " + jpipViewer.CurrentImageSize.ToString());
Debug.WriteLine(" ComponentIndex: " + jpipViewer.ComponentIndex.ToString());
Debug.WriteLine(" CurrentResolutionIndex: " + jpipViewer.CurrentResolutionIndex.ToString());
jpipViewer.ZoomIn();
Debug.WriteLine("\n Zoomed in");
Debug.WriteLine(" CurrentImageSize: " + jpipViewer.CurrentImageSize.ToString());
Debug.WriteLine(" ComponentIndex: " + jpipViewer.ComponentIndex.ToString());
Debug.WriteLine(" CurrentResolutionIndex: " + jpipViewer.CurrentResolutionIndex.ToString());
jpipViewer.ZoomIn();
Debug.WriteLine("\n Zoomed in");
Debug.WriteLine(" CurrentImageSize: " + jpipViewer.CurrentImageSize.ToString());
Debug.WriteLine(" ComponentIndex: " + jpipViewer.ComponentIndex.ToString());
Debug.WriteLine(" CurrentResolutionIndex: " + jpipViewer.CurrentResolutionIndex.ToString());
jpipViewer.ComponentIndex = 0;
Debug.WriteLine("Component Index value set to 1 ");
Debug.WriteLine(" CurrentImageSize: " + jpipViewer.CurrentImageSize.ToString());
Debug.WriteLine(" ComponentIndex: " + jpipViewer.ComponentIndex.ToString());
Debug.WriteLine(" CurrentResolutionIndex: " + jpipViewer.CurrentResolutionIndex.ToString());
jpipViewer.ZoomOut();
Debug.WriteLine("\n ZoomOut ");
Debug.WriteLine(" CurrentImageSize: " + jpipViewer.CurrentImageSize.ToString());
Debug.WriteLine(" ComponentIndex: " + jpipViewer.ComponentIndex.ToString());
Debug.WriteLine(" CurrentResolutionIndex: " + jpipViewer.CurrentResolutionIndex.ToString());
jpipViewer.ComponentIndex = -1;
Debug.WriteLine("Component Index value set to 1 ");
Debug.WriteLine(" CurrentImageSize: " + jpipViewer.CurrentImageSize.ToString());
Debug.WriteLine(" ComponentIndex: " + jpipViewer.ComponentIndex.ToString());
Debug.WriteLine(" CurrentResolutionIndex: " + jpipViewer.CurrentResolutionIndex.ToString());
jpipViewer.Zoom(jpipViewer.GetResolutionSize(2));
Debug.WriteLine(" Resolution size set to 2");
Debug.WriteLine(" CurrentImageSize: " + jpipViewer.CurrentImageSize.ToString());
Debug.WriteLine(" ComponentIndex: " + jpipViewer.ComponentIndex.ToString());
Debug.WriteLine(" CurrentResolutionIndex: " + jpipViewer.CurrentResolutionIndex.ToString());
SaveJpipViewerImageToFile(jpipViewer, CLIENT_SAVED_FILE_NAME);
/*Client side function to delete cached files of type LCCACHE File (Not LTCACHE File) prior to given date from CACHE_DIRECTORY*/
int count = jpipViewer.DeleteCacheFiles(deleteCacheFilePriorTo);
Debug.WriteLine(count.ToString() + " Cache File Deleted....");
if (jpipViewer.IsActive)
{
Debug.WriteLine(" JpipRasterImageViewer.IsActive Test ");
Debug.WriteLine(" CurrentImageSize: " + jpipViewer.CurrentImageSize.ToString());
}
jpipViewer.Close();
if (jpipViewer.IsActive)
{
Debug.WriteLine(" JpipRasterImageViewer.IsActive Test (this should not happen becaue no image is loaded at this point");
Debug.WriteLine(" CurrentImageSize: " + jpipViewer.CurrentImageSize.ToString());
}
/*Server side*/
StopJpipServer();
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images";
}