We help IT Professionals succeed at work.
Get Started

Memory Question

smithmrk
smithmrk asked
on
182 Views
Last Modified: 2014-05-07
OK, I need to understand something because I'm confused!

I have this loop here that adds images to a ArrayList from Memory.
Then I loop through the ArrayList to create a Multipage TIFF File.

What I don't understand is if I close the MemoryStream after I create each image it still adds the images to the ArrayList...but when I go to read them I get an error that the Stream is closed?

This WORKS:
For icount As Int16 = 0 To 2
            Dim Ticket As New Bitmap(1312, 574)
            Dim ticketGraphics As Graphics = Graphics.FromImage(Ticket)
            Dim MyFont As New Font("Arial", 20, FontStyle.Regular)
            Dim memstrm As IO.MemoryStream = New IO.MemoryStream

            ticketGraphics.DrawString("-- Test Ticket --", MyFont, Brushes.Black, 520, 90)
            ticketGraphics.DrawString(icount.ToString, MyFont, Brushes.Black, 520, 120)
            Ticket.Save(memstrm, System.Drawing.Imaging.ImageFormat.Tiff)

            MultipageImages.Add(memstrm)
            memstrm.Flush()
        Next

This DOES NOT WORK after I add them all to the ArrayList:
For icount As Int16 = 0 To 2
            Dim Ticket As New Bitmap(1312, 574)
            Dim ticketGraphics As Graphics = Graphics.FromImage(Ticket)
            Dim MyFont As New Font("Arial", 20, FontStyle.Regular)
            Dim memstrm As IO.MemoryStream = New IO.MemoryStream

            ticketGraphics.DrawString("-- Test Ticket --", MyFont, Brushes.Black, 520, 90)
            ticketGraphics.DrawString(icount.ToString, MyFont, Brushes.Black, 520, 120)
            Ticket.Save(memstrm, System.Drawing.Imaging.ImageFormat.Tiff)

            MultipageImages.Add(memstrm)
            memstrm.Flush()
           memstrm.Close()
       Next

Private Sub CreateMultiPage_ImageFile()
        ImageWriter = New IO.FileStream("C:\MemoryTest.tif", FileMode.Create)

        Dim saveEncoder As Encoder
        Dim compressionEncoder As Encoder
        Dim SaveEncodeParam As EncoderParameter
        Dim EncoderParams As New EncoderParameters(1)
        Dim codecInfo As ImageCodecInfo = getCodecForString("TIFF")

        saveEncoder = Encoder.SaveFlag
        compressionEncoder = Encoder.Compression

        ' Save the first page:.
        SaveEncodeParam = New EncoderParameter(saveEncoder, CLng(EncoderValue.MultiFrame))
        EncoderParams.Param(0) = SaveEncodeParam

        Dim TicketImage As Image = Image.FromStream(DirectCast(MultipageImages(0), Stream))
       TicketImage.Save(ImageWriter, codecInfo, EncoderParams)

        For iCount As Int16 = 1 To MultipageImages.Count - 1

            ' Save the next set of pages:
            SaveEncodeParam = New EncoderParameter(saveEncoder, CLng(EncoderValue.FrameDimensionPage))
            EncoderParams.Param(0) = SaveEncodeParam

            Dim NextImage As Image = Image.FromStream(DirectCast(MultipageImages(iCount), Stream))
            TicketImage.SaveAdd(NextImage, EncoderParams)
        Next

        SaveEncodeParam = New EncoderParameter(saveEncoder, CLng(EncoderValue.Flush))
        EncoderParams.Param(0) = SaveEncodeParam
        TicketImage.SaveAdd(EncoderParams)

        MultipageImages.Clear()
        MultipageImages = Nothing
        ImageWriter.Close()
        ImageWriter.Dispose()
    End Sub

The error occurs when trying to load the Image From the ArrayList into an Image...but it works fine if I don't close the Memory Stream after each image...why?

Just trying to manage the memory to the best to not overload it.

Thanks,
Mark
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE