Link to home
Create AccountLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

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))=@par_id", conn);
                if (sv_userid != null)
                {
                    cmd.Parameters.Add("@par_id", SqlDbType.VarChar).Value = sv_userid.Trim();
                }
                else
                {
                    cmd.Parameters.Add("@par_id", 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?
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

It seems you might be looking for a way to copy the image data from server side to client side 'as is', but I don't think you can in that way.

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:
aspImagePreview.Attributes["src"] = "data:image/jpg;base64," + Convert.ToBase64String(bArray);

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:
            aspImagePreview.Attributes["src"] = "data:image/jpg;base64," + Convert.ToBase64String(GetThumbBytes(img));
        }

        public byte[] GetThumbBytes(System.Drawing.Image img) {
            System.Drawing.Image thumbnail = img.GetThumbnailImage(290, 190, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallBack), IntPtr.Zero);
            MemoryStream thumbStream = new MemoryStream();
            thumbnail.Save(thumbStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] bArrayThumb = new Byte[thumbStream.Length];
            thumbStream.Position = 0;
            thumbStream.Read(bArrayThumb, 0, (int)thumbStream.Length);
            return bArrayThumb;
        }

        public bool ThumbnailCallBack() {
            return true;
        }

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:
<img id="aspImagePreview" src="<%= GetThumb() %>" alt="Preview" height="190" width="290"/>

Open in new window

Avatar of Peter Chan

ASKER

Thanks a lot Robert. How can I adjust my own codes above to save the image into the disk?
SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
html could then simply be something like:
<img id="aspImagePreview" alt="Preview" height="190" width="290" src="tmp/output.jpg" />

Open in new window

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...
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))=@par_id", conn);
                if (sv_userid != null)
                {
                    cmd.Parameters.Add("@par_id", SqlDbType.VarChar).Value = sv_userid.Trim();
                }
                else
                {
                    cmd.Parameters.Add("@par_id", SqlDbType.VarChar).Value = "";
                }
                byte[] bArray = (byte[])cmd.ExecuteScalar();
                using (FileStream fw = new FileStream(Path.Combine(Server.MapPath("~/"), "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).
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.