Save images to memorystream?

  • Thread starter Thread starter Stupid48
  • Start date Start date
S

Stupid48

I'm writing a simple screen capture program that is using the Bitmap
class to save the capture. I would like to do multiple captures and
save them as we go along. I know that I could save them to a
temporary folder on the filesystem but is there a way to save them to
memory (i.e. memorystream) and, if so, how do I reference them later
(C# or VB.Net is fine)?

Thanks, Chris
 
I'm writing a simple screen capture program that is using the Bitmap
class to save the capture. I would like to do multiple captures and
save them as we go along. I know that I could save them to a
temporary folder on the filesystem but is there a way to save them to
memory (i.e. memorystream) and, if so, how do I reference them later
(C# or VB.Net is fine)?

Have you looked at the Image.Save() method that takes a Stream as a
parameter?
http://msdn2.microsoft.com/en-us/library/ms142147.aspx

You would create the MemoryStream, save to it, and then use
Image.FromStream() later to reconstitute the image.

That said, if you just want to keep the images in memory, why not just do
that? What's the point of stuffing it into a MemoryStream if all you're
going to do is read it back out again? Just keep your Image (Bitmap)
reference around, and use it as needed.

Pete
 
Have you looked at the Image.Save() method that takes a Stream as a
parameter?http://msdn2.microsoft.com/en-us/library/ms142147.aspx

You would create the MemoryStream, save to it, and then use
Image.FromStream() later to reconstitute the image.

That said, if you just want to keep the images in memory, why not just do
that? What's the point of stuffing it into a MemoryStream if all you're
going to do is read it back out again? Just keep your Image (Bitmap)
reference around, and use it as needed.

Pete

I see your point. I would like to not have to save it at all. There
is a possibility that there will be multiple bitmaps captured one
after another. I guess I just don't know how to save all of the
references to them and be able to access them later by some sort of
index. My function is:

' Grab the selected piece of the image.
Dim rect As New Rectangle( _
Min(m_X1, m_X2), Min(m_Y1, m_Y2), _
Abs(m_X1 - m_X2), Abs(m_Y1 - m_Y2))
Dim bm As Bitmap = DirectCast( _
m_bmDesktop.Clone(rect, m_bmDesktop.PixelFormat), Bitmap)

' Save the image into the file.
bm.Save(file_name, ImageFormat.Jpeg)

So, instead of bm.save, I would like to create some sort of collection
that I can reference later. Can someone give me a pointer on how to
do that?

Thanks, Chris
 
[...]
So, instead of bm.save, I would like to create some sort of collection
that I can reference later. Can someone give me a pointer on how to
do that?

How about an array? There are a number of potential solutions that are
either arrays or are array-like, all very basic data structures that
should be known to anyone attempting to write any useful program. Or put
another way, if you want to write useful programs, it sounds as though you
may want to educate yourself a little more on basic data structures common
to all programming environments.

As far as your specific question goes, some examples of data structures
that would be useful to you:

Dim arrayOfBitmaps As List(Of Bitmap)
Dim arrayOfBitmaps As ArrayList
Dim arrayOfBitmaps() As Bitmap

Any of those variable declarations will result in a variable named
"arrayOfBitmaps" in which you can store an arbitrary number of Bitmap
instances. With the first two, you can add new elements using an Add()
method each of the classes has. I believe with the third, you would need
to use ReDim() to resize the array as new items are added, if you did not
intially create an array large enough for all of your items.

Hope that helps.

Pete
 
[...]
So, instead of bm.save, I would like to create some sort of collection
that I can reference later. Can someone give me a pointer on how to
do that?

How about an array? There are a number of potential solutions that are
either arrays or are array-like, all very basic data structures that
should be known to anyone attempting to write any useful program. Or put
another way, if you want to write useful programs, it sounds as though you
may want to educate yourself a little more on basic data structures common
to all programming environments.

As far as your specific question goes, some examples of data structures
that would be useful to you:

Dim arrayOfBitmaps As List(Of Bitmap)
Dim arrayOfBitmaps As ArrayList
Dim arrayOfBitmaps() As Bitmap

Any of those variable declarations will result in a variable named
"arrayOfBitmaps" in which you can store an arbitrary number of Bitmap
instances. With the first two, you can add new elements using an Add()
method each of the classes has. I believe with the third, you would need
to use ReDim() to resize the array as new items are added, if you did not
intially create an array large enough for all of your items.

Hope that helps.

Pete

Thanks for the info. I didn't know that one could put these datatypes
in an array. I guess being a novice, I have a lot to learn but I
guess we all have to start somewhere....
 
Thanks for the info. I didn't know that one could put these datatypes
in an array. I guess being a novice, I have a lot to learn but I
guess we all have to start somewhere....

Yup, that's true. We did all start somewhere, and we did all have a
moment when we had to learn what you can put into an array. This just
happened to be yours. :)
 
Back
Top