Leadtools Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
SetUserData Method
See Also  Example
Leadtools Namespace > RasterImage Class : SetUserData Method



data
Pointer to the unmanaged memory buffer containing the image new data.
size
Number of bytes in data.
Sets the data for the image to the specified unmanaged memory buffer.

Syntax

Visual Basic (Declaration) 
Public Sub SetUserData( _
   ByVal data As IntPtr, _
   ByVal size As Long _
) 
Visual Basic (Usage)Copy Code
Dim instance As RasterImage
Dim data As IntPtr
Dim size As Long
 
instance.SetUserData(data, size)
C# 
public void SetUserData( 
   IntPtr data,
   long size
)
C++/CLI 
public:
void SetUserData( 
   IntPtr data,
   long size
) 

Parameters

data
Pointer to the unmanaged memory buffer containing the image new data.
size
Number of bytes in data.

Example

This example uses GetPixelData and SetPixelData methods to swap the R and G values for a particular pixel.

Visual BasicCopy Code
Public Sub SetUserDataExample()
   RasterCodecs.Startup()
   Dim codecs As RasterCodecs = New RasterCodecs()

   Dim destFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "UserData.bmp"

   ' Create an image with user defined data
   Dim width As Integer = 40
   Dim height As Integer = 40
   Dim bitsPerPixel As Integer = 24
   Dim size As Integer = width * height * 3
   Dim data1 As IntPtr = Marshal.AllocHGlobal(size)

   ' Load the image
   Dim image As RasterImage = New RasterImage(RasterMemoryFlags.User, width, height, bitsPerPixel, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, IntPtr.Zero, 0)

   ' Get a pointer to the internal data and fill it with gray shades
   Dim dataSize As Integer = CInt(image.DataSize)

   Dim buffer As Byte() = New Byte(dataSize - 1) {}
   Dim inc As Boolean = True
   Dim value As Byte = 0
   Dim i As Integer = 0
   Do While i < buffer.Length
      buffer(i + 0) = value
      buffer(i + 1) = value
      buffer(i + 2) = value

      If inc Then
         If value = 255 Then
            inc = False
            value = 254
         Else
            value = value + Convert.ToByte(1)
         End If
      Else
         If value = 0 Then
            inc = True
            value = 1
         Else
            value = value - Convert.ToByte(1)
         End If
      End If
      i += 3
   Loop

   ' Copy this buffer to the image data
   Marshal.Copy(buffer, 0, data1, buffer.Length)

   ' Set the user data
   image.SetUserData(data1, dataSize)

   ' Save the image
   codecs.Save(image, destFileName, RasterImageFormat.Bmp, 24)

   image.Dispose()
   Marshal.FreeHGlobal(data1)
   codecs.Dispose()
   RasterCodecs.Shutdown()
End Sub
C#Copy Code
public void SetUserDataExample() 

   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string destFileName = LeadtoolsExamples.Common.ImagesPath.Path + "UserData.bmp"; 
 
   // Create an image with user defined data 
   int width = 40; 
   int height = 40; 
   int bitsPerPixel = 24; 
   int size = width * height * 3; 
   IntPtr data1 = Marshal.AllocHGlobal(size); 
 
   // Load the image 
   RasterImage image = new RasterImage( 
      RasterMemoryFlags.User, 
      width, 
      height, 
      bitsPerPixel, 
      RasterByteOrder.Bgr, 
      RasterViewPerspective.TopLeft, 
      null, 
      IntPtr.Zero, 
      0); 
 
   // Get a pointer to the internal data and fill it with gray shades 
   long dataSize = image.DataSize; 
 
   byte[] buffer = new byte[dataSize]; 
   bool inc = true; 
   byte value = 0; 
   for(int i = 0; i < buffer.Length; i += 3) 
   { 
      buffer[i + 0] = value; 
      buffer[i + 1] = value; 
      buffer[i + 2] = value; 
 
      if(inc) 
      { 
         if(value == 255) 
         { 
            inc = false; 
            value = 254; 
         } 
         else 
            value++; 
      } 
      else 
      { 
         if(value == 0) 
         { 
            inc = true; 
            value = 1; 
         } 
         else 
            value--; 
      } 
   } 
 
   // Copy this buffer to the image data 
   Marshal.Copy(buffer, 0, data1, buffer.Length); 
 
   // Set the user data 
   image.SetUserData(data1, dataSize); 
 
   // Save the image 
   codecs.Save(image, destFileName, RasterImageFormat.Bmp, 24); 
 
   image.Dispose(); 
   Marshal.FreeHGlobal(data1); 
   codecs.Dispose(); 
   RasterCodecs.Shutdown(); 
}

Remarks

The data that you specify will not be copied, but instead will be referenced by the image until the image is disposed, or until you call this method again.

To set up a RasterImage object with unmanaged user data, call the _ctor constructor passing RasterMemoryFlags.Userto the flags parameter.

Some image processing commands, such as RotateCommand and ColorResolutionCommand, need to re-allocate the image data. If you create an image with RasterMemoryFlags.User, and pass it to these command, they will change the image to RasterMemoryFlags.Conventional and re-allocate memory. Your original memory will no longer be used.

You are responsible for managing the image data. Dispose will not free data.

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also