使用ASP从SQL Server检索图像.. NET web服务

本文关键字:图像 NET web 服务 检索 Server ASP SQL 使用 | 更新日期: 2023-09-27 18:07:52

我有一个SQL Server (2008 R2)与一个图像表,表有一个列与类型图像。我使用

将我的图像插入该列
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//insert the file into database
string strQuery = "insert into pm005.images(imageName, type, alt, img)" + " values (@Name, @ContentType, @Alt, @Data)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contenttype;
cmd.Parameters.Add("@Alt", SqlDbType.VarChar).Value = TextBox1.Text;
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
InsertUpdateData(cmd);
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "File Uploaded Successfully";
private Boolean InsertUpdateData(SqlCommand cmd)
{
    // Open a connection the the SQL Server
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "server=sql-server;integrated security=SSPI;database=pm005";
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        return true;
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
        return false;
    }
    finally
    {
        con.Close();
        con.Dispose();
    }
}

我认为这很好,因为插入返回true,我可以看到数据库中的数据。

然而,我不能为我的生活看到我应该如何得到这个图像回来。具体来说,我想要的是能够调用我的web服务,将imageID传递给它,并将图像(并且只有图像)返回给我。这与PHP非常相似。这是我现在使用的代码。

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public System.Drawing.Image  getImage(int imageID, string uName, string pword)
{
    string str="";
 try
 {
     SqlCommand cmd = getSQLCommand("SELECT img FROM pm005.images WHERE id="+imageID);
 byte[] Img = (byte[])cmd.ExecuteScalar();
  // Convert the image record to file stream and display it to picture control
  str = Convert.ToString(DateTime.Now.ToFileTime());
  FileStream fs = new FileStream(str, FileMode.CreateNew, FileAccess.Write);
  fs.Write(Img, 0, Img.Length);
  fs.Flush();
  fs.Close();
 }
 catch
 {
 }
 finally
 {
 }
 System.Drawing.Image theImage = System.Drawing.Image.FromFile(str);
 return theImage;
}

给出错误:

System.ArgumentException: The path is not of a legal form.
   at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength)
   at System.IO.Path.GetFullPathInternal(String path)
   at System.IO.Path.GetFullPath(String path)
   at System.Drawing.IntSecurity.UnsafeGetFullPath(String fileName)
   at System.Drawing.IntSecurity.DemandReadFileIO(String fileName)
   at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
   at WebService2.Service1.getImage(Int32 imageID, String uName, String pword)

我看了很多教程,除了"使用PHP"之外,没有找到任何可靠的答案。如果我必须我会,但肯定必须有一种方法在ASP.NET中做到这一点?

使用ASP从SQL Server检索图像.. NET web服务

尝试指定完整FilePath:

FileStream fs = new FileStream(@"C:'Temp'YourFile.jpg", FileMode.CreateNew, FileAccess.Write);
编辑:

而不是使用文件系统,听起来你想在内存中做:

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
    return  ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

 System.Drawing.Image theImage = null;
 try
 {
     SqlCommand cmd = getSQLCommand("SELECT img FROM pm005.images WHERE id="+imageID);
     byte[] Img = (byte[])cmd.ExecuteScalar();
     theImage = byteArrayToImage(Img);
 }
 catch
 {
 }
 finally
 {
 }    
 return theImage;    
}