如何预览图像而不保存到文件夹中

本文关键字:文件夹 保存 何预览 图像 | 更新日期: 2023-09-27 18:19:18

我正在尝试创建一个社交网站。如果有人发布了一张图像,它将检测图像中的所有面孔,并在脸上画一个正方形。然后我想显示图像。我的问题是如何预览图像而不将其保存到文件夹中。这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
public partial class User_AddPhoto : System.Web.UI.Page
{
    private HaarCascade haar;
    string base64String;
    Stream fs;
    protected void Page_Load(object sender, EventArgs e)
    {
        string location = Server.MapPath("~/Bin/") + "haarcascade_frontalface_default.xml";
        haar = new HaarCascade(location);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        /*Image Preview */
        //Session["Image"] = FileUpload1.PostedFile;
        //fs = FileUpload1.PostedFile.InputStream;
        //BinaryReader br = new BinaryReader(fs);
        //byte[] bytes = br.ReadBytes((Int32)fs.Length);
        //base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
        //Image1.ImageUrl = "data:image/png;base64," + base64String;
        /***/
        ////////////////////////////*Detect Face*///////////////////////////
        //DetectFaces();
        Bitmap bmpImage = new Bitmap(fs);
        Image<Bgr, byte> InputFrame = new Image<Bgr, byte>(bmpImage);
        Image<Gray, byte> grayFrame = (InputFrame).Convert<Gray, byte>();
        MCvAvgComp[][] faces = grayFrame.DetectHaarCascade(
            haar,
            1.4,
            8,
            HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
            new Size(20, 20)
            );
        foreach (MCvAvgComp face in faces[0])
        {
            InputFrame.Draw(face.rect, new Bgr(Color.Red), 3);
        }
        Bitmap Display = InputFrame.ToBitmap();
        //Display.Save(Server.MapPath("~/Images/aaa.jpg"));
        //////////////////////////////////**/////////////////////////////////
        /* Photo Inserting Into Folder and Into Database*/
        BLUser blUser = new BLUser();
        string UserId=Session["UserId"].ToString();
        string date = DateTime.Now.ToString("mm-dd_hh_mm_ss");
        if (FileUpload1.HasFile)
        {
            string Extension = Path.GetExtension(FileUpload1.FileName);
            string ImageName = date;
            string PhotoUrl="~/Images/Posts/" + ImageName + Extension;
            FileUpload1.SaveAs(Server.MapPath("~/Images/Posts/" + ImageName + Extension));
            blUser.PostImage(PhotoUrl,UserId);
        }
        /**/
    }
}

但是我在卡住预览图像。请帮帮我。提前感谢:)

如何预览图像而不保存到文件夹中

在SQL server中保存图像

        Byte[] imgByte = null;
        if (FileUpload1.HasFile && FileUpload1.PostedFile != null)
        {
            HttpPostedFile File = FileUpload1.PostedFile;
            imgByte = new Byte[File.ContentLength];
            File.InputStream.Read(imgByte, 0, File.ContentLength);
        }
        connection = new SqlConnection(ConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString.ToString());
        connection.Open();
        string sql = "INSERT INTO Table1(title,image) VALUES(@theTitle, @theImage) SELECT @@IDENTITY";
        SqlCommand cmd = new SqlCommand(sql, connection);
        cmd.Parameters.AddWithValue("@theTitle", txtTitle.Text);
        cmd.Parameters.AddWithValue("@theImage", imgByte);
        int id = Convert.ToInt32(cmd.ExecuteScalar());
        lblStatus.Text = String.Format("ID is {0}", id);
        Image1.ImageUrl = "~/DisplayImg.ashx?id=" + id;

在浏览器(Handler)中显示保存的图像

public void ProcessRequest (HttpContext context)
{
    Int32 theID;
    if (context.Request.QueryString["id"] != null)
        theID = Convert.ToInt32(context.Request.QueryString["id"]);
    else
        throw new ArgumentException("No parameter specified");
    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)
{
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString());
    string sql = "SELECT image FROM Table1 WHERE id = @ID";
    SqlCommand cmd = new SqlCommand(sql,connection);
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@ID", theID);
    connection.Open();
    object theImg = cmd.ExecuteScalar();
    try
    {
        return new MemoryStream((byte[])theImg);
    }
    catch
    {
        return null;
    }
    finally
    {
        connection.Close();
    }
}
public bool IsReusable {
    get
    {
        return false;
    }
}

详细信息:http://www.aspnettutorials.com/tutorials/database/saving-retrieving-image-cs/

@john可以使用二进制读取器将图像转换为字节数据并分配给图像源。

请参阅下面的链接。

http://mehtawebsolution.com/blog/display-image-preview-without-saving-file-to-folder-asp-net/