在ASP.NET中的SQL中,图像未显示在varbinary(Max)数据类型的转发器中 [英] Images are not display in repeater of varbinary(Max) datatype in sql in asp.net

查看:79
本文介绍了在ASP.NET中的SQL中,图像未显示在varbinary(Max)数据类型的转发器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我我可以在数据库中插入缩略图,但是图像的varbinary(max)数据类型未显示在中继器控件中,我的插入代码是

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......

推荐答案

我不知道''认为仅将图像的byte[]堆积到ImageButtonImageUrlNavigateUrl属性中将起作用.实际上,我敢肯定它不会起作用.我上次以二进制形式将图像存储在任何类型的数据库中时,都需要装配一个读取byte[]的图像查看器页面(aspx),将其写入HttpResponseOutputStream,然后设置mimetype和东西.

如果今天必须这样做,我将使用.ashx处理程序.
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.


这篇关于在ASP.NET中的SQL中,图像未显示在varbinary(Max)数据类型的转发器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