Merhaba arkadaşlar bu makalemizde Asp.Net te FileUpload
ile yüklediğimiz resimin boyutunu ayarlayıp tekrar kaydediyoruz.
Sekil 1
Sekil 2
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace aspnet_picture_resize
{
public partial class WebForm1 :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLoad_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string img = string.Empty;
Bitmap newImg = null;
try
{
newImg = imageResize
(FileUpload1.PostedFile.InputStream, 210, 130);//yeni
resim için boyut veriyoruz..
img = Server.MapPath("images/") + Guid.NewGuid
().ToString() + ".png";
newImg.Save(img, ImageFormat.Jpeg);
}
catch (Exception ex)
{
Response.Write("Error: " +
ex.Message.ToString());
}
finally
{
img = string.Empty;
newImg.Dispose();
}
}
}
private Bitmap imageResize(Stream resim, int width, int height)
{
Bitmap orginalImg = new Bitmap(resim);
int newWidth = orginalImg.Width;
int newHeight = orginalImg.Height;
double rateWH = Convert.ToDouble(orginalImg.Width) /Convert.ToDouble(orginalImg.Height);
if (rateWH <= 1 && orginalImg.Width > width)
{
newWidth = width;
newHeight = Convert.ToInt32(Math.Round(newWidth /
rateWH));
}
else if (rateWH > 1 && orginalImg.Height > height)
{
newHeight =height;
newWidth = Convert.ToInt32(Math.Round(newHeight *
rateWH));
}
return new Bitmap(orginalImg, newWidth, newHeight);
}
}
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="aspnet_picture_resize.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnLoad" runat="server" Text="Load"
onclick="btnLoad_Click" Width="116px" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Bir
makalenin daha sonuna geldik. Bir sonraki makalede gorusmek uzere. Bahadir SAHIN
0 comments:
Yorum Gönder