PDA

View Full Version : تغییر سایز عکس با استفاده از Handler



ashkan.890
پنج شنبه 30 آبان 1392, 23:18 عصر
سلام
من با استفاده از کد زیر یه عکس رو از دیتا بیس ام فراخونی میکنم تو پیج handler
حالا میخوام عکس تغییر سایز هم پیدا کنه و بعد به خروجی بره
میشه کمکم کنید؟؟
ممنون


public class ImageSlider : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.Clear();

if (!String.IsNullOrEmpty(context.Request.QueryString["id"]))
{
int id = Int32.Parse(context.Request.QueryString["id"]);

Image image = GetImage(id);

context.Response.ContentType = "image/jpeg";
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
else
{
context.Response.ContentType = "text/html";
context.Response.Write("<p>Need a valid id</p>");
}
}

public bool IsReusable
{
get
{
return false;
}
}

private Image GetImage(int id)
{

MemoryStream memoryStream = new MemoryStream();
//Retrieve image from Database to a memeory stream.
// If you are using a different method, use it and assign
// the data to the 'memoryStream' variable.

string connectionString = "Data Source=(local);Initial Catalog=DB1;Integrated Security=True";
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
using (SqlCommand sqlCommand = new SqlCommand("SELECT Img FROM Slider WHERE ID = " + id.ToString(), sqlConnection))
{
sqlConnection.Open();
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

if (sqlDataReader.HasRows)
{
sqlDataReader.Read();
byte[] btImage = (byte[])sqlDataReader["Img"];

memoryStream = new MemoryStream(btImage, false);
}
}
sqlConnection.Close();
}
return Image.FromStream(memoryStream);
}
}

General-Xenon
جمعه 01 آذر 1392, 10:42 صبح
سلام وقت بخیر . انتهای کدت رو یه تغییر کوچیک دادم. موفق باشی


public class ImageSlider : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.Clear();

if (!String.IsNullOrEmpty(context.Request.QueryString["id"]))
{
int id = Int32.Parse(context.Request.QueryString["id"]);

Image image = GetImage(id);

context.Response.ContentType = "image/jpeg";
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
else
{
context.Response.ContentType = "text/html";
context.Response.Write("<p>Need a valid id</p>");
}
}

public bool IsReusable
{
get
{
return false;
}
}

private Image GetImage(int id)
{

MemoryStream memoryStream = new MemoryStream();
//Retrieve image from Database to a memeory stream.
// If you are using a different method, use it and assign
// the data to the 'memoryStream' variable.

string connectionString = "Data Source=(local);Initial Catalog=DB1;Integrated Security=True";
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
using (SqlCommand sqlCommand = new SqlCommand("SELECT Img FROM Slider WHERE ID = " + id.ToString(), sqlConnection))
{
sqlConnection.Open();
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

if (sqlDataReader.HasRows)
{
sqlDataReader.Read();
byte[] btImage = (byte[])sqlDataReader["Img"];

memoryStream = new MemoryStream(btImage, false);
}
}
sqlConnection.Close();
}
Image a = Image.FromStream(memoryStream);
Bitmap bmp = new Bitmap(100, 200);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(a, new Rectangle(0, 0, bmp.Width, bmp.Height));
return bmp;
}
}