Resimlerle sırası ile anlatalım.









































- resimyukle.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="resimyukle.aspx.cs" Inherits="resimyukle" %>
<%@ Register Src="~/BasicKontrols/wuc_resimyukle.ascx" TagPrefix="uc1" TagName="wuc_resimyukle" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-2.1.3.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:wuc_resimyukle runat="server" ID="wuc_resimyukle" />
</div>
</form>
</body>
</html>
<head runat="server">
<title></title>
<script src="Scripts/jquery-2.1.3.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:wuc_resimyukle runat="server" ID="wuc_resimyukle" />
</div>
</form>
</body>
</html>
- wuc_resimyukle.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="wuc_resimyukle.ascx.cs" Inherits="BasicKontrols_wuc_resimyukle" %>
<script type="text/javascript">
function dosyayukle() {
var value = $("#FileUpload1").val();
if (value != '') {
$("#form1").submit();
}
};
</script>
<asp:Panel ID="Pnlfileupload" runat="server" Width="253px">
<asp:FileUpload ID="FileUpload1" runat="server" ClientIDMode="Static" onchange="dosyayukle()"/>
</asp:Panel>
<asp:Panel ID="Pnlfilesonuc" runat="server" Height="125px" Width="172px">
<asp:Image ID="Imageyuklenen" runat="server" Height="126px" Width="169px" />
<br />
</asp:Panel>
<script type="text/javascript">
function dosyayukle() {
var value = $("#FileUpload1").val();
if (value != '') {
$("#form1").submit();
}
};
</script>
<asp:Panel ID="Pnlfileupload" runat="server" Width="253px">
<asp:FileUpload ID="FileUpload1" runat="server" ClientIDMode="Static" onchange="dosyayukle()"/>
</asp:Panel>
<asp:Panel ID="Pnlfilesonuc" runat="server" Height="125px" Width="172px">
<asp:Image ID="Imageyuklenen" runat="server" Height="126px" Width="169px" />
<br />
</asp:Panel>
- wuc_resimyukle.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class BasicKontrols_wuc_resimyukle : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var resimservisiCagir = new WSResim_Servisi();//oluşan servisi çağırma
if (!IsPostBack || FileUpload1.PostedFile == null)
{
Session["yukoncekiresim"] = null;
Pnlfilesonuc.Visible = false;
Pnlfileupload.Visible = true;
return;
}
else
{
Pnlfilesonuc.Visible = true;
Pnlfileupload.Visible = true;
{
protected void Page_Load(object sender, EventArgs e)
{
var resimservisiCagir = new WSResim_Servisi();//oluşan servisi çağırma
if (!IsPostBack || FileUpload1.PostedFile == null)
{
Session["yukoncekiresim"] = null;
Pnlfilesonuc.Visible = false;
Pnlfileupload.Visible = true;
return;
}
else
{
Pnlfilesonuc.Visible = true;
Pnlfileupload.Visible = true;
}
if (FileUpload1.PostedFile.FileName.Length <= 0) return;
if (resimservisiCagir.uzantiboyutkontrol(FileUpload1.PostedFile) != true) return;//serviste resim işlemlerini yap
if (resimservisiCagir.uzantiboyutkontrol(FileUpload1.PostedFile) != true) return;//serviste resim işlemlerini yap
//FileUpload1.SaveAs(Server.MapPath("~/images/") + FileUpload1.PostedFile.FileName);
//Label1.Text = FileUpload1.PostedFile.FileName + " yüklendi..";
Response.Write(Session["yukoncekiresim"].ToString());
Imageyuklenen.ImageUrl = Session["yukoncekiresim"].ToString();
//Label1.Text = FileUpload1.PostedFile.FileName + " yüklendi..";
Response.Write(Session["yukoncekiresim"].ToString());
Imageyuklenen.ImageUrl = Session["yukoncekiresim"].ToString();
}
}
- WSResim_Servisi.cs
using System;
using System.Activities.Expressions;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Activities.Expressions;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for WSResim_Servisi
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WSResim_Servisi : System.Web.Services.WebService {
/// Summary description for WSResim_Servisi
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WSResim_Servisi : System.Web.Services.WebService {
public WSResim_Servisi () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
public bool uzantiboyutkontrol(HttpPostedFile gelendosya)
{
/*if (Session["yukoncekiresim"] != null)
{
FileInfo IlkResimDosyasi = new FileInfo(HttpContext.Current.Server.MapPath(Session["yukoncekiresim"].ToString()));
IlkResimDosyasi.Delete();
}*/
string[] dosyaturleri = { "image/jpg", "image/png", "image/gif", "image/jpeg" };
if (!dosyaturleri.Contains(gelendosya.ContentType)) return false;//resim dosyası uzantı kontrol
int dosyaboyutu = gelendosya.ContentLength / 1024;
if (gelendosya.ContentLength > 0 && dosyaboyutu <= 500) //yüklenen dosya boyutu 500 kilobayta kadar kontrol
{
Random rstgele = new Random();
string ResimUzantisi = Path.GetExtension(gelendosya.FileName);
string ResimAdi = gelendosya.FileName;//gelen dosya adını alma
int resimBoyutu = 800;//hedef genişlik, yükseklik ayarı genişliğe göre otomatik
int resimaduz = gelendosya.FileName.Length;
resimaduz -= 4;
ResimAdi = ResimAdi.Substring(0, resimaduz);//resim adındaki uzantı ve noktasız olarak okuma
ResimAdi += rstgele.Next(99999999, 999999999);//yüklenen dosyaya yeni isimverme
System.Drawing.Image yuklenenFoto = null;
yuklenenFoto = System.Drawing.Image.FromStream(gelendosya.InputStream);
public string HelloWorld() {
return "Hello World";
}
public bool uzantiboyutkontrol(HttpPostedFile gelendosya)
{
/*if (Session["yukoncekiresim"] != null)
{
FileInfo IlkResimDosyasi = new FileInfo(HttpContext.Current.Server.MapPath(Session["yukoncekiresim"].ToString()));
IlkResimDosyasi.Delete();
}*/
string[] dosyaturleri = { "image/jpg", "image/png", "image/gif", "image/jpeg" };
if (!dosyaturleri.Contains(gelendosya.ContentType)) return false;//resim dosyası uzantı kontrol
int dosyaboyutu = gelendosya.ContentLength / 1024;
if (gelendosya.ContentLength > 0 && dosyaboyutu <= 500) //yüklenen dosya boyutu 500 kilobayta kadar kontrol
{
Random rstgele = new Random();
string ResimUzantisi = Path.GetExtension(gelendosya.FileName);
string ResimAdi = gelendosya.FileName;//gelen dosya adını alma
int resimBoyutu = 800;//hedef genişlik, yükseklik ayarı genişliğe göre otomatik
int resimaduz = gelendosya.FileName.Length;
resimaduz -= 4;
ResimAdi = ResimAdi.Substring(0, resimaduz);//resim adındaki uzantı ve noktasız olarak okuma
ResimAdi += rstgele.Next(99999999, 999999999);//yüklenen dosyaya yeni isimverme
System.Drawing.Image yuklenenFoto = null;
yuklenenFoto = System.Drawing.Image.FromStream(gelendosya.InputStream);
KucukBoyut(yuklenenFoto, resimBoyutu, ResimAdi);//istenen işlemler için diğer fonksiyonu çağır
return dosyaturleri.Contains(gelendosya.ContentType);
}
return false;
}
public void KucukBoyut(System.Drawing.Image yuklenenFoto, int resimBoyutu, string resimIsmi)
{
Bitmap islenmisFoto = null;
Graphics grafikNesnesi = null;
int hedeflenenGenislik = resimBoyutu;
int hedeflenenYukseklik = resimBoyutu;
int yeni_gen, yeni_yuk;
yeni_yuk = (int)Math.Round(((float)yuklenenFoto.Height * (float)resimBoyutu) / (float)yuklenenFoto.Width);
yeni_gen = hedeflenenGenislik;
hedeflenenYukseklik = yeni_yuk;
yeni_gen = yeni_gen > hedeflenenGenislik ? hedeflenenGenislik : yeni_gen;
yeni_yuk = yeni_yuk > hedeflenenYukseklik ? hedeflenenYukseklik : yeni_yuk;
//gönderilen genişliğe göre yükseklik ayarının otomatik olarak belirlenmesi
islenmisFoto = new Bitmap(hedeflenenGenislik, hedeflenenYukseklik);
grafikNesnesi = Graphics.FromImage(islenmisFoto);
grafikNesnesi.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, hedeflenenGenislik, hedeflenenYukseklik));
int x = (hedeflenenGenislik - yeni_gen) / 2;
int y = (hedeflenenYukseklik - yeni_yuk) / 2;
grafikNesnesi.SmoothingMode = SmoothingMode.HighQuality;
grafikNesnesi.CompositingQuality = CompositingQuality.HighQuality;
grafikNesnesi.InterpolationMode = InterpolationMode.Default;
ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders()[1];
EncoderParameters eParams = new EncoderParameters(1);
eParams.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
grafikNesnesi.DrawImage(yuklenenFoto, x, y, yeni_gen, yeni_yuk);
islenmisFoto.Save(HttpContext.Current.Server.MapPath("~/images/" + resimIsmi + ".jpg"), codec, eParams);
//Son olarak resmimizi resimler klasörüne kaydettik.bu alana uye klasörünüde ekle
Session["yukoncekiresim"] = "~/images/" + resimIsmi + ".jpg";
/*
*
* Klasor oluşturmak için ?
if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/klasoradi")))
Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/klasoradi"));
Klasor silmek için ?
if (Directory.Exists(HttpContext.Current.Server.MapPath("~/klasoradi")))
Directory.Delete(HttpContext.Current.Server.MapPath("~/klasoradi"));*/
}
}
Bir sonraki konuda aynı web servisini kullanarak çoklu resim yükleme yapacağız.