PDA

View Full Version : چطور میشه یک عکسی را که توی دیتابیس ذخیره شده, توی کنترل Image نشون داد؟



elena3001
چهارشنبه 08 شهریور 1391, 16:19 عصر
من خود عکس ( نه آدرس رو) توی دیتابیس ذخیره کردم , حالا می خوام توی کنترل Image نشون بدم.

meisam12
چهارشنبه 08 شهریور 1391, 19:41 عصر
سلام
می تونی از سورس کد زیر استفاده کنی.

public System.Drawing.Image RetrieveImage()
{
System.Drawing.Image image = null;SqlConnection cn = new
SqlConnection(ConfigurationManager.ConnectionStrin gs["FoodService2005ConnectionString"].ConnectionString); {
SqlCommand cmd = cn.CreateCommand(); {
cmd.CommandText = "SELECT RecipePhoto FROM tblRecipe WHERE RecipeIdentNo = " + (Convert.ToInt32(Request.QueryString["RecipeIdentNo"]));
cn.Open();
byte[] imageData = (byte[])cmd.ExecuteScalar();
MemoryStream memStream = new MemoryStream(imageData);image = System.Drawing.Image.FromStream(memStream); }
}
return image;
}


<asp:ImageID="imgRecipePhoto"Height="125"Width="100%"runat="server"ImageUrl='<%# RetrieveImage() %>'>
</asp:Image>

samadblaj
چهارشنبه 08 شهریور 1391, 20:05 عصر
میشه یکی این کار رو به روش EF بگه چطور عکس رو ذخیره کنم؟

fakhravari
پنج شنبه 09 شهریور 1391, 00:12 صبح
samadblaj (http://barnamenevis.org/member.php?235579-samadblaj)
تاپیک جدا بزنید.
روش دیگری هم هست
توسط هندلر
از add new item یک ashx به پروژه اضافه کنید
سورس به صورت زیر میشه
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Fakhravary.SQL;

namespace Image_Varbinary
{
public class ShowImages : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["UserName"] == null) return;

string UserName = context.Request.QueryString["UserName"];
using (SqlConnection conn = new SqlConnection(DAL.Config.Connection))
{
using (SqlCommand cmd = new SqlCommand("Management_Users_SelectByUserName", conn))
{
cmd.Parameters.Add(new SqlParameter("@UserName", UserName));
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection) )
{
reader.Read();

if (reader["Picture"] == (object)DBNull.Value)
{ return; }
else
{
context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("Picture")]);
reader.Close();
}

}
}
}

}
public bool IsReusable
{
get
{
return false;
}
}
}
}
بعد برای فراخونی
ImageEditUser.ImageUrl = @"~/ShowImages.ashx?UserName=" + Request.QueryString["UserName"];

myazdani
شنبه 16 دی 1391, 19:19 عصر
سلام
می تونی از سورس کد زیر استفاده کنی.

public System.Drawing.Image RetrieveImage()
{

System.Drawing.Image image = null;SqlConnection cn = new
SqlConnection(ConfigurationManager.ConnectionStrin gs["FoodService2005ConnectionString"].ConnectionString); {
SqlCommand cmd = cn.CreateCommand(); {
cmd.CommandText = "SELECT RecipePhoto FROM tblRecipe WHERE RecipeIdentNo = " + (Convert.ToInt32(Request.QueryString["RecipeIdentNo"]));
cn.Open();
byte[] imageData = (byte[])cmd.ExecuteScalar();
MemoryStream memStream = new MemoryStream(imageData);image = System.Drawing.Image.FromStream(memStream); }
}
return image;
}


<asp:ImageID="imgRecipePhoto"Height="125"Width="100%"runat="server"ImageUrl='<%# RetrieveImage() %>'>
</asp:Image>






ممنون
ولی نمیدونم چرا عکس رو لود نمی کنه؟؟

ممکنه ایراد از چی باشه؟؟ طول و عرض یا چیز دیگه؟؟

myazdani
یک شنبه 17 دی 1391, 16:34 عصر
دوستان کسی نمی دونه عدم نمایش عکس به طور کلی چه علتهایی می تونه داشته باشه؟

fakhravari
یک شنبه 17 دی 1391, 20:15 عصر
سمپل بسازید ضمیمه کنید.

myazdani
یک شنبه 17 دی 1391, 20:27 عصر
متوجه منظورتون نشدم

myazdani
چهارشنبه 20 دی 1391, 20:04 عصر
آقا کسی نمی دونه علت چی می تونه باشه؟

<asp:Image ID="imgRecipePhoto" runat="server" ImageUrl='<%# RetrieveImage() %>' BackColor="AliceBlue" >




public System.Drawing.Image RetrieveImage()
{
System.Drawing.Image image = null;

SqlConnection cn=new SqlConnection();
cn.ConnectionString="Data Source=.\\sqlexpress;Initial Catalog=Sample;Integrated Security=True";
SqlCommand cmd = cn.CreateCommand();

cmd.CommandText = "select Pic from ImgT where Id=1";

cn.Open();

byte[] imageData = (byte[])cmd.ExecuteScalar();
MemoryStream memStream = new MemoryStream(imageData);
image = System.Drawing.Image.FromStream(memStream);
return image;
}




دقیقا کد دوستمونه. نمی دونم چرا کار نمی کنه؟؟ :گیج::عصبانی++::عصبانی::ناراح :

fakhravari
جمعه 22 دی 1391, 01:54 صبح
ای بابا.
بیا یکی درست کردم.
http://mohamad-hoosein.persiangig.com/Flash/test%20Img%20Byte.rar