Blog: Tech Talk

agsdix-fas fa-home

Blog: Home

agsdix-fas fa-pen-fancy

Blog: CEO's Corner

agsdix-fas fa-code

Blog: Tech Talk

Blog: Product Releases

agsdix-fas fa-chalkboard-teacher

Blog: Viewing

Blog: Conversion

arrow left circle icon Blog: Tech Talk

Converting a Byte Array to Another Byte Array

by | Apr 26, 2012

When converting images, you may need to convert a byte array to another byte array. Snowbound Software’s RasterMaster Imaging SDK product for .NET lets you take an image, convert it to a byte array, and then save it as another byte array. This can help you when converting files from one format to another.

The code sample below explains how to convert a byte array to another byte array by saving and reading an image from memory:


‘PROGRAM: MemsaveForm.vb

‘PURPOSE: Demonstrates how to use the SnowBound Library for saving to and

‘ reading image from memory.

‘COMMENTS: Select an image from the open dialog box.

‘Copyright (c) 2005, SnowBound Software.

‘This is example code for all SnowBound customers to freely copy and
‘use however they wish.


‘ Snowbnd() – Init Snowbound .Net component.

‘ clearImage() – Remove image from memory.

‘ CIMG_decompress_bitmap() – Load an image from disk.

‘ CIMG_save_bitmap_mem() – Save image to memory in desired
‘ format.

‘ CIMG_display_bitmap() – Display current image.

‘ CIMG_decompress_bitmap_mem() – Read image from memory.


Imports System.IO

Imports SbdNetLib

Public Class Form1

Inherits System.Windows.Forms.Form

# Region “Windows Form Designer generated code “

Public Sub New()


‘This call is required by the Windows Form Designer.


‘Add any initialization after the InitializeComponent() call

End Sub

