By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,549 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,549 IT Pros & Developers. It's quick & easy.

Can images be saved to Session State and retrieved?

P: n/a
Hello -

I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#) is
there any way to temporarily save an image to a session object, and after
running some other operations, later retrieve the image from the session
object, convert it back to an image, and re-save it to the database?

Thanks?
Nov 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Why not? Check out the Bitmap class.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hello -

I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#)
is there any way to temporarily save an image to a session object, and
after running some other operations, later retrieve the image from the
session object, convert it back to an image, and re-save it to the
database?

Thanks?


Nov 19 '05 #2

P: n/a
Hi,

Why not covert the Image to Binary and then store it in session with the Img
format?
Is this a bad idea?

++Vijay R
"Brock Allen" wrote:
Why not? Check out the Bitmap class.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hello -

I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#)
is there any way to temporarily save an image to a session object, and
after running some other operations, later retrieve the image from the
session object, convert it back to an image, and re-save it to the
database?

Thanks?


Nov 19 '05 #3

P: n/a
Thanks Vijay,

As simple as this might sound, it's taking me forever. Could you pls point
me to some sample code?

The sample code i find, requires that i know the size and file type of the
image (bitmap) in the database, which i might not... I don't seem to find a
sample which is as "simple" as your solution seems to imply.

thanks

"Vijay" wrote:
Hi,

Why not covert the Image to Binary and then store it in session with the Img
format?
Is this a bad idea?

++Vijay R
"Brock Allen" wrote:
Why not? Check out the Bitmap class.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hello -

I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#)
is there any way to temporarily save an image to a session object, and
after running some other operations, later retrieve the image from the
session object, convert it back to an image, and re-save it to the
database?

Thanks?


Nov 19 '05 #4

P: n/a
Thanks Brock,

The bitmap class requires that i know that width and height of the image, to
first declare and instantiate the bitmap. Can this be done w/out know this
info? And, i'm also quite new at this, can you point me to some examples. I
have not found an example which enables me to save to session state and
retrieve. Only to save to file system... or retreive via stream with all of
the information coming from the stream object, which is difficult for me to
manipulate into an example that's right for me.

thanks,

"Brock Allen" wrote:
Why not? Check out the Bitmap class.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hello -

I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#)
is there any way to temporarily save an image to a session object, and
after running some other operations, later retrieve the image from the
session object, convert it back to an image, and re-save it to the
database?

Thanks?


Nov 19 '05 #5

P: n/a
Hi,

I think this should work just for converting the bitmap to a byte stream:
--------------------------------------------------------------------
FileStream Fs = new
FileStream(TextBox2.Text.Trim(),FileMode.Open,File Access.Read);
byte[] RawData = new byte[Fs.Length];

Fs.Read(RawData,0,Convert.ToInt32(Fs.Length));
Fs.Close();

Session.Add("FileData",RawData);
-------------------------------------------------------------------

Now assuming that the file format is always bmp, we have to work on
converting it back into bmp.
will work on this :-)

++Vijay R
"charliewest" wrote:
Thanks Vijay,

As simple as this might sound, it's taking me forever. Could you pls point
me to some sample code?

The sample code i find, requires that i know the size and file type of the
image (bitmap) in the database, which i might not... I don't seem to find a
sample which is as "simple" as your solution seems to imply.

thanks

"Vijay" wrote:
Hi,

Why not covert the Image to Binary and then store it in session with the Img
format?
Is this a bad idea?

++Vijay R
"Brock Allen" wrote:
Why not? Check out the Bitmap class.

-Brock
DevelopMentor
http://staff.develop.com/ballen

> Hello -
>
> I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#)
> is there any way to temporarily save an image to a session object, and
> after running some other operations, later retrieve the image from the
> session object, convert it back to an image, and re-save it to the
> database?
>
> Thanks?
>

Nov 19 '05 #6

P: n/a
Hi Charlie,

I wanted to ask,
How is the bitmap stored in the database? as a Blob or in bytes?
if it is stored in bytes, then I feel that Blob/Image is the better option.

you can insert the byte array as I mentioned before in the Image field of
the database.
For retrieving and displaying the image, put the byte array into a stream
and then create a bitmap file from the stream.

----------------------------------------------------------------------
MemoryStream stream = new MemoryStream(RawData, true);
stream.Write(RawData, 0, RawData.Length);

