←Select platform

SetUserData Method

Summary
Sets the data for the image to the specified unmanaged memory buffer.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public void SetUserData( 
   IntPtr data, 
   long size 
) 
- (BOOL)setUserData:(nullable unsigned char *)userData 
       userDataSize:(unsigned long)userDataSize 
              error:(NSError **)error 
public void setUserData( 
  byte data[],  
  long size 
) 
public: 
void SetUserData(  
   IntPtr data, 
   int64 size 
)  
def SetUserData(self,data,size): 

Parameters

data
Pointer to the unmanaged memory buffer containing the image new data.

size
Number of bytes in  data.

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.User to 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.

Example

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

C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Core; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Dicom; 
using Leadtools.Drawing; 
using Leadtools.Controls; 
using Leadtools.Svg; 
 
 
public void SetUserDataExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "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(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
Requirements

Target Platforms

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

Leadtools Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.