Click here to Skip to main content
13,143,185 members (64,261 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Pls someone tell that i am able to insert thumbnail images in database but varbinary(max) datatype of image is not displaying in repeater control my code of inserting is
protected void AddPhotoToDatabase()
        byte[] OriginalPhoto = GetImage();
        byte[] Thumbnail = GenerateThumbnail();
        //string Title = FileUpload1.FileName.ToString();
        //string sql = "INSERT INTO [photogallery] ([UserId], [Title], [OriginalImage], [ThumbImage]) VALUES ('" + User.Identity.Name + "', '" + Title + "', CAST('" + OriginalPhoto + "'AS VARBINARY(MAX)), CAST('" + Thumbnail + "'AS VARBINARY(MAX)))";
        //string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
        //SqlConnection conn = new SqlConnection(strCon);
        //SqlCommand comm = new SqlCommand(sql, conn);
        //string sql = "INSERT INTO [photogallery] ([Title], [OriginalImage],[ThumbImage]) VALUES (@title, @originalImage, @thumbImage)";
        string sql = "INSERT INTO photogallery (Title, OriginalImage,ThumbImage) VALUES ('"+ TextBox1 .Text +"', @originalImage, @thumbImage)";
        string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["cn"].ConnectionString;
        SqlConnection conn = new SqlConnection(strCon);
        SqlCommand comm = new SqlCommand(sql, conn);
        //comm.Parameters.Add(new SqlParameter("@userId", User.Identity.Name)); 
        //comm.Parameters.Add(new SqlParameter("@title", Title));
        comm.Parameters.Add(new SqlParameter("@originalImage", OriginalPhoto));
        comm.Parameters.Add(new SqlParameter("@thumbImage", Thumbnail));
    protected byte[] GetImage()
        byte[] photo = new byte[FileUpload1.PostedFile.ContentLength];
        FileUpload1.PostedFile.InputStream.Read(photo, 0, photo.Length);
        return photo;
    protected byte[] GenerateThumbnail()
        System.Drawing.Image image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
        double thumbwidth = 0;
        double thumbheight = 0;
        double imgsz = 150.0;
        if (imgsz / image.Width < imgsz / image.Height)
            thumbwidth = image.Width * (imgsz / image.Width);
            thumbheight = image.Height * (imgsz / image.Width);
            thumbwidth = image.Width * (imgsz / image.Height);
            thumbheight = image.Height * (imgsz / image.Height);
        System.Drawing.Image thumb = image.GetThumbnailImage((int)thumbwidth, (int)thumbheight, delegate() { return false; }, (IntPtr)0);
        MemoryStream ms = new MemoryStream();
        thumb.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    protected void btnAdd_Click(object sender, EventArgs e)
        labMessage.Text = "Saved Successfully";

and I am displaying in repeater like:
                                                                           <asp:ImageButton  ID="ImageButton1"  class="vlightbox1" runat="server"  ImageUrl='<%#Eval("ThumbImage")  %>'
                                                                           NavigateUrl='<%#Eval("OriginalImage")  %>'
                        Height="100px" Width="100"  />


Pls someone tell why my images are not displaying for photo gallery of orignal image and thumbnail image, where is my mistake......
Posted 29-Apr-11 0:47am
Updated 29-Apr-11 1:02am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I don't think simply piling the byte[] of the image into the ImageUrl and NavigateUrl properties of the ImageButton will work. In fact, I'm certain it won't work. The last time I stored images in binary form in a database of any kind I needed to rig up an image viewer page (aspx) that read the byte[], wrote it to the OutputStream of the HttpResponse, then set the mimetype and stuff.

If I had to do it today I'd use an .ashx handler.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 29 Apr 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100