Bitmap bmp = new Bitmap(stream);
stream.close();
---------------------------------------------------------------------

Hope this helped

++Vijay R
"Vijay" wrote:
Hi,

I think this should work just for converting the bitmap to a byte stream:
--------------------------------------------------------------------
FileStream Fs = new
FileStream(TextBox2.Text.Trim(),FileMode.Open,File Access.Read);
byte[] RawData = new byte[Fs.Length];

Fs.Read(RawData,0,Convert.ToInt32(Fs.Length));
Fs.Close();

Session.Add("FileData",RawData);
-------------------------------------------------------------------

Now assuming that the file format is always bmp, we have to work on
converting it back into bmp.
will work on this :-)

++Vijay R
"charliewest" wrote:
Thanks Vijay,

As simple as this might sound, it's taking me forever. Could you pls point
me to some sample code?

The sample code i find, requires that i know the size and file type of the
image (bitmap) in the database, which i might not... I don't seem to find a
sample which is as "simple" as your solution seems to imply.

thanks

"Vijay" wrote:
Hi,

Why not covert the Image to Binary and then store it in session with the Img
format?
Is this a bad idea?

++Vijay R
"Brock Allen" wrote:

> Why not? Check out the Bitmap class.
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
> > Hello -
> >
> > I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#)
> > is there any way to temporarily save an image to a session object, and
> > after running some other operations, later retrieve the image from the
> > session object, convert it back to an image, and re-save it to the
> > database?
> >
> > Thanks?
> >
>
>
>
>

Nov 19 '05 #7

P: n/a
Vijay,

First, i really appreciate your help sticking with this. I think i'm almost
there, but the examples i find (and that you provide) assume i pull the image
directly from the Web Form, and save the image to the Session State, and
then, pull the image from the session state. Where and how does the database
fit in?

I've been trying to do the following with zero luck:

byte[] RawData = new
byte[Convert.ToInt32(ds.Tables["Client"].Rows[0]["ImageSize"])];
MemoryStream stream = new MemoryStream(ds.Tables["Client"].Rows[0]["Image"],
FileMode.Open, FileAccess.Read);
Fs.Read(RawData, 0, Convert.ToInt32(Fs.Length));
Fs.Close();
Session["ImageByte"] = RawData;

And then....

Byte[] RawData = new byte[Convert.ToInt32(Session["ImageSize"].ToString())];
MemoryStream stream = new MemoryStream(RawData, true);
stream.Write(RawData, 0, RawData.Length);
Bitmap bmp = new Bitmap(stream);
stream.Close();

This is really going nowhere... i really am not catching how this conversion
between images, bytes and bitmaps is working....

If you could provide any insight on how the image is somehow saved to the a
session state var directly from the database, and how this is read from a
session state var... (which i think you've already provide in a former
response) that would be frankly amazing...

thanks,

"Vijay" wrote:
Hi Charlie,

I wanted to ask,
How is the bitmap stored in the database? as a Blob or in bytes?
if it is stored in bytes, then I feel that Blob/Image is the better option.

you can insert the byte array as I mentioned before in the Image field of
the database.
For retrieving and displaying the image, put the byte array into a stream
and then create a bitmap file from the stream.

----------------------------------------------------------------------
MemoryStream stream = new MemoryStream(RawData, true);
stream.Write(RawData, 0, RawData.Length);

Bitmap bmp = new Bitmap(stream);
stream.close();
---------------------------------------------------------------------

Hope this helped

++Vijay R
"Vijay" wrote:
Hi,

I think this should work just for converting the bitmap to a byte stream:
--------------------------------------------------------------------
FileStream Fs = new
FileStream(TextBox2.Text.Trim(),FileMode.Open,File Access.Read);
byte[] RawData = new byte[Fs.Length];

Fs.Read(RawData,0,Convert.ToInt32(Fs.Length));
Fs.Close();

Session.Add("FileData",RawData);
-------------------------------------------------------------------

Now assuming that the file format is always bmp, we have to work on
converting it back into bmp.
will work on this :-)

++Vijay R
"charliewest" wrote:
Thanks Vijay,

As simple as this might sound, it's taking me forever. Could you pls point
me to some sample code?

The sample code i find, requires that i know the size and file type of the
image (bitmap) in the database, which i might not... I don't seem to find a
sample which is as "simple" as your solution seems to imply.

thanks

"Vijay" wrote:

