PDA

View Full Version : سوال: مشکل در خواندن عکس از دیتابیس



captain_black81
دوشنبه 13 دی 1389, 16:33 عصر
بچه ها من این کدو از همین سایت برداشتم
یه هندلره که قاعدتا باید عکس باینری رو از سرور بخونه بیاره ولی کار نمیکنه و علامت ضربدر میاد
ببینید مشکل کجاس؟




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

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
using System.Collections;
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
general g = new general();
//
SqlConnection _cn = new SqlConnection(g.connectionString());
//
context.Response.ContentType = "image/jpeg";
//
context.Response.Cache.SetCacheability(HttpCacheab ility.NoCache);
context.Response.BufferOutput = false;
string q = "";
// Query String
if (context.Request.QueryString["cid"] != null && context.Request.QueryString["cid"] != "")
{
// Query String
q = context.Request.QueryString["cid"].ToString();
}
// Byte Stream
System.IO.MemoryStream strm = new System.IO.MemoryStream();
try
{
//
_cn.Open();
// SQL
SqlCommand cmd = new SqlCommand("SELECT [IMAGE1] FROM [news] WHERE [ID]='" + q + "'", _cn);
// Byte
byte[] b = new byte[0];
try
{
//
b = (byte[])cmd.ExecuteScalar();
}
catch
{
//
b = new System.Net.WebClient().DownloadData(context.Server .MapPath("images//education.jpg"));
}
finally
{
// Stream
strm.Write(b, 0, b.Length);
// Stream(byte) image
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(strm);
// image
bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
finally
{
//
_cn.Close();
// Stream
strm.Close();
}
}
public bool IsReusable { get { return true; } }
}