Sets the data for the image to the specified unmanaged memory buffer.
- (BOOL)setUserData:(nullable unsigned char *)userDatauserDataSize:(unsigned long)userDataSizeerror:(NSError **)error
public void setUserData(byte data[],long size)
function Leadtools.RasterImage.SetUserData(data ,size)
data
Pointer to the unmanaged memory buffer containing the image new data.
size
Number of bytes in data.
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.
This example uses GetPixelData and SetPixelData methods to swap the R and G values for a particular pixel.
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 LeadtoolsExamples.Common;using Leadtools.Svg;public void SetUserDataExample(){RasterCodecs codecs = new RasterCodecs();string destFileName = Path.Combine(ImagesPath.Path, "UserData.bmp");// Create an image with user defined dataint width = 40;int height = 40;int bitsPerPixel = 24;int size = width * height * 3;IntPtr data1 = Marshal.AllocHGlobal(size);// Load the imageRasterImage 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 shadeslong 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;}elsevalue++;}else{if (value == 0){inc = true;value = 1;}elsevalue--;}}// Copy this buffer to the image dataMarshal.Copy(buffer, 0, data1, buffer.Length);// Set the user dataimage.SetUserData(data1, dataSize);// Save the imagecodecs.Save(image, destFileName, RasterImageFormat.Bmp, 24);image.Dispose();Marshal.FreeHGlobal(data1);codecs.Dispose();}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.ImageProcessingImports Leadtools.ImageProcessing.CoreImports Leadtools.ImageProcessing.ColorImports Leadtools.ControlsImports Leadtools.DicomImports Leadtools.DrawingImports Leadtools.SvgPublic Sub SetUserDataExample()Dim codecs As RasterCodecs = New RasterCodecs()Dim destFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "UserData.bmp")' Create an image with user defined dataDim width As Integer = 40Dim height As Integer = 40Dim bitsPerPixel As Integer = 24Dim size As Integer = width * height * 3Dim data1 As IntPtr = Marshal.AllocHGlobal(size)' Load the imageDim 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 shadesDim dataSize As Integer = CInt(image.DataSize)Dim buffer As Byte() = New Byte(dataSize - 1) {}Dim inc As Boolean = TrueDim value As Byte = 0Dim i As Integer = 0Do While i < buffer.Lengthbuffer(i + 0) = valuebuffer(i + 1) = valuebuffer(i + 2) = valueIf inc ThenIf value = 255 Theninc = Falsevalue = 254Elsevalue = value + Convert.ToByte(1)End IfElseIf value = 0 Theninc = Truevalue = 1Elsevalue = value - Convert.ToByte(1)End IfEnd Ifi += 3Loop' Copy this buffer to the image dataMarshal.Copy(buffer, 0, data1, buffer.Length)' Set the user dataimage.SetUserData(data1, dataSize)' Save the imagecodecs.Save(image, destFileName, RasterImageFormat.Bmp, 24)image.Dispose()Marshal.FreeHGlobal(data1)codecs.Dispose()End SubPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"End Class
using Leadtools;using Leadtools.Codecs;using Leadtools.Dicom;using Leadtools.ImageProcessing;using Leadtools.ImageProcessing.Core;using Leadtools.ImageProcessing.Color;using Leadtools.Examples;using Leadtools.Windows.Media;public void SetUserDataExample(Stream destStream){// Create an image with user defined dataint width = 40;int height = 40;int bitsPerPixel = 24;// Create a user imageRasterImage image = new RasterImage(RasterMemoryFlags.User,width,height,bitsPerPixel,RasterByteOrder.Bgr,RasterViewPerspective.TopLeft,null,null,0);// Create a data buffer for the image data// and fill the pixel values (gradient)int size = width * height * 3;byte[] buffer = new byte[size];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;}elsevalue++;}else{if (value == 0){inc = true;value = 1;}elsevalue--;}}// Set the user data of the imageimage.CopyData(buffer, buffer.Length);// Save the imageRasterCodecs codecs = new RasterCodecs();codecs.Save(image, destStream, RasterImageFormat.Bmp, 24);image.Dispose();}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.DicomImports Leadtools.ImageProcessingImports Leadtools.ImageProcessing.CoreImports Leadtools.ImageProcessing.ColorImports Leadtools.Windows.MediaPublic Sub SetUserDataExample(ByVal destStream As Stream)' Create an image with user defined dataDim width As Integer = 40Dim height As Integer = 40Dim bitsPerPixel As Integer = 24' Create a user imageDim image As RasterImage = New RasterImage(RasterMemoryFlags.User, width, height, bitsPerPixel, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, Nothing, 0)' Create a data buffer for the image data' and fill the pixel values (gradient)Dim size As Integer = width * height * 3Dim buffer As Byte() = New Byte(size - 1) {}Dim inc As Boolean = TrueDim value As Byte = 0Dim i As Integer = 0Do While i < buffer.Lengthbuffer(i + 0) = valuebuffer(i + 1) = valuebuffer(i + 2) = valueIf inc ThenIf value = 255 Theninc = Falsevalue = 254Elsevalue += 1End IfElseIf value = 0 Theninc = Truevalue = 1Elsevalue -= 1End IfEnd Ifi += 3Loop' Set the user data of the imageimage.CopyData(buffer, buffer.Length)' Save the imageDim codecs As RasterCodecs = New RasterCodecs()codecs.Save(image, destStream, RasterImageFormat.Bmp, 24)image.Dispose()End Sub
|
Products |
Support |
Feedback: SetUserData Method - Leadtools |
Introduction |
Help Version 19.0.2017.6.19
|

Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
Your email has been sent to support! Someone should be in touch! If your matter is urgent please come back into chat.
Chat Hours:
Monday - Friday, 8:30am to 6pm ET
Thank you for your feedback!
Please fill out the form again to start a new chat.
All agents are currently offline.
Chat Hours:
Monday - Friday
8:30AM - 6PM EST
To contact us please fill out this form and we will contact you via email.