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.WriteLi ne("MemoryStrea m 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 13 2853
Don,
| Does this happen to
| anyone else?
Yes
| Is there a reason why the memory stream's position is set to
| 762 instead of 0?
Yes, as I suspect that the bmp.Save statement wrote 762 bytes of information
to the stream.
| bmp.Save(ms, bmp.RawFormat)
| Console.WriteLi ne("MemoryStrea m Position = " & ms.Position)
Seeing as Image.Save is writing information to the stream, I would expect
the stream's position to actually move, so when I write more information to
the stream the stream is positioned to a "writable" position...
| Or is it proper practice to always manually set stream
| positions to zero when first creating them?
No, I don't set the position when first creating them.
Hope this helps
Jay
"Don" <un*****@oblivi on.com> wrote in message
news:kwCye.1872 153$6l.739353@p d7tw2no...
| 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.WriteLi ne("MemoryStrea m 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
|
|
"Don" <un*****@oblivi on.com> schrieb: bmp = New System.Drawing. Bitmap("C:\2068 .bmp") ms = New MemoryStream bmp.Save(ms, bmp.RawFormat) Console.WriteLi ne("MemoryStrea m 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?
I assume it's caused by writing the data to the stream. By writing data the
input position will be incremented by the number of bytes written to the
stream.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
I've tried this with two different sized images, both about 30K in size, and
the Position was initialized to 762 in both cases. Using a hex editor I
determined that this was exactly 762 bytes into the actual image. It seems
like an arbitrary position in the middle of the stream.
- Don
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:ua******** ******@TK2MSFTN GP14.phx.gbl... "Don" <un*****@oblivi on.com> schrieb: bmp = New System.Drawing. Bitmap("C:\2068 .bmp") ms = New MemoryStream bmp.Save(ms, bmp.RawFormat) Console.WriteLi ne("MemoryStrea m 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? I assume it's caused by writing the data to the stream. By writing data
the input position will be incremented by the number of bytes written to the stream.
-- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/>
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:OS******** ******@TK2MSFTN GP10.phx.gbl... Don, | Does this happen to | anyone else? Yes
Have you tried it? What were the results?
| Is there a reason why the memory stream's position is set to | 762 instead of 0? Yes, as I suspect that the bmp.Save statement wrote 762 bytes of information to the stream.
Nope. The image that was loaded into the memory stream was about 30,000
bytes. | bmp.Save(ms, bmp.RawFormat) | Console.WriteLi ne("MemoryStrea m Position = " & ms.Position)
Seeing as Image.Save is writing information to the stream, I would expect the stream's position to actually move, so when I write more information to the stream the stream is positioned to a "writable" position...
That would make sense if the Position also equalled the length of the image
stored into the stream, but it doesn't.
| Or is it proper practice to always manually set stream | positions to zero when first creating them?
No, I don't set the position when first creating them.
I didn't do this either, at first, and it always resulted in a corrupt image
when I tried to make one from the stream because the first 762 bytes were
chopped off.
- Don
Don,
I only can say that I don't do the way you do.
Have a look at this sample that I made yesterday for somebody else.
\\\Needs two pictureboxes and a button on a form
Private Sub Form1_Load(ByVa l sender As _
System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Dim fo As New OpenFileDialog
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(f o.FileName, _
IO.FileMode.Ope n)
Dim br As New IO.BinaryReader (fs)
Dim abyt As Byte()
abyt = br.ReadBytes(CI nt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream (abyt)
Me.PictureBox1. Image = Image.FromStrea m(ms)
End If
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click
Dim abyt As Byte()
Dim ms As New IO.MemoryStream
PictureBox1.Ima ge.Save(ms, Imaging.ImageFo rmat.Bmp)
abyt = ms.GetBuffer
Dim ms2 As New IO.MemoryStream (abyt)
Me.PictureBox2. Image = Image.FromStrea m(ms2)
Me.PictureBox1. Image = Nothing
End Sub
////
I hope this helps a little bit?
Cor
Don,
| Have you tried it? What were the results?
I have written to a memory stream lots of times, each time I write to it,
the Position property moved the number of bytes that I wrote. Ergo the point
of my yes!
I have not specifically used the Image.Save method.
| Nope. The image that was loaded into the memory stream was about 30,000
| bytes.
Then obviously ms.Position needs to be about 30,000 then doesn't it! My
point is that ms.Position will not be zero as you wrote data to the Stream.
| That would make sense if the Position also equalled the length of the
image
| stored into the stream, but it doesn't.
Ah! There's the rub! I would expect Position should equal the length of the
image file. Oddly enough ms.Length equals the length of the image file.
Have you looked at what is actually in the memory stream? Is only the first
762 bytes of the image written? Has only the image header been written,
instead of the image header & actual bit information?
Interesting enough if start with a jpeg or save to a jpeg:
| > | bmp.Save(ms, Imaging.ImageFo rmat.Jpeg)
| > | Console.WriteLi ne("MemoryStrea m Position = " & ms.Position)
Then ms.Position & ms.Length do match, so it seems to me that writing a
Bitmap has some special quirk to it, unfortunately I don't bitmaps as much
as I use Jpegs. Have you tried writing to a FileStream instead of a
MemoryStream? Do you have the same problem? I see the same results with a
FileStream or MemoryStream, which suggests the "bug" aka quirk is really in
the Image.Save method when using ImageFormat.Bmp . A quick (very quick)
search of support.microso ft.com has not offered any suggestions.
Have you considered asking "down the hall" in the
microsoft.publi c.dotnet.framew ork.drawing newsgroup about any special quirks
on saving a Bitmap (.BMP) file?
Hope this helps
Jay
"Don" <un*****@oblivi on.com> wrote in message
news:PwQye.1876 332$6l.505694@p d7tw2no...
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
| news:OS******** ******@TK2MSFTN GP10.phx.gbl...
| > Don,
| > | Does this happen to
| > | anyone else?
| > Yes
|
| Have you tried it? What were the results?
|
|
| > | Is there a reason why the memory stream's position is set to
| > | 762 instead of 0?
| > Yes, as I suspect that the bmp.Save statement wrote 762 bytes of
| > information to the stream.
|
| Nope. The image that was loaded into the memory stream was about 30,000
| bytes.
|
|
| >
| > | bmp.Save(ms, bmp.RawFormat)
| > | Console.WriteLi ne("MemoryStrea m Position = " & ms.Position)
| >
| > Seeing as Image.Save is writing information to the stream, I would
| > expect the stream's position to actually move, so when I write more
| > information to the stream the stream is positioned to a "writable"
| > position...
|
| That would make sense if the Position also equalled the length of the
image
| stored into the stream, but it doesn't.
|
|
| > | Or is it proper practice to always manually set stream
| > | positions to zero when first creating them?
| >
| > No, I don't set the position when first creating them.
|
| I didn't do this either, at first, and it always resulted in a corrupt
image
| when I tried to make one from the stream because the first 762 bytes were
| chopped off.
|
| - Don
|
|
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.WriteLi ne("MemoryStrea m 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 ArgumentNullExc eption("stream" )
m_stream = stream
End Sub
Private Sub WriteTrace(ByVa l format As String, ByVal ParamArray args()
As Object)
Dim message As String = String.Format(f ormat, args)
Trace.WriteLine (message, "TraceStrea m")
End Sub
Public Overrides ReadOnly Property CanRead() As Boolean
Get
Return m_stream.CanRea d
End Get
End Property
Public Overrides ReadOnly Property CanSeek() As Boolean
Get
Return m_stream.CanSee k
End Get
End Property
Public Overrides ReadOnly Property CanWrite() As Boolean
Get
Return m_stream.CanWri te
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.Positi on
End Get
Set(ByVal value As Long)
WriteTrace("Pos ition={0}", value)
m_stream.Positi on = value
End Set
End Property
Public Overrides Sub Flush()
WriteTrace("Flu sh")
m_stream.Flush( )
End Sub
Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As
Integer, ByVal count As Integer) As Integer
WriteTrace("Rea d(buffer={0}, offset={1}, count={2})", buffer,
offset, count)
Return m_stream.Read(b uffer, offset, count)
End Function
Public Overrides Function Seek(ByVal offset As Long, ByVal origin As
System.IO.SeekO rigin) As Long
WriteTrace("See k(offset={0}, origin={1})", offset, origin)
Return m_stream.Seek(o ffset, origin)
End Function
Public Overrides Sub SetLength(ByVal value As Long)
WriteTrace("Set Length(value={0 })", value)
m_stream.SetLen gth(value)
End Sub
Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As
Integer, ByVal count As Integer)
WriteTrace("Wri te(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("Wri teByte(value={0 })", value)
m_stream.WriteB yte(value)
End Sub
End Class
---x--- cut here ---x--- end TraceStream.vb ---x---
Hope this helps
Jay
"Don" <un*****@oblivi on.com> wrote in message
news:kwCye.1872 153$6l.739353@p d7tw2no...
| 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.WriteLi ne("MemoryStrea m 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
|
|
Cor Ligthert,
many thanks!!
your code is wonderful..^^
but I can't still solve this problem..
Save Image in SQL DB ----------------------------------
Dim picturedata as byte()
Dim ms As New IO.MemoryStream
picturebox1.Ima ge.Save(ms, System.Drawing. Imaging.ImageFo rmat.Jpeg)
picturedata = ms.GetBuffer
....SQL process...
Dim myCommand As New SqlCommand(myIn sertQuery, myConnection)
myCommand.Comma ndText = "InsertData "
Dim myParm1 As SqlParameter = myCommand.Param eters.Add("@ima ge",
SqlDbType.image )
myParm1.Value = picturedata
Load image -----------------------------------------------
.....
if myreader.Read()
Dim picutredata as byte()
picturedata = myreader("image ")
Dim ms as New IO.Memorystream (picturedata)
pictureBox1.ima ge = image.Fromstrea m(ms) ---> argument error
I don't know what is a problem.. could you help me?
Joe
"Cor Ligthert [MVP]" wrote: Don,
I only can say that I don't do the way you do.
Have a look at this sample that I made yesterday for somebody else.
\\\Needs two pictureboxes and a button on a form Private Sub Form1_Load(ByVa l sender As _ System.Object, ByVal e As System.EventArg s) Handles MyBase.Load Dim fo As New OpenFileDialog If fo.ShowDialog = DialogResult.OK Then Dim fs As New IO.FileStream(f o.FileName, _ IO.FileMode.Ope n) Dim br As New IO.BinaryReader (fs) Dim abyt As Byte() abyt = br.ReadBytes(CI nt(fs.Length)) br.Close() 'just to show the sample without a fileread error Dim ms As New IO.MemoryStream (abyt) Me.PictureBox1. Image = Image.FromStrea m(ms) End If End Sub Private Sub Button1_Click(B yVal sender As System.Object, _ ByVal e As System.EventArg s) Handles Button1.Click Dim abyt As Byte() Dim ms As New IO.MemoryStream PictureBox1.Ima ge.Save(ms, Imaging.ImageFo rmat.Bmp) abyt = ms.GetBuffer Dim ms2 As New IO.MemoryStream (abyt) Me.PictureBox2. Image = Image.FromStrea m(ms2) Me.PictureBox1. Image = Nothing End Sub //// I hope this helps a little bit?
Cor
Joe,
I had to make this sample the day before yesterday, because I have a
standard sample for database hanlding in which I saw than that I had removed
the transforming from an image directly. However for your database handling
you can look at the original sample.
Try to look all around things handling this sampling as writting to disk
from the image and dataset. (I will probably make a more compact one from
this in short future). http://groups-beta.google.com/group/...29590856?hl=en
I hope this helps,
Cor This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: xmlguy |
last post by:
PREVIOUS BACKGROUND POST:
I am trying to reuse a Memory Stream for loading and
transforming the xml it contains
Basically I have defined following interfaces:
Class Render
{
|
by: Arcnet |
last post by:
Using MemoryStream
I Have a problem to create a new Image from byte array that originaly
was created from an older image
(Everything is being preformed in the same Thread)
//Get Image From File
byte arr;
|
by: Nicolas |
last post by:
Hi Everybody, I'm working with the MemoryStream and I'm having
problems with the ReadBytes method. When I use it, this method never
returns bytes!!!!
MemoryStream sw = new MemoryStream();
sw.Write(System.Text.UnicodeEncoding.Unicode.GetBytes("Testing"),0,6);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
byte buffer = new byte;
byte result = sw.GetBuffer();
int a = sw.Read(buffer,2,4);
|
by: SQLScott |
last post by:
For some reason I am drawing a blank on this, so I would be extremely
greatful if someone could help me out.
I am trying to get a MemoryStream out of a Byte array. How do I get it back
out?
I have a sub that calls a function:
Dim bByte() As Byte
bByte = ws.SQLGetUserDemographics()
|
by: Asaf |
last post by:
Hi,
I am trying to Compress & Decompress a DataSet.
When running this code I am receiving the error:
System.Xml.XmlException was unhandled
Message="Root element is missing."
Source="System.Xml"
The code:
| |
by: Heron |
last post by:
Hi,
Could someone explain me why the following code doesn't work? The
memorystream always remains with length 0.
MemoryStream input = new MemoryStream();
|
by: |
last post by:
Hi all,
I want to create a method that does the following:
1) Programmatically instantiate a new XmlDataSource control
2) For each file in a named directory, make a "FileSystemItem" element
3) On each FileSystemItem Element, make two child nodes, one with the file
name, one with the file size. ie.
<filesystemitems>
<filesystemitem>
|
by: =?Utf-8?B?UGhpbCBKb2huc29u?= |
last post by:
Hi,
I am using dotnet remoting with a binarry formatter.
I have a property that returns a memorystream that has had a file loaded
into it.
When I try to access this property though I get an error regarding "the
proxy has no channel sink.......or no suitable Client channel to talk to
the server."
|
by: =?Utf-8?B?VmljdG9y?= |
last post by:
Hi,
Could you tell me can I keep the MemoryStream open and "close" the
BinaryReader?
As the MemoryStream is used for buffering the TCP data and BinaryReader is
only used to read the MemoryStream, so I don't want to close the MemoryStream.
Do I really need to close the BinaryReader if the underlying backing store
needs not to be closed?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |