我正在尝试创建一个社交网站。如果有人发布图像,它将检测图像中的所有面部并在脸部绘制正方形。然后我想显示图像。我的问题是如何预览图像而不将其保存到文件夹中。这是我的代码
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);
}
/**/
}
}
但我仍然坚持预览图像。请帮帮我一个人。在此先感谢:)
答案 0 :(得分: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/
答案 1 :(得分:0)
@约翰 可以使用二进制Reader将图像转换为字节数据并分配给图像源。
请参阅以下链接。
http://mehtawebsolution.com/blog/display-image-preview-without-saving-file-to-folder-asp-net/