Member Avatar for Dharshika
Dharshika

Am trying to retrieve the original image from the database on clicking the thumbnail image in the grid view, but the image retrieved is getting displayed in the byte format, Can anyone help me.........................

 Here is the code

    <%@ WebHandler Language="C#" Class="FullImage" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Drawing;

public class FullImage : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
       string imageid = context.Request.QueryString["ImID"];
       if (imageid == null || imageid == "")
       {
           //Set a default imageID
           imageid = "1";
       }
       SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["vidhyaConnectionString"].ConnectionString);
        connection.Open();
        SqlCommand command = new SqlCommand("select Image from Image where ImageID="+imageid, connection);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        Stream str = new MemoryStream((Byte[])dr[0]);


        Bitmap loBMP = new Bitmap(str);
        Bitmap bmpOut = new Bitmap(100, 100);

        Graphics g = Graphics.FromImage(bmpOut);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.FillRectangle(Brushes.White, 0, 0, 100, 100);
        g.DrawImage(loBMP, 0, 0, 100, 100);

        MemoryStream ms = new MemoryStream();
        bmpOut.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        byte[] bmpBytes = ms.GetBuffer();
        bmpOut.Dispose();
        ms.Close();


        // byte[] bmpBytes = (byte[])System.ComponentModel.TypeDescriptor.GetConverter(bmpOut).ConvertTo(bmpOut, typeof(byte[]));

        context.Response.BinaryWrite(bmpBytes);
        //context.Response.BinaryWrite((Byte[])dr[0]);
        connection.Close();
        context.Response.End();  
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.