> Hi,
>
> Why not covert the Image to Binary and then store it in session with the Img
> format?
> Is this a bad idea?
>
> ++Vijay R
>
>
> "Brock Allen" wrote:
>
> > Why not? Check out the Bitmap class.
> >
> > -Brock
> > DevelopMentor
> > http://staff.develop.com/ballen
> >
> >
> >
> > > Hello -
> > >
> > > I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#)
> > > is there any way to temporarily save an image to a session object, and
> > > after running some other operations, later retrieve the image from the
> > > session object, convert it back to an image, and re-save it to the
> > > database?
> > >
> > > Thanks?
> > >
> >
> >
> >
> >

Nov 19 '05 #8

P: n/a
Hi Charlie,

Okay I thought I had explained that, must have missed it somewhere.
here is the code to retrieve the image from the database.
Assuming that the column in the database table is defined as Image [image]
NULL in the table "Table1".

---------------------------------------------------------------------------------------------
//Retrieve Image from database
string strCmd = String.Format("SELECT Image FROM Table1 WHERE id = '1'",
SqlCommand Sqlcmd = new SqlCommand(strCmd, sqlConn);

byte[] bytImg = (byte[])Sqlcmd.ExecuteScalar();

//Store retrieved image in session

Session.Add("RawData",bytImg)

//Creating memory stream with the raw image data
System.IO.MemoryStream MemStrm = new System.IO.MemoryStream(bytImg, true);
MemStrm .Write(bytImg, 0, bytImg.Length);

//creating bitmap with the memory stream
Bitmap bmp = new Bitmap(MemStrm);
MemStrm.close();

------------------------------------------------------------------------------------------------
Hope this helped.

Regarding your code, I think you are missing something.
Firstly you created a byte array "RawData" filled it with the contents of
the dataset, where the col name was Imagesize. ->Now why is the image size a
byte? and why is it stored in the database? (okay giving you that you need
the image size)
Secondly you created a memory stream from another col of the dataset which
seems to be the Image. ->the image if stored in the database (in bytes)
should be retrieved to the byte array and then put into a memory stream.
Next you have used the file stream to read the "Rawdata" which now contains
the Image Size. -> why do you need a file stream to read the byte array? also
the byte array contains the image size!
lastly you are storing the image size (RawData) in the session ->assuming
you need the file size in the session, so what happened to the image in byte
form which was to be stored in the session? and which was in the memory
stream?

I understand what you want, but your code seems wrong or probably I am
missing something.
anyway try the code I gave you.
--
--Vijay R
"charliewest" wrote:
Vijay,

First, i really appreciate your help sticking with this. I think i'm almost
there, but the examples i find (and that you provide) assume i pull the image
directly from the Web Form, and save the image to the Session State, and
then, pull the image from the session state. Where and how does the database
fit in?

I've been trying to do the following with zero luck:

byte[] RawData = new
byte[Convert.ToInt32(ds.Tables["Client"].Rows[0]["ImageSize"])];
MemoryStream stream = new MemoryStream(ds.Tables["Client"].Rows[0]["Image"],
FileMode.Open, FileAccess.Read);
Fs.Read(RawData, 0, Convert.ToInt32(Fs.Length));
Fs.Close();
Session["ImageByte"] = RawData;

And then....

Byte[] RawData = new byte[Convert.ToInt32(Session["ImageSize"].ToString())];
MemoryStream stream = new MemoryStream(RawData, true);
stream.Write(RawData, 0, RawData.Length);
Bitmap bmp = new Bitmap(stream);
stream.Close();

This is really going nowhere... i really am not catching how this conversion
between images, bytes and bitmaps is working....

If you could provide any insight on how the image is somehow saved to the a
session state var directly from the database, and how this is read from a
session state var... (which i think you've already provide in a former
response) that would be frankly amazing...

thanks,

"Vijay" wrote:
Hi Charlie,

I wanted to ask,
How is the bitmap stored in the database? as a Blob or in bytes?
if it is stored in bytes, then I feel that Blob/Image is the better option.

you can insert the byte array as I mentioned before in the Image field of
the database.
For retrieving and displaying the image, put the byte array into a stream
and then create a bitmap file from the stream.

----------------------------------------------------------------------
MemoryStream stream = new MemoryStream(RawData, true);
stream.Write(RawData, 0, RawData.Length);

Bitmap bmp = new Bitmap(stream);
stream.close();
---------------------------------------------------------------------

Hope this helped

++Vijay R
"Vijay" wrote:
Hi,

I think this should work just for converting the bitmap to a byte stream:
--------------------------------------------------------------------
FileStream Fs = new
FileStream(TextBox2.Text.Trim(),FileMode.Open,File Access.Read);
byte[] RawData = new byte[Fs.Length];

Fs.Read(RawData,0,Convert.ToInt32(Fs.Length));
Fs.Close();

Session.Add("FileData",RawData);
-------------------------------------------------------------------

Now assuming that the file format is always bmp, we have to work on
converting it back into bmp.
will work on this :-)

