显示存储在数据库asp.net中的图像

本文关键字:图像 net asp 存储 数据库 显示 | 更新日期: 2023-09-27 18:07:57

这是我的数据库:

 create table images 
(
 ID int primary key identity, 
 Name nvarchar(255),
 Size int, 
 ImgData varbinary(max) 
)
CREATE PROCEDURE UploadImages
 @Name nvarchar(255),
 @Size int,
 @ImgData varbinary(max),
 @NewId int output
AS
BEGIN
   INSERT INTO images
     VALUES (@Name, @Size, @ImgData)
SELECT @NewId = SCOPE_IDENTITY()    
END
Create procedure spGetImageById 
@ID int
as
Begin
Select ImgData
from images where ID=@ID
End

这是我的代码在管理页,我写保存图像在数据库和超链接似乎可以看到在另一个页面的图像,以确保它是正确的图像:

HttpPostedFile PostedFile = FileUpload1.PostedFile;
string fileName = Path.GetFileName(PostedFile.FileName);
string fileExtension = Path.GetExtension(fileName);
int fileSize = PostedFile.ContentLength;
if(fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".bmp"|| fileExtension.ToLower() == ".gif" || fileExtension.ToLower() == ".png")
 {
  Stream stream = PostedFile.InputStream;
  BinaryReader binaryReader = new BinaryReader(stream);
  byte[] bytes = binaryReader.ReadBytes((int)stream.Length);
  string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
  using (SqlConnection con = new SqlConnection(cs))
    {
       SqlCommand cmd = new SqlCommand("UploadImages", con);
       cmd.CommandType = CommandType.StoredProcedure;
       con.Open();
     SqlParameter paramName = new SqlParameter()
     {
         ParameterName = "@Name",
         Value = fileName
     };
     cmd.Parameters.Add(paramName);
     SqlParameter paramSize = new SqlParameter()
     {
         ParameterName = "@Size",
         Value = fileSize
     };
     cmd.Parameters.Add(paramSize);
     SqlParameter paramImgData = new SqlParameter()
     {
         ParameterName = "@ImgData",
         Value = bytes
     };
     cmd.Parameters.Add(paramImgData);
     SqlParameter paramNewId = new SqlParameter()
     {
         ParameterName = "@NewId",
         Value =-1,
         Direction = ParameterDirection.Output
     };
     cmd.Parameters.Add(paramNewId);
     cmd.ExecuteNonQuery();
     con.Close();
     Lmas.Visible = true;
     Lmas.Text = "done";
     Lmas.ForeColor = System.Drawing.Color.Green;
     HyperLink1.Visible = true;
     HyperLink1.NavigateUrl = "~/ShowImage.aspx?Id=" + cmd.Parameters["@NewId"].Value.ToString();
    } 
} 
  else 
  {
    Lmas.Visible = true;
    Lmas.Text = "only images (.jpg .png .gif .bmp) can be uploaded";
    Lmas.ForeColor = System.Drawing.Color.Red;
     HyperLink1.Visible = false;
   }

我写这篇文章是为了对图片进行回顾:

    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
      using (SqlConnection con = new SqlConnection(cs))
     {
          SqlCommand cmd = new SqlCommand("spGetImageById", con);
         cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter paramID = new SqlParameter()
        {
            ParameterName = "@ID",
            Value = Request.QueryString["ID"]
        };
        cmd.Parameters.Add(paramID);
        con.Open();
        cmd.ExecuteScalar();
        byte[] bytes = (byte[])cmd.ExecuteScalar();
        string strBase64 = Convert.ToBase64String(bytes);
        Image1.ImageUrl = "data:Image/png;base64," + strBase64;
        }

我想在img-tag中显示我的主页中的图像。所以我试图复制代码,使您审查图像到我的主页,但它说:"过程或函数'spGetImageById'期望参数'@ID',这是没有提供的"。

显示存储在数据库asp.net中的图像

确保获得所有字节,尝试替换

string strBase64 = Convert.ToBase64String(bytes);

string strBase64 = Convert.ToBase64String(bytes, 0, bytes.Length); 

同时将Image:

改为小写

Image1。图像ImageUrl = "数据:/png; base64,"+ strBase64;