【问题标题】:how to preview an image without saving into folder如何在不保存到文件夹的情况下预览图像
【发布时间】:2015-10-27 04:35:10
【问题描述】:

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

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);
        }
        /**/
    }
}

但我无法预览图像。请帮助我的人。提前致谢:)

【问题讨论】:

    标签: c# asp.net .net emgucv


    【解决方案1】:

    将图片保存在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;
    

    在浏览器中显示保存的图像(处理程序)

    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/

    【讨论】:

      【解决方案2】:

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

      参考下面的链接。

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

      【讨论】:

      • 我试过了,但问题是,它不是一个发布的文件。首先我将发布的文件转换为位图图像并执行一些逻辑,最后我会得到另一个位图图像。我想显示名为 Display 的图像(这是在检测到的面上带有 Square 的图像)
      • @JohnMathew,首先将您的位图图像转换为字节并将此字节转换为base64string。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 2011-07-12
      • 2021-12-20
      • 2013-09-07
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      相关资源
      最近更新 更多