在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[]
堆积到ImageButton
的ImageUrl
和NavigateUrl
属性中将起作用.实际上,我敢肯定它不会起作用.我上次以二进制形式将图像存储在任何类型的数据库中时,都需要装配一个读取byte[]
的图像查看器页面(aspx),将其写入HttpResponse
的OutputStream
,然后设置mimetype和东西.
如果今天必须这样做,我将使用.ashx
处理程序.
I don''t think simply piling thebyte[]
of the image into theImageUrl
andNavigateUrl
properties of theImageButton
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 thebyte[]
, wrote it to theOutputStream
of theHttpResponse
, then set the mimetype and stuff.
If I had to do it today I''d use an.ashx
handler.