0

SQL Server 2008 Expressから画像を取得して、に挿入しようとしています<ASP:Image>。使ってみましたMemoryStreamが、うまくいかないようです。

その瞬間の私のC#コードは次のようになります:

  try
  {
     con.Open();

     SqlCommand sqlGetStep1 = new SqlCommand("staff_getStep1", con);
     {
         sqlGetStep1.CommandType = CommandType.StoredProcedure;
         sqlGetStep1.Parameters.Add(new SqlParameter("@taskID", Convert.ToInt16(taskID)));
         SqlDataReader step1 = sqlGetStep1.ExecuteReader();

         //Check if username exists
         if (step1.Read())
         {
            step1Text = (string)step1["step1Text"];
            step1Image = (byte)step1["step1Image"];

          }//if 
          else
          {
             step1Text = "null";
             step1Image = 0;
          }//else

           }//sqlDeleteNotification

       }//try
       catch (SqlException sqlEx)
       {
           lblSQLError.Visible = true;
           lblSQLError.Text = sqlEx.Message.ToString();
        }//catach sql
        catch (Exception ex)
       {
           lblError.Visible = true;
           lblError.Text = ex.ToString();

       }//Catch exception
       finally
       {
           con.Close();
       }//Finally

画像を表示したいaspxコードは次のようになります。

<asp:Panel runat="server" ID="pnlStep1" Visible="false" CssClass="NormalText">        
   <asp:Label runat="server" ID="lblStep1Text" Text="Step 1 Instruction: "></asp:Label>
   <asp:TextBox runat="server" ID="txtStep1Text" Text="" ReadOnly="true"></asp:TextBox>
   <asp:Label runat="server" ID="lblStep1Image" Text="Step 1 Image: "></asp:Label>
   <asp:Image runat="server" ID="imgStep1" ImageUrl="" Height="100px" Width="100px"/>
</asp:Panel>

どんな助けでも大歓迎です:)

4

1 に答える 1

2

配信される画像のID(この場合はステップID)を取得し、正しいMIMEタイプで結果としてコンテンツを配信する別のURLが必要です。このメソッドのURLをのURLとして使用しasp:Imageます。通常、これは(WebFormsで)HttpHandlerとして実装します。ASP.NETで応答として画像を返す方法を参照してください

于 2013-01-28T03:48:19.307 に答える