I am working on a feature called "En-Masse Encryption", and I am running into some problems. The first one is that if I put the following code to a button's click event, it successfully works:

Private Sub EnMasseEncryptor()

Dim timeone = Nothing, timetwo As DateTime = Nothing

Dim s As System.IO.StreamReader = Nothing

timeone = Now

Dim a As Image = Nothing

For Each listedFile As String In ListBox1.Items

Select Case Path.GetExtension(listedFile)

Case ".jpg", "png", ".tiff", ".bmp", ".emp", ".exif", ".wmf", "ico"

a = Image.FromFile(listedFile)

ImageEncryption.ImageEncryptor(listedFile, a.RawFormat, a)


Case ".doc", ".txt", ".docx"

s = New System.IO.StreamReader(listedFile)

Dim str As String = s.ReadToEnd()

TextEncryption.TextEncryptor(listedFile + ".get", str)


Case Else

MessageBox.Show("The following file couldn't be encrypted: " & listedFile, "Error in Encrypting a File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

End Select


Dim ts As TimeSpan

timetwo = Now

ts = timetwo.Subtract(timeone)

TSLEnMasseEncryption.Text = ("Process completed succefully in " + ts.TotalSeconds.ToString())

End Sub

Public Sub ImageEncryptor(ByVal fileName As String, ByVal imageFormat As System.Drawing.Imaging.ImageFormat, ByVal imageToEncrypt As Image)

Dim fs As FileStream = Nothing

Dim cs As CryptoStream = Nothing


fs = New FileStream(fileName + ".gei", FileMode.Create)

cs = New CryptoStream(fs, KeyGenerate.symmetricAlgo.CreateEncryptor, CryptoStreamMode.Write)

Catch ex As Exception


End Try


imageToEncrypt.Save(cs, imageToEncrypt.RawFormat)

Catch ex As Exception





End Try

End Sub

The main limitation of this is that it often throws the OutOfMemoryException, even though I called the Dispose() mehod. So, I thought I will put it in a BackGroundWorker component, however, it now generates a NullReferenceException in the ImageEncryptor method. I don't understand. Could you help, please Also, is there a way to limit the memory usage of a program Thank you very much! Smile

Re: Visual Basic Express Edition When I use BackGroundWorker, it throws an exception

Tall Dude

The 'out of memory' error is being generated when a file that

cannot be read as an image gets loaded by your code.

a = Image.FromFile(listedFile)

Some of your allowed extensions may not be supported by VB

or some of them have corrupted data.

Re: Visual Basic Express Edition When I use BackGroundWorker, it throws an exception


Ohh, I see. Thanks, Tall Dude.