++Vijay R
"charliewest" wrote:

> Thanks Vijay,
>
> As simple as this might sound, it's taking me forever. Could you pls point
> me to some sample code?
>
> The sample code i find, requires that i know the size and file type of the
> image (bitmap) in the database, which i might not... I don't seem to find a
> sample which is as "simple" as your solution seems to imply.
>
> thanks
>
> "Vijay" wrote:
>
> > Hi,
> >
> > Why not covert the Image to Binary and then store it in session with the Img
> > format?
> > Is this a bad idea?
> >
> > ++Vijay R
> >
> >
> > "Brock Allen" wrote:
> >
> > > Why not? Check out the Bitmap class.
> > >
> > > -Brock
> > > DevelopMentor
> > > http://staff.develop.com/ballen
> > >
> > >
> > >
> > > > Hello -
> > > >
> > > > I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#)
> > > > is there any way to temporarily save an image to a session object, and
> > > > after running some other operations, later retrieve the image from the
> > > > session object, convert it back to an image, and re-save it to the
> > > > database?
> > > >
> > > > Thanks?
> > > >
> > >
> > >
> > >
> > >

Nov 19 '05 #9

P: n/a
Vijay,

First, thanks a ton for your help. This has not been fun i know. I've
managed to figure it out thanks to you. I don't know what i was trying to do
before so your comments are all well taken, but i've just followed what
you've suggested. There's still one thing i cannot do, but it's no longer an
issue: I can now pull the image from the db, and save to the session state,
as well as convert to a bitmap. But i still cannot pull the image from the
session state object and convert to bitmap. there's clearly something that
i'm missing... here's how my code that works looks:

//Retrieve Image from database
string strCmd = String.Format("SELECT Image FROM mClient WHERE (ClientId = "
+ Session["iClientId"].ToString() + ")");
using (SqlConnection cn = utils.GetConnection())
{
cn.Open();
SqlCommand Sqlcmd = new SqlCommand(strCmd, cn);
byte[] bytImg = (byte[])Sqlcmd.ExecuteScalar();

//Creating memory stream with the raw image data
System.IO.MemoryStream MemStrm = new System.IO.MemoryStream(bytImg, true);
MemStrm.Write(bytImg, 0, bytImg.Length);

//creating bitmap with the memory stream
Bitmap bmp = new Bitmap(MemStrm);
MemStrm.Close();

myData = bytImg;

cn.Close();
}

Thanks agian!

"Vijay" wrote:
Hi Charlie,

Okay I thought I had explained that, must have missed it somewhere.
here is the code to retrieve the image from the database.
Assuming that the column in the database table is defined as Image [image]
NULL in the table "Table1".

