I have database and image data example: 0xFFD8FFE000104A46494600010100000100010000FFE1018C45786966000049492A0008000000020031010200070000002600000069870400010000002E00000000000000476F6F676C6500000500009007000400000030323230099007000B0000007000000086920700080100007B00000002A00400010000006F02000003A0
and I think it is image converted to bytes. My Uploading code:
protected void Upload(object sender, EventArgs e)
{
FileUpload FileUpload1 = LoginView3.FindControl("FileUpload1") as FileUpload;
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
string email = User.Identity.Name;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into tblFiles values (@Name, @ContentType, @Data, @email)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
cmd.Parameters.AddWithValue("@email", email);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
And now, I must convert data from database and display image in browser. I looking for tutorials and information about "image.fromstream", "Image handlers", "base64" and I don't know what I do wrong. I looking for ready method or tutorials or information about new I should have read. My sql database code:
CREATE TABLE [dbo].[tblFiles]
(
[id] INT IDENTITY (1, 1) NOT NULL,
[Name] VARCHAR (50) NOT NULL,
[ContentType] NVARCHAR (200) NOT NULL,
[Data] VARBINARY (MAX) NOT NULL,
[email] VARCHAR (50) NOT NULL
);
If I use "Image.fromstream", I will have this error " Error 1 'System.Web.UI.WebControls.Image' does not contain a definition for 'FromStream'" or "Error 1 A using namespace directive can only be applied to namespaces; 'System.Drawing.Image' is a type not a namespace"
I using this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.IO.Stream;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing.Image;
Image.FromStreamwill do a great job of parsing thebyte[]stored in the database, and providing you with metadata about the image like "what's the encoding? jpg? gif?" or "what are the dimensions?" but if all you are doing is sending it back to the browser, you may consider just returning the stream directly back to the response. This approach is a little unsafe (what if someone stored a non-imagebyte[]somehow?) but if you do no image manipulation, trust your inputs, and trust browsers to parse image streams safely, then it may suffice