Solved

Get the Length of an Image Created using Memory Stream

Posted on 2014-04-21
7
104 Views
Last Modified: 2015-03-11
Hello Experts!

OK, so I need to get the Length of an Image created from a Memory Stream:
        Dim memstrm As IO.MemoryStream = New IO.MemoryStream
        Dim ticket As New Bitmap(1312, 574)

        Dim ticketGraphics As Graphics = Graphics.FromImage(ticket)
        ticketGraphics.FillRectangle(Brushes.White, 0, 0, 1312, 574)
        ticket.Save(memstrm, System.Drawing.Imaging.ImageFormat.Tiff)

When I do memstrm.Length this is NOT the Length I need.
The Length I need is 14584

I get the 14584 when I open the file using a FileStream...but how can I get this length so I don't have to save the file then open again?

Mark
0
Comment
Question by:smithmrk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 20

Expert Comment

by:Russ Suter
ID: 40012844
So what length are you getting?

You may need to call the Flush() method before requesting the length.
0
 

Author Comment

by:smithmrk
ID: 40012855
I just tried the Flush that didn't seem to work.
I'm getting 50168

Mark
0
 
LVL 40
ID: 40013011
Is there something that your are not showing us. The following gives me exactly the same reading, 4926 and 4926:

		Dim memstrm As IO.MemoryStream = New IO.MemoryStream
		Dim filestrm As IO.FileStream = New IO.FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Test.tiff", FileMode.Create)
		Dim ticket As New Bitmap(1312, 574)

		Dim ticketGraphics As Graphics = Graphics.FromImage(ticket)
		ticketGraphics.FillRectangle(Brushes.White, 0, 0, 1312, 574)
		ticket.Save(memstrm, System.Drawing.Imaging.ImageFormat.Tiff)
		ticket.Save(filestrm, Imaging.ImageFormat.Tiff)
		filestrm.Close()

		Debug.WriteLine(memstrm.Length)
		Debug.WriteLine(New IO.FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Test.tiff").Length)
		memstrm.Close()

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:smithmrk
ID: 40013069
Do it this way and you will see what I mean:

    Private Sub Test()
        Dim memstrm As IO.MemoryStream = New IO.MemoryStream
        Dim ticket As New Bitmap(1312, 574)

        Dim ticketGraphics As Graphics = Graphics.FromImage(ticket)
        ticketGraphics.FillRectangle(Brushes.White, 0, 0, 1312, 574)
        ticket.Save(memstrm, System.Drawing.Imaging.ImageFormat.Tiff)
        ticket.Save("C:\Images\Mark.tif")

        MessageBox.Show(memstrm.Length)
        MessageBox.Show(New IO.FileStream("C:\Images\Mark.tif", IO.FileMode.Open).Length)
    End Sub

Mark
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40013110
Your are saving directly through Bitmap. Not that I went through a filestream to do the job.

Compression can be different depending on the method you use to save the file. And some methods of saving a bitmap will had extra information.

In my example, I explicitely saved through a Stream both for the memory stream and the file stream, to make sure that the same algorithm was used.
0
 

Author Comment

by:smithmrk
ID: 40013133
OK, so I can't get the 14584 Length unless I save it?

I don't want to have to save it open it get the 14584 then close it and delete it.
That is a lot of steps just to get the 14584 number.

I'm creating images on the fly and need to save them all in a Stacked Image when done.
Once the Stacked Image is done I will have an XML file with all the offsets and lengths to find the image with in the stacked image.

I don't want to have to write out a bunch images then loop through them all to get the lenghts and create a stacked image from all the indivdual images, then delete them all.

Mark
0
 

Author Closing Comment

by:smithmrk
ID: 40658962
OK, I figured it out!

Thanks,
Mark
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question