---------------------------------------------------------------------------------------------
//Retrieve Image from database
string strCmd = String.Format("SELECT Image FROM Table1 WHERE id = '1'",
SqlCommand Sqlcmd = new SqlCommand(strCmd, sqlConn);

byte[] bytImg = (byte[])Sqlcmd.ExecuteScalar();

//Store retrieved image in session

Session.Add("RawData",bytImg)

//Creating memory stream with the raw image data
System.IO.MemoryStream MemStrm = new System.IO.MemoryStream(bytImg, true);
MemStrm .Write(bytImg, 0, bytImg.Length);

//creating bitmap with the memory stream
Bitmap bmp = new Bitmap(MemStrm);
MemStrm.close();

------------------------------------------------------------------------------------------------
Hope this helped.

Regarding your code, I think you are missing something.
Firstly you created a byte array "RawData" filled it with the contents of
the dataset, where the col name was Imagesize. ->Now why is the image size a
byte? and why is it stored in the database? (okay giving you that you need
the image size)
Secondly you created a memory stream from another col of the dataset which
seems to be the Image. ->the image if stored in the database (in bytes)
should be retrieved to the byte array and then put into a memory stream.
Next you have used the file stream to read the "Rawdata" which now contains
the Image Size. -> why do you need a file stream to read the byte array? also
the byte array contains the image size!
lastly you are storing the image size (RawData) in the session ->assuming
you need the file size in the session, so what happened to the image in byte
form which was to be stored in the session? and which was in the memory
stream?

I understand what you want, but your code seems wrong or probably I am
missing something.
anyway try the code I gave you.
--
--Vijay R
"charliewest" wrote:
Vijay,

First, i really appreciate your help sticking with this. I think i'm almost
there, but the examples i find (and that you provide) assume i pull the image
directly from the Web Form, and save the image to the Session State, and
then, pull the image from the session state. Where and how does the database
fit in?

I've been trying to do the following with zero luck:

byte[] RawData = new
byte[Convert.ToInt32(ds.Tables["Client"].Rows[0]["ImageSize"])];
MemoryStream stream = new MemoryStream(ds.Tables["Client"].Rows[0]["Image"],
FileMode.Open, FileAccess.Read);
Fs.Read(RawData, 0, Convert.ToInt32(Fs.Length));
Fs.Close();
Session["ImageByte"] = RawData;

And then....

Byte[] RawData = new byte[Convert.ToInt32(Session["ImageSize"].ToString())];
MemoryStream stream = new MemoryStream(RawData, true);
stream.Write(RawData, 0, RawData.Length);
Bitmap bmp = new Bitmap(stream);
stream.Close();

This is really going nowhere... i really am not catching how this conversion
between images, bytes and bitmaps is working....

If you could provide any insight on how the image is somehow saved to the a
session state var directly from the database, and how this is read from a
session state var... (which i think you've already provide in a former
response) that would be frankly amazing...

thanks,

"Vijay" wrote:
Hi Charlie,

I wanted to ask,
How is the bitmap stored in the database? as a Blob or in bytes?
if it is stored in bytes, then I feel that Blob/Image is the better option.

you can insert the byte array as I mentioned before in the Image field of
the database.
For retrieving and displaying the image, put the byte array into a stream
and then create a bitmap file from the stream.

----------------------------------------------------------------------
MemoryStream stream = new MemoryStream(RawData, true);
stream.Write(RawData, 0, RawData.Length);

Bitmap bmp = new Bitmap(stream);
stream.close();
---------------------------------------------------------------------

Hope this helped

++Vijay R
"Vijay" wrote:

> Hi,
>
> I think this should work just for converting the bitmap to a byte stream:
> --------------------------------------------------------------------
> FileStream Fs = new
> FileStream(TextBox2.Text.Trim(),FileMode.Open,File Access.Read);
> byte[] RawData = new byte[Fs.Length];
>
> Fs.Read(RawData,0,Convert.ToInt32(Fs.Length));
> Fs.Close();
>
> Session.Add("FileData",RawData);
> -------------------------------------------------------------------
>
> Now assuming that the file format is always bmp, we have to work on
> converting it back into bmp.
> will work on this :-)
>
> ++Vijay R
>
>
> "charliewest" wrote:
>
> > Thanks Vijay,
> >
> > As simple as this might sound, it's taking me forever. Could you pls point
> > me to some sample code?
> >
> > The sample code i find, requires that i know the size and file type of the
> > image (bitmap) in the database, which i might not... I don't seem to find a
> > sample which is as "simple" as your solution seems to imply.
> >
> > thanks
> >
> > "Vijay" wrote:
> >
> > > Hi,
> > >
> > > Why not covert the Image to Binary and then store it in session with the Img
> > > format?
> > > Is this a bad idea?
> > >
> > > ++Vijay R
> > >
> > >
> > > "Brock Allen" wrote:
> > >
> > > > Why not? Check out the Bitmap class.
> > > >
> > > > -Brock
> > > > DevelopMentor
> > > > http://staff.develop.com/ballen
> > > >
> > > >
> > > >
> > > > > Hello -
> > > > >
> > > > > I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#)
> > > > > is there any way to temporarily save an image to a session object, and
> > > > > after running some other operations, later retrieve the image from the
> > > > > session object, convert it back to an image, and re-save it to the
> > > > > database?
> > > > >
> > > > > Thanks?
> > > > >
> > > >
> > > >
> > > >
> > > >

Nov 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.