Re: Can images be saved to Session State and retrieved?
- From: "Vijay" <VijayR@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 7 Aug 2005 23:37:22 -0700
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,FileAccess.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?
> > > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
.
- Follow-Ups:
- Re: Can images be saved to Session State and retrieved?
- From: charliewest
- Re: Can images be saved to Session State and retrieved?
- References:
- Can images be saved to Session State and retrieved?
- From: charliewest
- Re: Can images be saved to Session State and retrieved?
- From: Brock Allen
- Re: Can images be saved to Session State and retrieved?
- From: Vijay
- Re: Can images be saved to Session State and retrieved?
- From: charliewest
- Re: Can images be saved to Session State and retrieved?
- From: Vijay
- Re: Can images be saved to Session State and retrieved?
- From: Vijay
- Re: Can images be saved to Session State and retrieved?
- From: charliewest
- Can images be saved to Session State and retrieved?
- Prev by Date: How to set a property in a user control
- Next by Date: I cannot run the puzzle sample
- Previous by thread: Re: Can images be saved to Session State and retrieved?
- Next by thread: Re: Can images be saved to Session State and retrieved?
- Index(es):
Relevant Pages
|