تغییر اندازه تصاویر #2
نویسنده: صابر فتح الهی
تاریخ: ۱۳۹۱/۱۲/۱۱ ۱۲:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PWS.UI.Handler
{
/// <summary>
/// Summary description for PhotoHandler
/// </summary>
public class PhotoHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Web;
namespace PWS.UI.Handler
{
/// <summary>
/// Summary description for PhotoHandler
/// </summary>
public class PhotoHandler : IHttpHandler
{
/// <summary>
/// بازیابی تصویر اصلی از بانک اطلاعاتی
/// </summary>
/// <param name="photoId">کد تصویر</param>
/// <returns></returns>
private byte[] GetImageFromDatabase(int photoId)
{
using (var connection = new SqlConnection("ConnectionString"))
{
using (var command = new SqlCommand("Select Photo From tblPhotos Where Id = @PhotoId", connection))
{
command.Parameters.Add(new SqlParameter("@PhotoId", photoId));
connection.Open();
var result = command.ExecuteScalar();
return ((byte[])result);
}
}
}
/// <summary>
/// بازیابی فایل از دیسک
/// </summary>
/// <param name="photoId">با فرض اینکه نام فایل این است</param>
/// <returns></returns>
private byte[] GetImageFromDisk(string photoId /* or somting */)
{
using (var sourceStream = new FileStream("Original File Path + id", FileMode.Open, FileAccess.Read))
{
return StreamToByteArray(sourceStream);
}
}
/// <summary>
/// Streams to byte array.
/// </summary>
/// <param name="inputStream">The input stream.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException"></exception>
static byte[] StreamToByteArray(Stream inputStream)
{
if (!inputStream.CanRead)
{
throw new ArgumentException();
}
// This is optional
if (inputStream.CanSeek)
{
inputStream.Seek(0, SeekOrigin.Begin);
}
var output = new byte[inputStream.Length];
int bytesRead = inputStream.Read(output, 0, output.Length);
Debug.Assert(bytesRead == output.Length, "Bytes read from stream matches stream length");
return output;
}
/// <summary>
/// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler" /> interface.
/// </summary>
/// <param name="context">An <see cref="T:System.Web.HttpContext" /> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
public void ProcessRequest(HttpContext context)
{
// Set up the response settings
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
// مرحله اول
int size = 0;
switch (context.Request.QueryString["Size"])
{
case "S":
size = 100; //100px
break;
case "M":
size = 198; //198px
break;
case "L":
size = 500; //500px
break;
}
byte[] changedImage;
var id = Convert.ToInt32(context.Request.QueryString["PhotoId"]);
byte[] sourceImage = GetImageFromDatabase(id);
// یا
//byte[] sourceImage = GetImageFromDisk(id.ToString(CultureInfo.InvariantCulture));
//مرحله 2
if (size != 0) //غیر از حالت واقعی تصویر
{
changedImage = Helpers.ResizeImageFile(sourceImage, size, ImageFormat.Jpeg);
}
else
{
changedImage = (byte[])sourceImage.Clone();
}
// مرحله 3
if (changedImage == null) return;
context.Response.AddHeader("Content-Length", changedImage.Length.ToString(CultureInfo.InvariantCulture));
context.Response.BinaryWrite(changedImage);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}PhotoHandler.ashx?PhotoId=10&Size=S مانند <img src='PhotoHandler.ashx?PhotoId=10&Size=S' alt='تصویر ازمایشی' />
موفق باشید.
byte[] sourceImage = GetImageFromDatabase(id);
byte[] sourceImage = GetImageFromDisk(
Path.Combine(context.Server.MapPath("~/uploads"),Path.GetFileName(id.ToString())));PhotoHandler.ashx?PhotoId=test.jpg&Size=S مانند <img src='PhotoHandler.ashx?PhotoId=test.jpg&Size=S' alt='تصویر ازمایشی' />
<system.webServer>
<handlers>
<add name="ImageHandler" path="ImageHandler.ashx" verb="*" type="ImageHandler" />
</handlers>
</system.webServer>