seroiusly there must be an easier way to convert a filestream object into a memory stream i'm amazed by how many examples i'm seeing that require us to manually create a fixed length TEMP buffer and move data from one stream to the other.
i thought there was some reason like your suggestion. I noticed the Stream and FileStream class does not have the ToArray() method, while the memory stream does.
if i'm using an OpenFileDialog to grab a file (ie. a Stream or FileStream) how can i convert that into a MemoryStream, so i can access the ToArray() method easily, then As u said, i would love to utilise the memorystream to hand the block copy of bytes.
public static SaveImage( string name, byte[] imageData ) { //use the web.config to store the connection string SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]); SqlCommand command = new SqlCommand( "INSERT INTO Image(name, data) VALUES (@name, @data)", connection );
You can open and stream and block copy the bytes that you have readed to an array, but this is a lot easier! All the growing of the byte array is done by the MemoryStream class.
store image
David N.4117
Well its not exactlty hard is it Just a few lines of code.
You basically taking all the bytes from one and placing it into the other stream, doing it in chunks is easier on memory usage etc.
dgVisioscopie
Thanks my dear
I am asking ALLA (God)
to give you what you want
Alex Yakushev
seroiusly there must be an easier way to convert a filestream object into a memory stream i'm amazed by how many examples i'm seeing that require us to manually create a fixed length TEMP buffer and move data from one stream to the other.
i'm simply amazed, i a bad way :(
John P. Nelson
i thought there was some reason like your suggestion. I noticed the Stream and FileStream class does not have the ToArray() method, while the memory stream does.
if i'm using an OpenFileDialog to grab a file (ie. a Stream or FileStream) how can i convert that into a MemoryStream, so i can access the ToArray() method easily, then As u said, i would love to utilise the memorystream to hand the block copy of bytes.
KluchCode
Peter Koller
Image image = Image.FromFile(@"c:\myimage.bmp");
using(MemoryStream stream = new MemoryStream())
{
image.Save( stream );
byte[] buffer = steam.ToByteArray();
SaveImage( "myimage", buffer );
}
public static SaveImage( string name, byte[] imageData )
{
//use the web.config to store the connection string
SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]);
SqlCommand command = new SqlCommand( "INSERT INTO Image(name, data) VALUES (@name, @data)", connection );
SqlParameter pName = new SqlParameter( "@name", SqlDbType.VarChar,50 );
pName.Value = name;
command.Parameters.Add( pName );
SqlParameter pData = new SqlParameter( "@data", SqlDbType.Image );
pData.Value = imageData;
command.Parameters.Add( pData );
try
{
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
}
finally
{
connection.Close();
}
}
Nima_DK
Frank X. Huang
R.X.
Amen...
grsmith
int iLength = 2048;
byte[] buffer = new byte[iLength];
MemoryStream stream = new MemoryStream();
using(FileStream objFileIn = new FileStream(sFileName,FileMode.Open))
{
iLength = objFileIn.Read(buffer, 0, buffer.Length)
while (iLength != 0)
{
stream.Write(buffer, 0, iLength);
iLength = objFileIn.Read(buffer, 0, buffer.Length)
}
objFileIn.Close();
}
Replace sFileName with the path given by the dialog...
Mosha
PJ, can u elaborate on why you have used a memory stream in the following, as opposed to just a System.IO.Stream
Image image = Image.FromFile(@"c:\myimage.bmp");
using(MemoryStream stream = new MemoryStream())
{
image.Save( stream );
byte[] buffer = stream.ToByteArray();
SaveImage( "myimage", buffer );
}
is the memory stream the only way to do ' .ToByteArray()' Also, why do u SaveImage - what does that method do
note: i updated the byte[] line - it had a typo.