Resim boyutlandırma örneği (asp.net)

Ceviz Viki sitesinden

Git ve: kullan, ara

Konu başlıkları

Motivasyon

http://forum.ceviz.net/showthread.php?t=50585 Konusuna cevap olarak yazılmıştır.

Amaç

.net framework içinde bulunan hazır kütüphanelerle resmi daha küçük boyutlara getirmek (thumbnail) işlemini yapmaktır.

Gereksinimler

Graphics sınıfının DrawImage metodunun hedefini kurmak için 100x100 boyutlarında "blank.jpg" isimli boş bir jpeg resmi. Buraya yapıştırılan kod, Windows XP Service Pack 2 kurulu bir bilgisayarda .net framework 2.0 ve IIS ile test edilmiştir.

Kod

Aspx kodu : (resim_boyutlandir.aspx)

<%@ Page
Language = "C#"
AutoEventWireup = "false"
Inherits = "dotnet.resim_boyutlandir"
ValidateRequest = "false"
EnableSessionState = "false"
%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Resmi herhangi bir bileşen kullanmadan boyutlandır.</title>
 
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />
<meta http-equiv="PRAGMA" content="NO-CACHE" />
 
</head>
<body>
<form id="Form_resim_boyutlandir" method="post" runat="server">
<asp:FileUpload ID="fileuploadResim" runat="server"/>
<asp:Button ID="buttonGonder" runat="server" Text="Gönder"/>
</form>
</body>
</html>

C# kodu : (resim_boyutlandir.aspx.cs)

/*
* Created by SharpDevelop.
* User: anov
* Date: 17.05.2008
* Time: 11:54
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/

 
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D; //önemlidir.
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
 
namespace dotnet
{
/// <summary>
/// Description of resim_boyutlandir
/// </summary>
public class resim_boyutlandir : Page
{
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#region Data
protected FileUpload fileuploadResim;
protected Button buttonGonder;
#endregion
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#region Initialize Component
 
protected override void OnInit(EventArgs e)
{ InitializeComponent();
base.OnInit(e);
}
//----------------------------------------------------------------------
private void InitializeComponent()
{
//------------------------------------------------------------------
buttonGonder.Click += new EventHandler(buttonGonder_Click);
//------------------------------------------------------------------
}
#endregion
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#region button_Click
private void buttonGonder_Click(object sender, EventArgs e)
{
if(fileuploadResim.HasFile)
{
System.Drawing.Image i = System.Drawing.Image.FromStream(fileuploadResim.PostedFile.InputStream);
//Not: blank.jpg 100x100 boyutunda bembeyaz bir resim dosyasıdır.
System.Drawing.Image hedef = System.Drawing.Image.FromFile(Server.MapPath("resim/blank.jpg"));
Graphics g = Graphics.FromImage(hedef);
g.InterpolationMode = InterpolationMode.Bilinear; //Resim küçüldüğünde oluşan (flicker)
//g.SmoothingMode da kullanılabilir burada.
g.DrawImage(i, new Rectangle(0,0,100,100));
Response.Clear(); //Ekrana sadece resmi yazdıracağız.
Response.ContentType = "image/jpeg";
hedef.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End();
}
}
#endregion
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
}