‘Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose( ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then


End If

End If


End Sub

‘Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

Private simg As Snowbnd

Private fileName As String

Private iLoadedFlag As Integer

Private status As Integer

Private arrImageSave As Byte()

Private iImgLength As Integer

‘NOTE: The following procedure is required by the Windows Form

‘It can be modified using the Windows Form Designer.

‘Do not modify it using the code editor.

Friend WithEvents btnLoad As System.Windows.Forms.Button

Friend WithEvents btnLoadMem As System.Windows.Forms.Button

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents panImage As System.Windows.Forms.Panel

Friend WithEvents ofImageDialog As System.Windows.Forms.OpenFileDialog

Private Sub InitializeComponent()

Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager( GetType(Form1))

Me.btnLoad = New System.Windows.Forms.Button

Me.btnLoadMem = New System.Windows.Forms.Button

Me.Label1 = New System.Windows.Forms.Label

Me.panImage = New System.Windows.Forms.Panel

Me.ofImageDialog = New System.Windows.Forms.OpenFileDialog



Me.btnLoad.Font = New System.Drawing.Font(“Microsoft Sans Serif”, 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.btnLoad.Location = New System.Drawing.Point(168, 24)

Me.btnLoad.Name = “btnLoad”

Me.btnLoad.Size = New System.Drawing.Size(168, 32)

Me.btnLoad.TabIndex = 0

Me.btnLoad.Text = “Load Image File”


Me.btnLoadMem.Font = New System.Drawing.Font(“Microsoft Sans Serif”, 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.btnLoadMem.Location = New System.Drawing.Point(344, 24)

Me.btnLoadMem.Name = “btnLoadMem”

Me.btnLoadMem.Size = New System.Drawing.Size(168, 32)

Me.btnLoadMem.TabIndex = 1

Me.btnLoadMem.Text = “Get Image From Memory”


Me.Label1.Image = CType(resources.GetObject(“Label1.Image”), System.Drawing.Image)

Me.Label1.Location = New System.Drawing.Point(24, 16)

Me.Label1.Name = “Label1”

Me.Label1.Size = New System.Drawing.Size(136, 40)

Me.Label1.TabIndex = 2


Me.panImage.Location = New System.Drawing.Point(24, 64)

Me.panImage.Name = “panImage”

Me.panImage.Size = New System.Drawing.Size(512, 320)

Me.panImage.TabIndex = 3


Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(680, 422)

Me.Controls.Add( Me.panImage)

Me .Controls.Add( Me.Label1)

Me .Controls.Add( Me.btnLoadMem)

Me.Controls.Add( Me.btnLoad)

Me.Name = “Form1”

Me.Text = “Form1”

Me.ResumeLayout( False)

End Sub

# End Region

Private Sub btnLoad_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click

ofImageDialog.Filter = “Image files (*.bmp)|*.tif|All files (*.*)|*.*”

ofImageDialog.FilterIndex = 2

ofImageDialog.RestoreDirectory = True

If (ofImageDialog.ShowDialog() = DialogResult.OK) Then

iLoadedFlag = 0

fileName = ofImageDialog.FileName

Dim fs As FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read)

iImgLength = fs.Length

Dim arrImageData(iImgLength) As Byte

fs.Read(arrImageData, 0, fs.Length)


arrImageSave = ConvertFile(arrImageData)

End If

End Sub

Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

simg = New Snowbnd

End Sub

Private Sub btnLoadMem_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadMem.Click


iImgLength = iImgLength * 4 + 10000

status = simg.CIMGLOW_set_alias(4)

status = simg.CIMG_decompress_bitmap_mem(arrImageSave, 0)

If (status >= 0) Then

Dim sdibHeader As CDIB_HEADER

sdibHeader = New CDIB_HEADER

status = simg.CIMG_bitmap_info(sdibHeader)

If (sdibHeader.biHeight <> 0) Then

panImage.Width = panImage.Height * sdibHeader.biWidth / sdibHeader.biHeight

End If

iLoadedFlag = 1


End If

End Sub

Private Sub panImage_Paint( ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles panImage.Paint

If (status >= 0) And (iLoadedFlag = 1) Then simg.CIMG_display_bitmap(e.Graphics, 0, 0, panImage.Width, panImage.Height)

End Sub

Public Function ConvertFile( ByVal arrOrigFile As Byte()) As Byte()

Dim iPageNumber As Integer, status As Integer

Dim simage As New SbdNetLib.Snowbnd

Dim arrNewfile(5000000) As Byte

‘ Snowbound : declare some new stuff

Dim iSize As Integer

Dim header As New SbdNetLib.CDIB_HEADER


‘sb.Append(Now().ToString & ” – Staring Convert File” & ControlChars.CrLf)

Dim intFiles As Integer = 0

Dim intTotalPages As Double = 0

Dim intArraySize As Integer = 0


Dim strExt As String = “PDF”

‘Dim strFileDes As String = strConvDir

‘strSaveImage = strFileDes & intFiles & “.pdf”

iPageNumber = simage.CIMGLOW_get_pages_mem(arrOrigFile)

status = simage.CIMGLOW_set_document_input(300, 24)

status = simage.CIMG_decompress_bitmap_mem(arrOrigFile, 0)

‘ Snowbound : get header info to determine size of array


iSize = header.biSizeImage * iPageNumber

‘ Snowbound: declare array only as big as needed

ReDim arrNewfile(iSize)

Array.Clear(arrNewfile, 0, iSize)

‘Save first page

intArraySize += simage.CIMG_save_bitmap_mem(arrNewfile, 59)

intTotalPages += iPageNumber

If (iPageNumber < 1) Then iPageNumber = 1

‘Save the rest pages

For i As Integer = 1 To iPageNumber – 1


‘status = simage.CIMGLOW_set_document_input(300, 24)

status = simage.CIMG_decompress_bitmap_mem(arrOrigFile, i)

intArraySize += simage.CIMG_save_bitmap_mem(arrNewfile, 59)


Next i

‘ Snowbound: don’t need this array anymore

‘Dim arrRetrunFile(intArraySize – 1) As Byte

‘For j As Integer = 0 To intArraySize – 1

‘ arrRetrunFile(j) = arrNewfile(j)


‘arrNewfile.Copy(arrNewfile, arrRetrunFile, intArraySize – 1)

‘Return arrRetrunFile

‘ Snowbound: return correct array

‘ReDim it as the exact size saved.

ReDim Preserve arrNewfile(intArraySize)

Return arrNewfile

Catch ex As Exception

arrNewfile = Nothing



‘arrNewfile = Nothing

‘sb.Append(Now().ToString & ” – Ending Convert File” & ControlChars.CrLf)

End Try

End Function

End Class