By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,453 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

Out of memory error while trying to assing memorystream object to a bitmap object

P: n/a
Hello all

Am trying to debug an application written by someone else. The issue is we get an "Out of Memory" when executing the following code

Expand|Select|Wrap|Line Numbers
  1. Dim mImages(1000) As Image
  2. dr = cmd.ExecuteReader()
  3.                 While dr.Read
  4.                     mName = CStr(dr.GetValue(0))
  5.                     mAccount = CStr(dr.GetValue(1))
  6.                     mLocation = CStr(dr.GetValue(2))
  7.                     mdate = FormatDateTime(dr.GetValue(3))
  8.                     Dim bytBlobData(dr.GetBytes(4, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
  9.                     dr.GetBytes(4, 0, bytBlobData, 0, bytBlobData.Length)
  10.                     Dim stmBlobData As New MemoryStream(bytBlobData) 
  11.                     Dim MyBitmap As New Bitmap(stmBlobData) <<== Error in this line
  12.  
  13.                     Dim objStream As New MemoryStream
  14.                     Dim myEncoder As Encoder
  15.                     Dim myEncoderParameter As EncoderParameter
  16.                     Dim myEncoderParameters As EncoderParameters
  17.                     Dim myImageCodecInfo As ImageCodecInfo = GetEncoderInfo("image/jpeg")
  18.                     myEncoder = Encoder.Transformation
  19.                     myEncoderParameters = New EncoderParameters(1)
  20.                     myEncoderParameter = New EncoderParameter(myEncoder, i)
  21.                     myEncoderParameters.Param(0) = myEncoderParameter
  22.                     MyBitmap.Save(objStream, myImageCodecInfo, myEncoderParameters)
  23.                     mImages(i) = Drawing.Image.FromStream(objStream)
  24.                     Me.ilsImages.Images.Add(i, System.Drawing.Image.FromStream(objStream))
  25.                     Me.ToolStrip1.Items.Add(System.Drawing.Image.FromStream(objStream))
  26.                     Me.ToolStrip1.Items(i).Text = FormatDateTime(mdate, DateFormat.ShortDate)
  27.                     stmBlobData.Close()
  28.                     'objStream.Close()
  29.                     MyBitmap.Dispose()
  30.                     i = i + 1
  31.                 End While
  32.  
This happens mostly when the image is big. Can someone help me how to handle this issue? I tried searching everywhere but in vain.
(ilsImages is ImageList)

Thanks
AC
Oct 22 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.