Leadtools Namespace > RasterRegion Class : GetData Method |
public byte[] GetData()
'Declaration Public Function GetData() As Byte()
'Usage Dim instance As RasterRegion Dim value() As Byte value = instance.GetData()
public byte[] GetData()
public byte[] getData()
function Leadtools.RasterRegion.GetData()
public: array<byte>^ GetData();
You can use the GetData and SetData methods to save and load the content of a region to disk or memory.
If this RasterRegion is empty, then this method will return an array of 0 items.
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.ImageProcessing Public Sub RasterRegionDataExample() Dim codecs As New RasterCodecs() Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp") Dim destFileName1 As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_WithRegion1.bmp") Dim regionFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_Region.bin") Dim destFileName2 As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_WithRegion2.bmp") Dim region As RasterRegion = Nothing ' Load the source image Using image As RasterImage = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1) ' Add an elliptical region to it image.AddEllipseToRegion(Nothing, New LeadRect(0, 0, image.ImageWidth, image.ImageHeight), RasterRegionCombineMode.Set) ' Fill the image with a color and save it to disk to show the region Dim cmd As New FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Yellow)) cmd.Run(image) codecs.Save(image, destFileName1, RasterImageFormat.Bmp, 24) ' Get the region region = image.GetRegion(Nothing) End Using ' Save this region to disk Dim data() As Byte = region.GetData() File.WriteAllBytes(regionFileName, data) ' Dispose the region region.Dispose() ' Now, reload the image and region from disk, set the region into the image directly ' from the data we save, re-fill and save again Using image As RasterImage = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1) ' Create a region from the data we saved on disk data = File.ReadAllBytes(regionFileName) region = New RasterRegion(data) ' Set this region into the image image.SetRegion(Nothing, region, RasterRegionCombineMode.Set) region.Dispose() ' Fill the image with a color and save it to disk to show the region Dim cmd As New FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Yellow)) cmd.Run(image) codecs.Save(image, destFileName2, RasterImageFormat.Bmp, 24) End Using codecs.Dispose() End Sub Public NotInheritable Class LEAD_VARS Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" End Class
using Leadtools; using Leadtools.Codecs; using Leadtools.ImageProcessing; public void RasterRegionDataExample() { string srcFileName = Path.Combine(ImagesPath.Path, "Image1.cmp"); string destFileName1 = Path.Combine(ImagesPath.Path, "Image1_WithRegion1.bmp"); string regionFileName = Path.Combine(ImagesPath.Path, "Image1_Region.bin"); string destFileName2 = Path.Combine(ImagesPath.Path, "Image1_WithRegion2.bmp"); RasterRegion region = null; using (RasterCodecs codecs = new RasterCodecs()) { // Load the source image using (RasterImage image = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)) { // Add an elliptical region to it image.AddEllipseToRegion(null, new LeadRect(0, 0, image.ImageWidth, image.ImageHeight), RasterRegionCombineMode.Set); // Fill the image with a color and save it to disk to show the region FillCommand cmd = new FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Yellow)); cmd.Run(image); codecs.Save(image, destFileName1, RasterImageFormat.Bmp, 24); // Get the region region = image.GetRegion(null); } // Save this region to disk byte[] data = region.GetData(); File.WriteAllBytes(regionFileName, data); // Dispose the region region.Dispose(); // Now, reload the image and region from disk, set the region into the image directly // from the data we save, re-fill and save again using (RasterImage image = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)) { // Create a region from the data we saved on disk data = File.ReadAllBytes(regionFileName); using (region = new RasterRegion(data)) { // Set this region into the image image.SetRegion(null, region, RasterRegionCombineMode.Set); } // Fill the image with a color and save it to disk to show the region FillCommand cmd = new FillCommand(RasterColor.FromKnownColor(RasterKnownColor.Yellow)); cmd.Run(image); codecs.Save(image, destFileName2, RasterImageFormat.Bmp, 24); } } }
RasterRegionExamples.prototype.RasterRegionDataExample = function () { Tools.SetLicense(); with (Leadtools) { with (Leadtools.Codecs) { var codecs = new RasterCodecs(); var srcFileName = "Assets\\Image1.cmp"; var destFileName1 = "Image1_WithRegion1.bmp"; var regionFileName = "Image1_Region.bin"; var destFileName2 = "Image1_WithRegion2.bmp"; var region = null; var image; var data; var regionStream; var buffer; // Load the source image return Tools.AppInstallFolder().getFileAsync(srcFileName).then(function (loadFile) { return codecs.loadAsync(LeadStreamFactory.create(loadFile), 0, CodecsLoadByteOrder.bgr, 1, 1) }) .then(function (img) { image = img; // Add an elliptical region to it image.addEllipseToRegion(null, LeadRectHelper.create(0, 0, image.imageWidth, image.imageHeight), RasterRegionCombineMode.set); // Fill the image with a color and save it to disk to show the region var cmd = new Leadtools.ImageProcessing.FillCommand(RasterColorHelper.fromKnownColor(RasterKnownColor.yellow)); cmd.run(image); return Tools.AppLocalFolder().createFileAsync(destFileName1) }) .then(function (saveFile) { var saveStream = LeadStreamFactory.create(saveFile); return codecs.saveAsync(image, saveStream, RasterImageFormat.bmp, 0) }) .then(function () { // Get the region region = image.getRegion(null); // Save this region to disk data = region.getData(); return Tools.AppLocalFolder().createFileAsync(regionFileName, Windows.Storage.CreationCollisionOption.replaceExisting) }) .then(function (regionFile) { return regionFile.openAsync(Windows.Storage.FileAccessMode.readWrite) }) .then(function (regStream) { regionStream = regStream; var dataWriter = new Windows.Storage.Streams.DataWriter(); dataWriter.writeBytes(data); buffer = dataWriter.detachBuffer(); return regionStream.writeAsync(buffer) }).then(function () { regionStream.close(); // Dispose the region region.close(); // Now, reload the image and region from disk, set the region into the image directly // from the data we save, re-fill and save again return Tools.AppInstallFolder().getFileAsync(srcFileName) }) .then(function (loadFile) { return codecs.loadAsync(LeadStreamFactory.create(loadFile), 0, CodecsLoadByteOrder.bgr, 1, 1) }) .then(function (loadFile) { // Create a region from the data we saved on disk return Tools.AppLocalFolder().createFileAsync(regionFileName, Windows.Storage.CreationCollisionOption.openIfExists) //return Tools.AppLocalFolder().openStreamForReadAsync(regionFileName) }) .then(function (regionFile2) { return regionFile2.openReadAsync() }) .then(function (regionStream2) { var dataReader = new Windows.Storage.Streams.DataReader(regionStream2); data = new Uint8Array(dataReader.unconsumedBufferLength); dataReader.readBytes(data); region = new RasterRegion(); region.setData(data); // Set this region into the image image.setRegion(null, region, RasterRegionCombineMode.set); region.close(); // Fill the image with a color and save it to disk to show the region var cmd = new Leadtools.ImageProcessing.FillCommand(RasterColorHelper.fromKnownColor(RasterKnownColor.yellow)); cmd.run(image); return Tools.AppLocalFolder().createFileAsync(destFileName2) }) .then(function (saveFile) { var saveStream = LeadStreamFactory.create(saveFile); return codecs.saveAsync(image, saveStream, RasterImageFormat.bmp, 24) }) .then(function () { image.close(); codecs.close(); }); } } }
using Leadtools; using Leadtools.Codecs; using Leadtools.ImageProcessing; public async Task RasterRegionDataExample() { RasterCodecs codecs = new RasterCodecs(); string srcFileName = @"Assets\Image1.cmp"; string destFileName1 = "Image1_WithRegion1.bmp"; string regionFileName = "Image1_Region.bin"; string destFileName2 = "Image1_WithRegion2.bmp"; RasterRegion region = null; // Load the source image StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName); using (RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 0, CodecsLoadByteOrder.Bgr, 1, 1)) { // Add an elliptical region to it image.AddEllipseToRegion(null, LeadRectHelper.Create(0, 0, image.ImageWidth, image.ImageHeight), RasterRegionCombineMode.Set); // Fill the image with a color and save it to disk to show the region FillCommand cmd = new FillCommand(RasterColorHelper.FromKnownColor(RasterKnownColor.Yellow)); cmd.Run(image); StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName1); ILeadStream saveStream = LeadStreamFactory.Create(saveFile); await codecs.SaveAsync(image, saveStream, RasterImageFormat.Bmp, 0); // Get the region region = image.GetRegion(null); } // Save this region to disk byte[] data = region.GetData(); StorageFile regionFile = await Tools.AppLocalFolder.CreateFileAsync(regionFileName, CreationCollisionOption.ReplaceExisting); System.IO.Stream regionStream = await regionFile.OpenStreamForWriteAsync(); regionStream.Write(data, 0, data.Length); regionStream.Dispose(); // Dispose the region region.Dispose(); // Now, reload the image and region from disk, set the region into the image directly // from the data we save, re-fill and save again loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName); using (RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 0, CodecsLoadByteOrder.Bgr, 1, 1)) { // Create a region from the data we saved on disk System.IO.Stream regionStream2 = await Tools.AppLocalFolder.OpenStreamForReadAsync(regionFileName); regionStream2.Read(data, 0, (int)regionStream2.Length); using(region = new RasterRegion()) { region.SetData(data); // Set this region into the image image.SetRegion(null, region, RasterRegionCombineMode.Set); } // Fill the image with a color and save it to disk to show the region FillCommand cmd = new FillCommand(RasterColorHelper.FromKnownColor(RasterKnownColor.Yellow)); cmd.Run(image); StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName2); ILeadStream saveStream = LeadStreamFactory.Create(saveFile); await codecs.SaveAsync(image, saveStream, RasterImageFormat.Bmp, 24); } codecs.Dispose(); }