PDA

View Full Version : تار شدن عکس پس از thumbnail



mirahsani
جمعه 06 دی 1392, 11:23 صبح
سلام دوستان
من از تامبنیل برای تصاویر گالری استفاده می کنم. ولی تار و کمی شطرنجی میشن. کدهایی که استفاده می کنم در زیر قرار میدم. شما کدهای بهتری سراغ ندارید؟


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace asiaamp_english
{
/// <summary>
/// Creates a thumbnail image from a file spec in the calling URL.
/// </summary>
public partial class MakeThumbnail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
int i = Convert.ToInt32(Request.QueryString["i"]);
int j = Convert.ToInt32(Request.QueryString["j"]);
string file = Request.QueryString["file"];
using (System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(file) ))
{
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(i, j, new System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback), IntPtr.Zero);
MemoryStream imageStream = new MemoryStream();

thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
byte[] imageContent = new byte[imageStream.Length];
imageStream.Position = 0;
imageStream.Read(imageContent, 0, (int)imageStream.Length);
Response.ContentType = "image/png";
Response.BinaryWrite(imageContent);
}
}
catch { }
}



public bool ThumbnailCallback()
{
return true;
}


// public System.Drawing.Image.GetThumbnailImageAbort ThumbnailCallBack { get; set; }
}


}

kamranetemadi
جمعه 06 دی 1392, 11:54 صبح
Bitmap target = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(target)) {
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.DrawImage(photo, 0, 0, width, height);
using (MemoryStream memoryStream = new MemoryStream()) {

if (photoType == "jpg")
target.Save(memoryStream, ImageFormat.Jpeg);

else
target.Save(memoryStream, ImageFormat.Png);

OutputCacheResponse(context, File.GetLastWriteTime(photoPath));

memoryStream.WriteTo(context.Response.OutputStream );
}
}

kamranetemadi
جمعه 06 دی 1392, 12:07 عصر
کد اصلی اینه که کامنت ها رو از حالت کامنت خارج کنید و بجای width و height از private استفاده کنید که کامنت شده
این کد برای طول و عرض دلخواهه
تصویر 365 روز کش میشه!!!!!!!!!
<%@ WebHandler Language="C#‎‎" Class="Thumbnail" %>

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;

public class Thumbnail : IHttpHandler {
private Regex _nameValidationExpression = new Regex(@"[^\w/]");
//private int _thumbnailSize = 500;

public void ProcessRequest(HttpContext context) {
string photoName = context.Request.QueryString["p"];
int width, height;
width=int.Parse(context.Request.QueryString["w"].ToString());
height = int.Parse(context.Request.QueryString["h"].ToString());

if (_nameValidationExpression.IsMatch(photoName)) {
throw new HttpException(404, "Invalid photo name.");
}
string cachePath = Path.Combine(HttpRuntime.CodegenDir, photoName + ".jpg");
if (File.Exists(cachePath)) {
OutputCacheResponse(context, File.GetLastWriteTime(cachePath));
context.Response.WriteFile(cachePath);
return;
}
string photoPath = context.Server.MapPath("~/Photo/" + photoName + ".jpg");
Bitmap photo;
try {
photo = new Bitmap(photoPath);
}
catch (ArgumentException) {
throw new HttpException(404, "Photo not found.");
}
context.Response.ContentType = "image/Jpeg";

//if (photo.Width >= photo.Height)
//{
// width = _thumbnailSize;
// height = photo.Height * _thumbnailSize / photo.Width;
//}
//else
//{
// width = photo.Width * _thumbnailSize / photo.Height;
// height = _thumbnailSize;
//}

Bitmap target = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(target)) {
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.DrawImage(photo, 0, 0, width, height);
using (MemoryStream memoryStream = new MemoryStream()) {
target.Save(memoryStream, ImageFormat.Png);
OutputCacheResponse(context, File.GetLastWriteTime(photoPath));
using (FileStream diskCacheStream = new FileStream(cachePath, FileMode.CreateNew)) {
memoryStream.WriteTo(diskCacheStream);
}
memoryStream.WriteTo(context.Response.OutputStream );
}
}
}

private static void OutputCacheResponse(HttpContext context, DateTime lastModified) {
HttpCachePolicy cachePolicy = context.Response.Cache;
cachePolicy.SetCacheability(HttpCacheability.Publi c);
cachePolicy.VaryByParams["p"] = true;
cachePolicy.SetOmitVaryStar(true);
cachePolicy.SetExpires(DateTime.Now + TimeSpan.FromDays(365));
cachePolicy.SetValidUntilExpires(true);
cachePolicy.SetLastModified(lastModified);
}

public bool IsReusable {
get {
return true;
}
}
}