是否有可能将varbinary映像转换为其在C#中的文件路径?
我想在编辑时从数据库中检索图像 但是我希望它显示为它的文件路径(以前上传过) 我的想法是使用MemoryStream将varbinary转换为byte[],将byte[]转换为image,并将image转换为其文件路径是否有可能将varbinary映像转换为其在C#中的文件路径?,c#,C#,我想在编辑时从数据库中检索图像 但是我希望它显示为它的文件路径(以前上传过) 我的想法是使用MemoryStream将varbinary转换为byte[],将byte[]转换为image,并将image转换为其文件路径 有没有可能,有人能帮我吗?你可以从下面所示的数据库中获取图像。 至于路径,您需要在保存图像时将其保存在db中 public void GetImage(string strSQL) { using( System.Data.IDbConnection con = new
有没有可能,有人能帮我吗?你可以从下面所示的数据库中获取图像。
至于路径,您需要在保存图像时将其保存在db中
public void GetImage(string strSQL)
{
using( System.Data.IDbConnection con = new System.Data.SqlClient.SqlConnection("constring"))
{
lock (con)
{
using (System.Data.IDbCommand cmd = con.CreateCommand())
{
lock (cmd)
{
cmd.CommandText = strSQL;
if (con.State != System.Data.ConnectionState.Open)
con.Open();
using (System.Data.IDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
{
lock (reader)
{
int i = reader.GetOrdinal("Field1");
byte[] baImage;
System.Drawing.Image img;
using (var ms = new System.IO.MemoryStream())
{
byte[] buffer = new byte[8040]; // sql page size
int read;
long offset = 0;
while ((read = (int)reader.GetBytes(i, offset, buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
offset += read; // oops! added this later... kinda important
} // Whend
baImage = ms.ToArray(); // Here's your image as byte array
img = System.Drawing.Image.FromStream(ms); // Here's your image as image
} // End Using ms
} // End Lock reader
} // End Using reader
} // End Lock cmd
} // End Using cmd
} // End Lock con
} // End using con
} // End Sub GetImage
如果要在网站上显示图像,可以这样做(在新的通用处理程序的流程请求中):
你试过什么了吗?你在数据库的某个地方保存了原始路径吗?如果在某个地方有原始路径,你可以这样做!我认为您需要两个字段,一个用于字节[],另一个用于文件路径,不要认为您可以从字节[]获取文件路径是的@tsukimi是正确的!创建一个字段作为文件路径来保存它,创建一个二进制字段来存储文件!
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ContentType("image/jpeg");
System.Web.HttpContext.Current.Response.BinaryWrite(baImage);