←Select platform

StartRedirecting Method

Summary

Starts file I/O redirection.

Syntax
C#
VB
C++
Java
public void StartRedirecting() 
Public Sub StartRedirecting()  
public void startRedirecting() 
public: 
void StartRedirecting();  

Remarks

LEADTOOLS I/O redirection allows you to replace the default input/output methods for opening, reading, writing, seeking, and closing files.

For example, you can redirect all the library I/O methods to your own I/O methods to load/save images from your own streams.

Once you call StartRedirecting, all subsequent file I/O operation will fire the following events:

Event Operation
RedirectOpen When the toolkit is trying to open a file
RedirectRead When the toolkit is trying to reading from a file
RedirectWrite When the toolkit is trying to writing to a file
RedirectSeek When the toolkit is trying to seeking into a file
RedirectClose When the toolkit is trying to closing a file

Call StopRedirecting to reset the I/O methods back to the defaults.

Example

This example demonstrates overriding the default IO routines.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
using LeadtoolsExamples.Common; 
 
// Redirect Read event example 
FileStream myStream; 
 
// Redirect Open event example 
private void Codecs_Open(object sender, CodecsRedirectOpenEventArgs e) 
{ 
   myStream = new FileStream(e.FileName, e.Mode); 
   e.Success = true; 
} 
 
// Redirect Seek event example 
private void Codecs_Seek(object sender, CodecsRedirectSeekEventArgs e) 
{ 
   myStream.Seek(e.Offset, e.Origin); 
   e.Offset = myStream.Position; 
} 
 
// Redirect Read event example 
private void Codecs_Read(object sender, CodecsRedirectReadEventArgs e) 
{ 
   byte[] byteBuffer = new byte[e.Count]; 
   myStream.Read(byteBuffer, 0, e.Count); 
   Marshal.Copy(byteBuffer, 0, e.Buffer, e.Count); 
   e.Read = e.Count; 
   Console.WriteLine("The number of bytes that read request should copy to the buffer is : {0}", e.Count); 
   Console.WriteLine("The actual number of bytes that this read request has copied to Buffer is : {0}", e.Read); 
} 
 
// Redirect Write event example 
private void Codecs_Write(object sender, CodecsRedirectWriteEventArgs e) 
{ 
   byte[] byteBuffer = new byte[e.Count]; 
   Marshal.Copy(e.Buffer, byteBuffer, 0, e.Count); 
   myStream.Write(byteBuffer, 0, e.Count); 
   e.Written = e.Count; 
 
   Console.WriteLine("The number of bytes that write request should copy from the buffer is : {0}", e.Count); 
   Console.WriteLine("The actual number of bytes that this write request has copied from Buffer is : {0}", e.Written); 
} 
 
// Redirect Close event example 
private void Codecs_Close(object sender, CodecsRedirectCloseEventArgs e) 
{ 
   myStream.Close(); 
} 
 
public void StartRedirectingExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   codecs.RedirectOpen += new EventHandler<CodecsRedirectOpenEventArgs>(Codecs_Open); 
   codecs.RedirectSeek += new EventHandler<CodecsRedirectSeekEventArgs>(Codecs_Seek); 
   codecs.RedirectRead += new EventHandler<CodecsRedirectReadEventArgs>(Codecs_Read); 
   codecs.RedirectWrite += new EventHandler<CodecsRedirectWriteEventArgs>(Codecs_Write); 
   codecs.RedirectClose += new EventHandler<CodecsRedirectCloseEventArgs>(Codecs_Close); 
 
   string srcFilename = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
 
   codecs.StartRedirecting(); 
   RasterImage image = codecs.Load(srcFilename); 
   myStream.Close(); 
   codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "Image1_redirect.jpg"), RasterImageFormat.Jpeg, image.BitsPerPixel); 
   codecs.StopRedirecting(); 
 
   // Clean up 
   myStream.Dispose(); 
   image.Dispose(); 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
Imports Leadtools.Drawing 
Imports Leadtools.Svg 
 
' Redirect Read event example 
Private myStream As FileStream 
 
' Redirect Open event example 
Private Sub Codecs_Open(ByVal sender As Object, ByVal e As CodecsRedirectOpenEventArgs) 
   myStream = New FileStream(e.FileName, e.Mode) 
   e.Success = True 
End Sub 
 
' Redirect Seek event example 
Private Sub Codecs_Seek(ByVal sender As Object, ByVal e As CodecsRedirectSeekEventArgs) 
   myStream.Seek(e.Offset, e.Origin) 
   e.Offset = myStream.Position 
End Sub 
 
' Redirect Read event example 
Private Sub Codecs_Read(ByVal sender As Object, ByVal e As CodecsRedirectReadEventArgs) 
   Dim byteBuffer As Byte() = New Byte(e.Count - 1) {} 
   myStream.Read(byteBuffer, 0, e.Count) 
   Marshal.Copy(byteBuffer, 0, e.Buffer, e.Count) 
   e.Read = e.Count 
   Console.WriteLine("The number of bytes that read request should copy to the buffer is : {0}", e.Count) 
   Console.WriteLine("The actual number of bytes that this read request has copied to Buffer is : {0}", e.Read) 
End Sub 
 
' Redirect Write event example 
Private Sub Codecs_Write(ByVal sender As Object, ByVal e As CodecsRedirectWriteEventArgs) 
   Dim byteBuffer As Byte() = New Byte(e.Count - 1) {} 
   Marshal.Copy(e.Buffer, byteBuffer, 0, e.Count) 
   myStream.Write(byteBuffer, 0, e.Count) 
   e.Written = e.Count 
 
   Console.WriteLine("The number of bytes that write request should copy from the buffer is : {0}", e.Count) 
   Console.WriteLine("The actual number of bytes that this write request has copied from Buffer is : {0}", e.Written) 
End Sub 
 
' Redirect Close event example 
Private Sub Codecs_Close(ByVal sender As Object, ByVal e As CodecsRedirectCloseEventArgs) 
   myStream.Close() 
End Sub 
 
Public Sub StartRedirectingExample() 
   Dim codecs As RasterCodecs = New RasterCodecs() 
 
   AddHandler codecs.RedirectOpen, AddressOf Codecs_Open 
   AddHandler codecs.RedirectSeek, AddressOf Codecs_Seek 
   AddHandler codecs.RedirectRead, AddressOf Codecs_Read 
   AddHandler codecs.RedirectWrite, AddressOf Codecs_Write 
   AddHandler codecs.RedirectClose, AddressOf Codecs_Close 
 
   Dim srcFilename As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp") 
 
   codecs.StartRedirecting() 
   Dim image As RasterImage = codecs.Load(srcFilename) 
   myStream.Close() 
   codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "Image1_redirect.jpg"), RasterImageFormat.Jpeg, image.BitsPerPixel) 
   codecs.StopRedirecting() 
 
   ' Clean up 
   myStream.Dispose() 
   image.Dispose() 
   codecs.Dispose() 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 

Requirements

Target Platforms

Help Version 20.0.2020.4.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Codecs Assembly