Convert byte[] to image

Posted by donraza007-15388 under ASP.NET on 5/31/2012 | Points: 10 | Views : 7399 | Status : [Member] | Replies : 5
My .ashx handler file contains this code but it gives ArgumentException (Parameter is not valid)

public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PictureAlbumConnectionString"].ConnectionString);
string sql = "SELECT pic FROM Album WHERE Pic_ID = " + request.QueryString["id"];
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
byte[] bytes = (byte[])cmd.ExecuteScalar();
conn.Close();

System.Drawing.ImageConverter imageConverter = new System.Drawing.ImageConverter();
Image image = imageConverter.ConvertFrom(bytes) as Image;
System.Drawing.Bitmap b = new Bitmap(image);
b.Save(context.Response.OutputStream, ImageFormat.Jpeg);
b.Dispose();
}




Responses

Posted by: Kavi.Sirius on: 5/31/2012 [Member] Starter | Points: 25

Up
0
Down
hi

You can use this code.

<%@ WebHandler Language="C#" Class="ImgHandler" %>

using System;
using System.Web;
public class ImgHandler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
System.Data.SqlClient.SqlDataReader rdr = null;
System.Data.SqlClient.SqlConnection conn = null;
System.Data.SqlClient.SqlCommand selcmd = null;
try
{
if (context.Request.QueryString["ID"] != null)
{
string strEmpcode = context.Request.QueryString["ID"].Replace("'", "");
conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings["cons"]);
selcmd = new System.Data.SqlClient.SqlCommand("SELECT imgphoto FROM tblFMS_SeatingTransaction WHERE nvrEmpcode = '" + strEmpcode + "'", conn);
conn.Open();
rdr = selcmd.ExecuteReader();
while (rdr.Read())
{
if (rdr["imgphoto"].ToString() != "")
{
context.Response.ContentType = "image/jpg";
context.Response.BinaryWrite((byte[])rdr["imgphoto"]);
}
}
if (rdr != null)
rdr.Close();
}
}
finally
{
if (conn != null)
conn.Close();
}
}
public bool IsReusable {
get {
return false;
}
}
}



Regards
Kavi.n

donraza007-15388, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: donraza007-15388 on: 5/31/2012 [Member] Starter | Points: 25

Up
0
Down
Thanx Kavi

can you provide some sample binary data from database so that i can insert some sample data in database and try this one.

donraza007-15388, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 5/31/2012 [Member] Silver | Points: 25

Up
0
Down
Refer this...

http://blogs.msdn.com/b/vijay/archive/2007/07/20/how-to-convert-image-into-byte-and-byte-to-image-using-c-in-asp-net.aspx

Regards,
Singaravel M

donraza007-15388, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 5/31/2012 [Member] Silver | Points: 25

Up
0
Down
http://webbeyond.blogspot.in/2011/09/convert-byte-array-to-image-in-aspnet.html

Regards,
Singaravel M

donraza007-15388, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Happyfor on: 4/28/2013 [Member] Starter | Points: 25

Up
0
Down
Imports System.IO
Imports System.Drawing.Printing
Imports RasterEdge.Imaging
Imports RasterEdge.Imaging.Processing
Imports RasterEdge.Imaging.Converting.ByteToImage

Dim Image As New RasterEdgeImaging()

Public Sub ConvertByteArraytoImage()
If True Then
Dim image As New MemoryStream(ByteArrayID)
Dim returnImage As Image = Image.FromStream(image)
Return returnImage
End If
End Sub
this is some example on converting byte array to image. http://www.rasteredge.com/how-to/vb-net-imaging/convert-image-to-byte/

donraza007-15388, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response