LEADTOOLS Image File Support (Leadtools.Codecs assembly)
LEAD Technologies, Inc

RetrieveDataFromImage Property

Example 





Gets or sets a value that indicate whether to automatically populate the scanline buffers in CodecsSaveImageEventArgs when using the RasterCodecs.SaveImage event. .NET support WinRT support Silverlight support
Syntax
public bool RetrieveDataFromImage {get; set;}
'Declaration
 
Public Property RetrieveDataFromImage As Boolean
'Usage
 
Dim instance As CodecsSaveOptions
Dim value As Boolean
 
instance.RetrieveDataFromImage = value
 
value = instance.RetrieveDataFromImage
public bool RetrieveDataFromImage {get; set;}
 get_RetrieveDataFromImage();
set_RetrieveDataFromImage(value);
public:
property bool RetrieveDataFromImage {
   bool get();
   void set (    bool value);
}

Property Value

true to automatically populate the scanline buffers in CodecsSaveImageEventArgs when using the RasterCodecs.SaveImage event, or false to make the user responsible for populating the scanline buffers. Default value is false.
Remarks

The Buffer property works as the input and output buffer containing the image data to save. If the value of RetrieveDataFromImage is set to false (the default), then the user is always responsible for providing the image data by setting in Buffer. If the value of RetrieveDataFromImage is set to true, then the RasterCodecs object will populate the Buffer prior to raising this event. The user can then inspect or modify the scanlines data or simple ignore it to save the original image data as is.

Notice that on either case, the user must provide the scanline data in the source image original format (stored in the Image property. The RasterCodecs object will then convert this data to the appropriate output format if needed, for example, if the user instructed the RasterCodecs object to save the image in a different file format than the original image.

Example
 
Private myMode As Integer
   Public Sub RetrieveDataFromImageExample()

      Using codecs As New RasterCodecs()
         Dim srcFile As String = Path.Combine(LEAD_VARS.ImagesDir, "Sample1.cmp")
         Dim blankFile As String = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Blank.bmp")
         Dim defaultFile As String = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Default.bmp")
         Dim invertedFile As String = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Inverted.bmp")

         ' Load the source image
         Using image As RasterImage = codecs.Load(srcFile)
            ' Subscribe to the SaveImage event
            AddHandler codecs.SaveImage, AddressOf codecs_SaveImage

            ' First, set RetrieveDataFromImage to false and save
            ' This should generate a blank image
            myMode = 0
            codecs.Options.Save.RetrieveDataFromImage = False
            codecs.Save(image, blankFile, RasterImageFormat.Bmp, 24)

            ' Next, set RetrieveDataFromImage to true but do not
            ' change the data, this should generate the correct image and is the equivalant
            ' to calling Save without subscribing to SaveImage
            myMode = 1
            codecs.Options.Save.RetrieveDataFromImage = True
            codecs.Save(image, defaultFile, RasterImageFormat.Bmp, 24)

            ' Finally, leave RetrieveDataFromImage as true but change the data by inverting
            ' the scanlines, this should generate an inverted image
            myMode = 2
            codecs.Save(image, invertedFile, RasterImageFormat.Bmp, 24)

            RemoveHandler codecs.SaveImage, AddressOf codecs_SaveImage
         End Using
      End Using

   End Sub

   Private Sub codecs_SaveImage(ByVal sender As Object, ByVal e As CodecsSaveImageEventArgs)
      ' This example works with 24 bpp images only
      Debug.Assert(e.Image.BitsPerPixel = 24)

      Select Case myMode
         Case 0
            ' Do not do anything

         Case 1
            ' Do not do anything

         Case 2
            ' Invert the image data
            Dim scanlineLength As Integer = e.Image.BytesPerLine
            Dim scanline(scanlineLength - 1) As Byte

            ' Loop through all the scanlines in the data
            For y As Integer = 0 To e.Lines - 1
               ' Get this row
               e.Buffer.GetData(y * scanlineLength, scanline, 0, scanlineLength)

               ' Process it by inverting every pixel data
               For x As Integer = 0 To scanlineLength - 1
                  scanline(x) = CByte(255 - scanline(x))
               Next

               ' Copy it back to the event buffer
               e.Buffer.SetData(y * scanlineLength, scanline, 0, scanlineLength)
            Next
      End Select
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
private int myMode;
     public void RetrieveDataFromImageExample()
     {
         using (RasterCodecs codecs = new RasterCodecs())
         {
             string srcFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1.cmp");
             string blankFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Blank.bmp");
             string defaultFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Default.bmp");
             string invertedFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Inverted.bmp");

             // Load the source image
             using (RasterImage image = codecs.Load(srcFile))
             {
                 // Subscribe to the SaveImage event
                 codecs.SaveImage += new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage);

                 // First, set RetrieveDataFromImage to false and save
                 // This should generate a blank image
                 myMode = 0;
                 codecs.Options.Save.RetrieveDataFromImage = false;
                 codecs.Save(image, blankFile, RasterImageFormat.Bmp, 24);

                 // Next, set RetrieveDataFromImage to true but do not
                 // change the data, this should generate the correct image and is the equivalant
                 // to calling Save without subscribing to SaveImage
                 myMode = 1;
                 codecs.Options.Save.RetrieveDataFromImage = true;
                 codecs.Save(image, defaultFile, RasterImageFormat.Bmp, 24);

                 // Finally, leave RetrieveDataFromImage as true but change the data by inverting
                 // the scanlines, this should generate an inverted image
                 myMode = 2;
                 codecs.Save(image, invertedFile, RasterImageFormat.Bmp, 24);

                 codecs.SaveImage -= new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage);
             }
         }
     }

     private void codecs_SaveImage(object sender, CodecsSaveImageEventArgs e)
     {
         // This example works with 24 bpp images only
         Debug.Assert(e.Image.BitsPerPixel == 24);

         switch (myMode)
         {
             case 0:
                 // Do not do anything
                 break;

             case 1:
                 // Do not do anything
                 break;

             case 2:
                 // Invert the image data
                 {
                     int scanlineLength = e.Image.BytesPerLine;
                     byte[] scanline = new byte[scanlineLength];

                     // Loop through all the scanlines in the data
                     for (int y = 0; y < e.Lines; y++)
                     {
                         // Get this row
                         e.Buffer.GetData(y * scanlineLength, scanline, 0, scanlineLength);

                         // Process it by inverting every pixel data
                         for (int x = 0; x < scanlineLength; x++)
                         {
                             scanline[x] = (byte)(255 - scanline[x]);
                         }

                         // Copy it back to the event buffer
                         e.Buffer.SetData(y * scanlineLength, scanline, 0, scanlineLength);
                     }
                 }
                 break;
         }
     }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
