0

我正在为此开发应用程序,我想捕获图像并再次存储在数据库中 ANA 想要检索网页上的图像,我尝试使用此代码

        context.Response.ContentType = "image/jpeg";
        Stream strm = DisplayImage(theID);
        byte[] buffer = new byte[2048];
        int byteSeq = strm.Read(buffer, 0, 2048);

        while (byteSeq > 0)
        {

            context.Response.OutputStream.Write(buffer, 0, byteSeq);
            byteSeq=strm.Read(buffer, 0, 2048);
        }
    }

    public Stream DisplayImage(int theID)
    {
        DB_Connection();

        string sql = "SELECT Image_Data FROM Image_T WHERE Img_ID = '" + theID + "'";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("Img_ID", theID);

        object imgg = (byte[])cmd.ExecuteScalar(); 
        //byte[] imagedata = (byte[])cmd.ExecuteScalar();
        try
        {
            return new MemoryStream((byte[])imgg);
        }
        catch
        {
            return null;
        }
        finally
        {
            con.Close();
        }
    }

在按钮点击呼叫

  protected void Button1_Click(object sender, EventArgs e)
    {
        Image1.ImageUrl = "Handler1.ashx?id=" + 101;

    }
4

1 回答 1

1
 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html>

 <%@ page import="com.itextpdf.text.pdf.PdfWriter"%>
 <%@ page import="com.itextpdf.text.Document"%>
 <%@ page import="com.itextpdf.text.DocumentException"%>
 <%@ page import="com.itextpdf.text.Element"%>
 <%@ page import="org.apache.pdfbox.pdmodel.PDDocument"%> 
 <%@ page import="org.apache.pdfbox.util.PDFTextStripper"%>
 <%@ page import="org.apache.pdfbox.util.PDFTextStripperByArea"%>
 <%@ page import="java.sql.*" %> 
 <%@ page import="java.io.*" %> 
 <%@ page import="java.lang.*" %> 
 <%@ page import="com.itextpdf.*" %>

 <jsp:useBean id="photo" class="api.images" scope="session" />
 <body onload="setTimeout(function(){window.location = 'test.jsp';}, 1000)">
     <%
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); 
Connection con = DriverManager.getConnection(//my aws connection string//);
String temp=(String)session.getAttribute("myId");

String strSQL = "SELECT  bImage FROM [Userdetails].[dbo].[upload_image]      where iImageID='"+temp+"'";

尝试 {

   int iNumPhoto ;

   iNumPhoto=Integer.parseInt(temp);
   con.setAutoCommit (false);  

   // get the image from the database
  byte[] imgData = photo.getPhoto( con,iNumPhoto ) ;   

   // display the image

   response.setContentType("image/gif");

   OutputStream o = response.getOutputStream();
   o.write(imgData);
   o.flush(); 
   o.close();





}
catch (Exception e)
{
  e.printStackTrace();
  throw e;
}

%>

试试上面的代码。Replace //my connection string with yours// 它应该有远程服务器连接,数据库名称,用户名和密码

于 2016-05-01T12:22:56.807 回答