ASP.NET RESİM İŞLEMLERİ
Asp.Net ile resim yükleme ve veritabanına kaydetme. Veritabanına kaydedilen resimleri web formda listelemek. Tıklanan resmi ayrı sayfada tam boy göstermek.
Resim bulunmayan web sitesi yoktur. Bu nedenle her web sitesinin resim yükleme ve kullanıcıya gösterme sayfaları bulunur. Profesyonel web sitelerinde resim yükleme işlemi admin panelinde yapılır. Admin panelinde veri kaydı, güncelleme, silme, raporlama gibi sayfalar bulunur. Karışıklık olmaması için resimler ana dizinde ayrı bir klasöre kaydedilir. Önemli ve silinmesi istenmeyen resimlerde silinebilecek resimlerden ayrı bir klasörde tutulur.
Yine profesyonel web sitelerinde class yapısı vardır. Classlarda veritabanı bağlantıları, kayıt prosedürleri, listeleme, güncelleme sayfaları bulunur. Bunlar web sitesinin çeşitli yerlerinde yüzlerce kez çağrılır. Sınıf yapısı sayesinde bir kez yazılıp istenildiği kadar kullanılabilirler.
Bu makalede yukarıda anlattığım şekilde yapmadım. Ama fonksiyonlar şeklinde yaptım. Bunun nedeni çok sayıda sınıf ve sayfalara bölerek anlaşılmasını zorlaştırmamaktı. İsterseniz fonksiyonlar şeklinde yazılan her bloku bir classa taşıyarak kullanabilirsiniz.
Uygulamaya dönersek, öncelikle ana dizinde “Resimler” adlı bir klasör bulunuyor. Bu klasör resim kayıt şilemlerinde resimlerin kaydedileceği yer olarak gösteriliyor.
Yine veritabanında RESIMLER adlı bir tablo oluşturdum. Burayada resimlerin yolunu, başlığını ve kayıt tarihini gireceğiz.
Uygulama 3 sayfadan oluşuyor. 1. si resimlerin seçilip kaydedildiği sayfa, ikincisi veritabanından çekilip ziyaretçiye gösterildiği sayfa, 3. İse küçük resme tıklayınca, resmin daha büyük gösterileceği sayfa. Her birinin markup ve cs kodları sırası ile aşağıda yazılmıştır.
Kolay gelsin.
ImagesLoad.aspx Markup Kodları
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImageLoad.aspx.cs" Inherits="ImageLoad" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="margin-top:100px; margin-left:250px">
<table border="1">
<tr>
<td>Resim Seçimi</td>
<td><asp:FileUpload ID="ImageUp" runat="server" /></td>
</tr>
<tr>
<td>Başlık Giriniz</td>
<td>
<asp:TextBox ID="BaslikText" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="reslabel" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="ImageSave" runat="server" Text="KAYDET" OnClick="ImageSave_Click" />
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
ImageLoad.aspx.cs Kodları
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class ImageLoad : System.Web.UI.Page
{
SqlCommand SqlCmdSb;
SqlDataAdapter SntAdtp;
DataSet xdset;
SqlConnection sqlbaglanti;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ImageSave_Click(object sender, EventArgs e)
{
string mesaj = "";
string baslik;
baslik = BaslikText.Text;
string resimad;
resimad = ImageFile();
if (resimad == "hata1")
{
mesaj = "Seçtiğiniz Resim Uzantısı Yanlış";
}
if (resimad == "hata2")
{
mesaj = "Resim Yükleme Hatası";
}
if (resimad != "hata1" && resimad != "hata2")
{
ResimKayit(baslik, resimad);
mesaj = "Resim Kaydedildi";
reslabel.Text = mesaj;
}
}
protected string ImageFile()
{
string img_kayit = "~/Resimler/";
string Img_Name;
string Img1, Img2;
string Img_url = "";
if (ImageUp.HasFile)
{
Img_Name = ImageUp.FileName;
int idx1, idx2, idx3;
if (Img_Name != "" && Img_Name != null)
{
Img1 = Img_Name;
idx1 = Img1.Length;
idx2 = Img1.LastIndexOf(".", Img1.Length - 1, Img1.Length - 1);
idx3 = idx1 - idx2;
Img2 = Img1.Substring(idx2 + 1, idx3 - 1);
Img_url = img_kayit + Img_Name;
if (Img2 == "jpg" || Img2 == "JPEG" || Img2 == "png" || Img2 == "gif" || Img2 == "GIF")
{
ImageUp.SaveAs(Server.MapPath(Img_url));
}
else
{
Img_url = "hata1";
}
}
else
{
Img_url = "hata2";
}
}
return Img_url;
}
public void ResimKayit(string baslik, string resim)
{
SqlCmdSb = new SqlCommand();
xdset = new DataSet();
SqlCmdSb.Connection = ConnectSb();
SqlCmdSb.CommandText = "Select MAX(ID) From RESIMLER";
SntAdtp = new SqlDataAdapter(SqlCmdSb);
SntAdtp.Fill(xdset);
int kayit = 0;
if (xdset.Tables[0].Rows.Count == 0 || xdset.Tables[0].Rows[0][0].ToString() == "")
{
kayit = 1;
}
else
{
kayit = (int)xdset.Tables[0].Rows[0][0];
kayit = kayit + 1;
}
SqlCmdSb.Connection = ConnectSb();
SqlCmdSb.CommandText = "Insert Into RESIMLER(ID,RESIM,BASLIK,TARIH) Values(@id, @resim,@baslik,@tarih)";
SqlParameter SP1 = new SqlParameter("@id", SqlDbType.Int);
SqlParameter SP2 = new SqlParameter("@baslik", SqlDbType.NVarChar);
SqlParameter SP3 = new SqlParameter("@resim", SqlDbType.NVarChar);
SqlParameter SP4 = new SqlParameter("@tarih", SqlDbType.DateTime);
SP1.Value = kayit;
SP2.Value = baslik;
SP3.Value = resim;
SP4.Value = DateTime.Now;
SqlCmdSb.Parameters.Add(SP1);
SqlCmdSb.Parameters.Add(SP2);
SqlCmdSb.Parameters.Add(SP3);
SqlCmdSb.Parameters.Add(SP4);
SqlCmdSb.ExecuteNonQuery();
if(sqlbaglanti.State==ConnectionState.Open)
{
sqlbaglanti.Close();
}
}
public SqlConnection ConnectSb()
{
string yol = "Data Source=localhost; Initial Catalog=FIRMA1; Integrated Security=true";
sqlbaglanti = new SqlConnection(yol);
if (sqlbaglanti.State == ConnectionState.Closed)
{
sqlbaglanti.Open();
}
return sqlbaglanti;
}
}
Galeri.aspx Markup Kodları
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Galeri.aspx.cs" Inherits="Galeri" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
background:#286c8f;
}
.images
{
width:175px;
float:left;
margin:5px;
}
a
{
text-decoration:none;
color:#cbbc0e;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="width:755px">
<asp:Repeater ID="imgrepeat" runat="server">
<ItemTemplate>
<div style="width:200px; height:255px; background:#761313; float:left">
<div style="float:left; width:100%; height:25px; padding:0; margin:0">
<a target="_blank" href="resimdetay.aspx?imgkey=<%# Eval("ID") %>"><%# Eval("BASLIK") %></a>
</div>
<div style="width:100%; float:left">
<a target="_blank" href="resimdetay.aspx?imgkey=<%# Eval("ID") %>">
<asp:Image ID="ImgResim" CssClass="images" runat="server" ImageUrl='<%# Eval("RESIM") %>' />
</a>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Galeri.aspx.cs Kodları
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Galeri : System.Web.UI.Page
{
SqlCommand Cmdresim;
SqlDataAdapter Adtpresim;
DataSet Setresim;
SqlConnection sqlbaglanti;
protected void Page_Load(object sender, EventArgs e)
{
ResimYukle();
}
private void ResimYukle()
{
Cmdresim = new SqlCommand();
Cmdresim.Connection = ConnectSb();
Cmdresim.CommandText = "Select *From RESIMLER order by(TARIH) desc";
Adtpresim = new SqlDataAdapter(Cmdresim);
Setresim = new DataSet();
Adtpresim.Fill(Setresim);
imgrepeat.DataSource = Setresim;
imgrepeat.DataBind();
if (sqlbaglanti.State == ConnectionState.Open)
{
sqlbaglanti.Close();
}
}
public SqlConnection ConnectSb()
{
string yol = "Data Source=localhost; Initial Catalog=FIRMA1; Integrated Security=true";
sqlbaglanti = new SqlConnection(yol);
if (sqlbaglanti.State == ConnectionState.Closed)
{
sqlbaglanti.Open();
}
return sqlbaglanti;
}
}
Resimdetay.aspx Markup Kodları
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="resimdetay.aspx.cs" Inherits="resimdetay" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="buyukresim" runat="server" />
</div>
</form>
</body>
</html>
Resimdetay.aspx.cs Kodları
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class resimdetay : System.Web.UI.Page
{
SqlCommand Cmdimage;
SqlDataAdapter Adtpimage;
DataSet Setimage;
SqlConnection sqlbaglanti;
protected void Page_Load(object sender, EventArgs e)
{
string resimyolu;
resimyolu = Request.QueryString["imgkey"];
Cmdimage = new SqlCommand();
Cmdimage.Connection = ConnectSb();
Cmdimage.CommandText = "Select RESIM From RESIMLER Where ID='" + resimyolu + "'";
Adtpimage = new SqlDataAdapter(Cmdimage);
Setimage = new DataSet();
Adtpimage.Fill(Setimage);
buyukresim.ImageUrl = Setimage.Tables[0].Rows[0][0].ToString();
if (sqlbaglanti.State == ConnectionState.Open)
{
sqlbaglanti.Close();
}
}
public SqlConnection ConnectSb()
{
string yol = "Data Source=localhost; Initial Catalog=FIRMA1; Integrated Security=true";
sqlbaglanti = new SqlConnection(yol);
if (sqlbaglanti.State == ConnectionState.Closed)
{
sqlbaglanti.Open();
}
return sqlbaglanti;
}
}
SANATSAL BİLGİ
20/11/2016