var  myMode;

CodecsOptionsExamples.prototype.RetrieveDataFromImageExample = function () {
   Tools.SetLicense();
   with (Leadtools) {
      with (Leadtools.Codecs) {
         
         var codecs = new RasterCodecs();
         Leadtools.RasterSupport.initialize();
         codecs.eventsDispatchMode = Leadtools.LeadEventsDispatchMode.useCoreDispatcher;
         var srcFileName = "Assets\\Image1.cmp";

         var blankFile = "Sample1_Blank.bmp";
         var defaultFile = "Sample1_Default.bmp";
         var invertedFile = "Sample1_Inverted.bmp";
         var image ;

         // Load the source image
         return Tools.AppInstallFolder().getFileAsync(srcFileName).then(function (loadFile) {
            return codecs.loadAsync(LeadStreamFactory.create(loadFile))
         })
            .then(function (img) {
               image = img;

               // Subscribe to the SaveImage event
               codecs.addEventListener("saveimage", codecs_SaveImage);

               // First, set RetrieveDataFromImage to false and save
               // This should generate a blank image
               myMode = 0;
               codecs.options.save.retrieveDataFromImage = false;
               return Tools.AppLocalFolder().createFileAsync(blankFile)
            })
            .then(function (saveFile) {
               return codecs.saveAsync(image, LeadStreamFactory.create(saveFile), RasterImageFormat.bmp, 24)
            })
            .then(function () {

               // Next, set RetrieveDataFromImage to true but do not
               // change the data, this should generate the correct image and is the equivalant
               // to calling Save without subscribing to SaveImage
               myMode = 1;
               codecs.options.save.retrieveDataFromImage = true;
               return Tools.AppLocalFolder().createFileAsync(defaultFile)
            })
            .then(function (saveFile) {
               return codecs.saveAsync(image, LeadStreamFactory.create(saveFile), RasterImageFormat.bmp, 24)
            })
            .then(function () {

               // Finally, leave RetrieveDataFromImage as true but change the data by inverting
               // the scanlines, this should generate an inverted image
               myMode = 2;
               return Tools.AppLocalFolder().createFileAsync(invertedFile)
            })
            .then(function (saveFile) {
               return codecs.saveAsync(image, LeadStreamFactory.create(saveFile), RasterImageFormat.bmp, 24)
            })
            .then(function () {
               codecs.removeEventListener("saveimage", codecs_SaveImage);
               codecs.close();
            });
      }
   }
}


