Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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);
        //conn.Open();
        //comm.ExecuteNonQuery();
        //conn.Close();
        //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));
        conn.Open();
        comm.ExecuteNonQuery();
        conn.Close();
 
    }
    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);
        }
        else
        {
            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)
    {
        AddPhotoToDatabase();
        labMessage.Text = "Saved Successfully";
    }
and I am displaying in repeater like:
<ItemTemplate>
 
                                                                           <asp:ImageButton  ID="ImageButton1"  class="vlightbox1" runat="server"  ImageUrl='<%#Eval("ThumbImage")  %>'
                                                                           NavigateUrl='<%#Eval("OriginalImage")  %>'
 
                        Height="100px" Width="100"  />
 

</ItemTemplate>
 
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
Edited 29-Apr-11 1:02am
v3

1 solution

Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 CPallini 350
1 Sergey Alexandrovich Kryukov 320
2 BillWoodruff 309
3 George Jonsson 274
4 OriginalGriff 237
0 OriginalGriff 4,903
1 CPallini 3,960
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,786
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 29 Apr 2011
Copyright © CodeProject, 1999-2014
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