检索varbinary数据c#到picturebox
本文关键字:picturebox 数据 varbinary 检索 | 更新日期: 2023-09-27 18:15:53
大家好,我有一个问题与我的代码,我保存图像为varbinary ok,但我希望图像显示在一个图片框时,从一个文件上传选择,但我不断得到一个错误,是可能的吗?我正在使用c#和asp.net。我一直在搜索,但我似乎不能得到代码工作在我的源代码。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
public partial class Mensagem : System.Web.UI.Page
{
protected void BtnUploadClick(object sender, EventArgs e)
{
string filePath = Server.MapPath(upload1.FileName);
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(upload1.PostedFile.FileName);
Stream fs = upload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
string dt = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss");
SqlConnection conn = new SqlConnection("Data Source=Microsoft SQL Server (SqlClient);Server=NEVETS-LAPTOP''NEVETS;Initial Catalog=Forum;uid=sa;pwd=sql;Connect Timeout=10;TrustServerCertificate=True ");
conn.Open();
SqlCommand sqlCommand = conn.CreateCommand();
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandText = "usp_inserirFicheiro";
sqlCommand.Parameters.AddWithValue("@nome", SqlDbType.VarChar).Value = filename;
sqlCommand.Parameters.AddWithValue("@tamanho", SqlDbType.VarBinary).Value = bytes;
sqlCommand.Parameters.AddWithValue("@formato", SqlDbType.VarChar).Value = ext;
sqlCommand.Parameters.AddWithValue("@data", SqlDbType.DateTime).Value = dt;
sqlCommand.Parameters.AddWithValue("@ficheiro", SqlDbType.VarBinary).Value = br;
sqlCommand.ExecuteNonQuery();
txtnome.Text = "sucesso";
conn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
//upload1.SaveAs(Server.MapPath("~/imgs/" + upload1.FileName).ToString());
//pbficheiro.ImageUrl = @"~/imgs/" + upload1.FileName;
Byte[] ba = new BinaryReader(upload1.PostedFile.InputStream).ReadBytes((Int32)upload1.PostedFile.InputStream.Length);
using (MemoryStream ms = new MemoryStream(ba, false))
{
Image imgTmp = Image.FromStream(ms);
Bitmap bm = new Bitmap(imgTmp.Width, imgTmp.Height);
Graphics g = Graphics.FromImage(bm);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
Rectangle rect = new Rectangle(0, 0, imgTmp.Width, imgTmp.Height);
g.DrawImage(imgTmp, rect, 0, 0, imgTmp.Width, imgTmp.Height, GraphicsUnit.Pixel);
using (MemoryStream ms2 = new MemoryStream())
{
bm.Save(ms2, imgTmp.RawFormat);
bm.Dispose();
imgTmp.Dispose();
if (WantAnImage)
return Image.FromStream(ms2);
else (WantBytes)
ms2.ToArray();}
}
}
}
如果错误与内存有关,那么很可能是因为当此方法退出时,图像数据正在与Stream对象一起处理。尝试将其捕获到中间位图对象中,并将该位图分配给PictureBox图像属性。
这里有一个链接可能会有所帮助:
将图像保存到MemoryStream- Generic GDI+ Error
最终,它被引用为信息源:
http://msdn.microsoft.com/en-us/library/z7ha67kw 编辑:我不知道"upload1"是什么类型或它的范围,但假设它是一个本地对象,我想知道这种方法(这将是一个方法,你会调用检索字节[]或图像):
protected void Button2_Click(object sender, EventArgs e)
{
//upload1.SaveAs(Server.MapPath("~/imgs/" + upload1.FileName).ToString());
//pbficheiro.ImageUrl = @"~/imgs/" + upload1.FileName;
Byte[] ba = new BinaryReader(upload1.PostedFile.InputStream).ReadBytes((Int32)upload1.PostedFile.InputStream.Length);
using (MemoryStream ms = new MemoryStream(ba, false))
{
System.Drawing.Image imgTmp = System.Drawing.Image.FromStream(ms);
Bitmap bm = new Bitmap(imgTmp.Width, imgTmp.Height);
Graphics g = Graphics.FromImage(bm);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
Rectangle rect = new Rectangle(0, 0, imgTmp.Width, imgTmp.Height);
g.DrawImage(imgTmp, rect, 0, 0, imgTmp.Width, imgTmp.Height, GraphicsUnit.Pixel);
bm.Save(Server.MapPath("~/imgs/" + upload1.FileName).ToString());
pbficheiro.ImageUrl = @"~/imgs/" + upload1.FileName;
}
}