function codecs_SaveImage(e) {
   // This example works with 24 bpp images only
   console.assert(e.image.bitsPerPixel == 24, "e.image.bitsPerPixel == 24");

   switch (myMode) {
      case 0:
         // Do not do anything
         break;

      case 1:
         // Do not do anything
         break;

      case 2:
         // Invert the image data
         {
            var scanlineLength = e.image.bytesPerLine;
            var scanline = new Array(scanlineLength);

            // Loop through all the scanlines in the data
            for (var y = 0; y < e.lines; y++) {
               // Get this row
               e.buffer.getData(y * scanlineLength, scanline, 0, scanlineLength);

               // Process it by inverting every pixel data
               for (var x = 0; x < scanlineLength; x++) {
                  scanline[x] = Tools.ToByte(255 - scanline[x]);
               }

               // Copy it back to the event buffer
               e.buffer.setData(y * scanlineLength, scanline, 0, scanlineLength);
            }
         }
         break;
   }
}
private int myMode;
[TestMethod]
public async Task RetrieveDataFromImageExample()
{
   using (RasterCodecs codecs = new RasterCodecs())
   {
      string srcFileName = @"Assets\Image1.cmp";
      string blankFile = "Sample1_Blank.bmp";
      string defaultFile = "Sample1_Default.bmp";
      string invertedFile = "Sample1_Inverted.bmp";

      // Load the source image
      StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
      using (RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile)))
      {
         // Subscribe to the SaveImage event
         codecs.SaveImage += new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage);

         // First, set RetrieveDataFromImage to false and save
         // This should generate a blank image
         myMode = 0;
         codecs.Options.Save.RetrieveDataFromImage = false;
         StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(blankFile);
         await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.Bmp, 24);

         // Next, set RetrieveDataFromImage to true but do not
         // change the data, this should generate the correct image and is the equivalant
         // to calling Save without subscribing to SaveImage
         myMode = 1;
         codecs.Options.Save.RetrieveDataFromImage = true;
         saveFile = await Tools.AppLocalFolder.CreateFileAsync(defaultFile);
         await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.Bmp, 24);

         // Finally, leave RetrieveDataFromImage as true but change the data by inverting
         // the scanlines, this should generate an inverted image
         myMode = 2;
         saveFile = await Tools.AppLocalFolder.CreateFileAsync(invertedFile);
         await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.Bmp, 24);

         codecs.SaveImage -= new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage);
      }
   }
}

private void codecs_SaveImage(object sender, CodecsSaveImageEventArgs e)
{
   // This example works with 24 bpp images only
   Assert.IsTrue(e.Image.BitsPerPixel == 24);

   switch (myMode)
   {
      case 0:
         // Do not do anything
         break;

      case 1:
         // Do not do anything
         break;

      case 2:
         // Invert the image data
         {
            int scanlineLength = e.Image.BytesPerLine;
            byte[] scanline = new byte[scanlineLength];

            // Loop through all the scanlines in the data
            for (int y = 0; y < e.Lines; y++)
            {
               // Get this row
               e.Buffer.GetData(y * scanlineLength, scanline, 0, scanlineLength);

               // Process it by inverting every pixel data
               for (int x = 0; x < scanlineLength; x++)
               {
                  scanline[x] = (byte)(255 - scanline[x]);
               }

               // Copy it back to the event buffer
               e.Buffer.SetData(y * scanlineLength, scanline, 0, scanlineLength);
            }
         }
         break;
   }
}
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

CodecsSaveOptions Class
CodecsSaveOptions Members

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.