Peter Chan
asked on
To assign the image
Hi,
I've retrieved the image column from the table using these
SqlCommand cmd = new SqlCommand("SELECT photo_file FROM tab2 where LTRIM(RTRIM(user_abbr))=@p ar_id", conn);
if (sv_userid != null)
{
cmd.Parameters.Add("@par_i d", SqlDbType.VarChar).Value = sv_userid.Trim();
}
else
{
cmd.Parameters.Add("@par_i d", SqlDbType.VarChar).Value = "";
}
byte[] bArray = (byte[])cmd.ExecuteScalar( );
MemoryStream fs = new MemoryStream(bArray);
System.Drawing.Image img = new System.Drawing.Bitmap(fs);
...
and I have one image on the page like
<asp:Image ID="aspImagePreview" runat="server" AlternateText="Preview" Height="190px" Width="290px" />
...
How can I assign the retrieved image to the Image item on the page, using VS 2012?
I've retrieved the image column from the table using these
SqlCommand cmd = new SqlCommand("SELECT photo_file FROM tab2 where LTRIM(RTRIM(user_abbr))=@p
if (sv_userid != null)
{
cmd.Parameters.Add("@par_i
}
else
{
cmd.Parameters.Add("@par_i
}
byte[] bArray = (byte[])cmd.ExecuteScalar(
MemoryStream fs = new MemoryStream(bArray);
System.Drawing.Image img = new System.Drawing.Bitmap(fs);
...
and I have one image on the page like
<asp:Image ID="aspImagePreview" runat="server" AlternateText="Preview" Height="190px" Width="290px" />
...
How can I assign the retrieved image to the Image item on the page, using VS 2012?
ASKER
Thanks a lot Robert. How can I adjust my own codes above to save the image into the disk?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
html could then simply be something like:
<img id="aspImagePreview" alt="Preview" height="190" width="290" src="tmp/output.jpg" />
In the previous post, I used 'tmp' directory because you will probably have to set write permissions for web user(s) to that directory. Also you may want to think of a solution to delete the files after some period or maybe when session ends but never tried that...
ASKER
Thanks a lot.
I have this Image item on the page
<asp:Image ID="aspImagePreview" runat="server" AlternateText="Preview" Height="190px" Width="290px" src="~/output.jpg" />
...
and I have these codes within Page_Load
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT photo_file FROM tab2 where LTRIM(RTRIM(user_abbr))=@p ar_id", conn);
if (sv_userid != null)
{
cmd.Parameters.Add("@par_i d", SqlDbType.VarChar).Value = sv_userid.Trim();
}
else
{
cmd.Parameters.Add("@par_i d", SqlDbType.VarChar).Value = "";
}
byte[] bArray = (byte[])cmd.ExecuteScalar( );
using (FileStream fw = new FileStream(Path.Combine(Se rver.MapPa th("~/"), "output.jpg"), FileMode.Create))
{
fw.Write(bArray, 0, bArray.Length);
}
...
but when running the deployed project, I don't see the Image is displayed there, while for the relevant record, there is really one Jpg file inside and I am able to display it by other ways (directly from the database).
I have this Image item on the page
<asp:Image ID="aspImagePreview" runat="server" AlternateText="Preview" Height="190px" Width="290px" src="~/output.jpg" />
...
and I have these codes within Page_Load
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT photo_file FROM tab2 where LTRIM(RTRIM(user_abbr))=@p
if (sv_userid != null)
{
cmd.Parameters.Add("@par_i
}
else
{
cmd.Parameters.Add("@par_i
}
byte[] bArray = (byte[])cmd.ExecuteScalar(
using (FileStream fw = new FileStream(Path.Combine(Se
{
fw.Write(bArray, 0, bArray.Length);
}
...
but when running the deployed project, I don't see the Image is displayed there, while for the relevant record, there is really one Jpg file inside and I am able to display it by other ways (directly from the database).
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I see 2 options:
1) use the retrieved byte array to show a (thumbnail) image using a data uri.
2) save the data to disk and assign the src of the image to that.
For the 1st option, you could use:
Open in new window
If the pictures are big (bigger than the 290x190 you're showing) then it would be beneficial to generate a thumbnail image:Open in new window
you can use a regular img instead of server side by the way, just have a function returning the 'src' string and in the html something like:Open in new window