Don,
Mystery solved!
Its how Bitmap.Save is implemented!
Based on the TraceStream class (below) Bitmap.Save writes rows from the end
of the file to the beginning of the file.
Try the following with the TraceStream class:
Dim bmp As Image
Dim ms As IO.MemoryStream
bmp = New System.Drawing.Bitmap("C:\2068.bmp")
ms = New IO.MemoryStream
Dim ts As New TraceStream(ms)
bmp.Save(ts, bmp.RawFormat)
Console.WriteLine("MemoryStream Position = " & ms.Position)
ms.Close()
Notice the initial SetLength and then how the Position decreases & one "row"
of bytes are written. Hence the Stream is left at the length of the header +
the length of one "row" in bytes, your 762 and not the about 30,000 I would
expect (based on your other message).
A quick TraceStream class, it writes trace information on selected
methods...
---x--- cut here ---x--- begin TraceStream.vb ---x---
Option Strict On
Option Explicit On
Imports System.IO
Public Class TraceStream
Inherits Stream
Private ReadOnly m_stream As Stream
Public Sub New(ByVal stream As Stream)
If stream Is Nothing Then Throw New ArgumentNullException("stream")
m_stream = stream
End Sub
Private Sub WriteTrace(ByVal format As String, ByVal ParamArray args()
As Object)
Dim message As String = String.Format(format, args)
Trace.WriteLine(message, "TraceStream")
End Sub
Public Overrides ReadOnly Property CanRead() As Boolean
Get
Return m_stream.CanRead
End Get
End Property
Public Overrides ReadOnly Property CanSeek() As Boolean
Get
Return m_stream.CanSeek
End Get
End Property
Public Overrides ReadOnly Property CanWrite() As Boolean
Get
Return m_stream.CanWrite
End Get
End Property
Public Overrides ReadOnly Property Length() As Long
Get
Return m_stream.Length
End Get
End Property
Public Overrides Property Position() As Long
Get
Return m_stream.Position
End Get
Set(ByVal value As Long)
WriteTrace("Position={0}", value)
m_stream.Position = value
End Set
End Property
Public Overrides Sub Flush()
WriteTrace("Flush")
m_stream.Flush()
End Sub
Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As
Integer, ByVal count As Integer) As Integer
WriteTrace("Read(buffer={0}, offset={1}, count={2})", buffer,
offset, count)
Return m_stream.Read(buffer, offset, count)
End Function
Public Overrides Function Seek(ByVal offset As Long, ByVal origin As
System.IO.SeekOrigin) As Long
WriteTrace("Seek(offset={0}, origin={1})", offset, origin)
Return m_stream.Seek(offset, origin)
End Function
Public Overrides Sub SetLength(ByVal value As Long)
WriteTrace("SetLength(value={0})", value)
m_stream.SetLength(value)
End Sub
Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As
Integer, ByVal count As Integer)
WriteTrace("Write(buffer={0}, offset={1}, count={2})", buffer,
offset, count)
m_stream.Write(buffer, offset, count)
End Sub
Public Overrides Sub WriteByte(ByVal value As Byte)
WriteTrace("WriteByte(value={0})", value)
m_stream.WriteByte(value)
End Sub
End Class
---x--- cut here ---x--- end TraceStream.vb ---x---
Hope this helps
Jay
| When I run the following code, the MemoryStream's Position is always set
to
| 762 instead of 0, which is what I would expect:
|
|
| Dim bmp As Image
| Dim ms As MemoryStream
|
| bmp = New System.Drawing.Bitmap("C:\2068.bmp")
| ms = New MemoryStream
| bmp.Save(ms, bmp.RawFormat)
| Console.WriteLine("MemoryStream Position = " & ms.Position)
| ms.Close()
|
|
| This is happening on two different computers for me. Does this happen to
| anyone else? Is there a reason why the memory stream's position is set to
| 762 instead of 0? Or is it proper practice to always manually set stream
| positions to zero when first creating them?
|
| - Don
|
|