using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DAL;
using System.IO;
using System.Drawing.Imaging;
namespace New_Practice.Admin
{
public partial class AddContent : System.Web.UI.Page
{
public string strimg = "";
ContentBl bl = new ContentBl();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["id"] != null)
{
FillData();
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Request.QueryString["id"] == null)
{
bl.InsertContent(txtTitle.Text, "", txtDescription.Text, chkIsActive.Checked);
tblContent tbl = bl.SelectMaxId();
Int32 maxid = tbl.id;
string img = UploadImage(maxid);
bl.UpdateContentImage(maxid, img);
Response.Redirect("Content.aspx?flag=add");
}
else
{
if (fuImage.HasFile)
{
string strImg = UploadImage(Convert.ToInt32(Request.QueryString["id"]));
bl.UpdateContent(Convert.ToInt32(Request.QueryString["id"]), txtTitle.Text, strImg, txtDescription.Text, chkIsActive.Checked);
}
else
{
bl.UpdateContent(Convert.ToInt32(Request.QueryString["id"]), txtTitle.Text, hfImage.Value, txtDescription.Text, chkIsActive.Checked);
}
}
}
public string UploadImage(Int32 maxid)
{
string actualFolder = Server.MapPath("../Content/Full/");
string thumbFolder = Server.MapPath("../Content/Thumb/");
string smallFolder = Server.MapPath("../Content/Small/");
DirectoryInfo actDir = new DirectoryInfo(actualFolder);
DirectoryInfo thumbDir = new DirectoryInfo(thumbFolder);
DirectoryInfo smallDir = new DirectoryInfo(smallFolder);
if (!actDir.Exists)
{
Directory.CreateDirectory(actualFolder);
}
if (!thumbDir.Exists)
{
Directory.CreateDirectory(thumbFolder);
}
if (!smallDir.Exists)
{
Directory.CreateDirectory(smallFolder);
}
string imagename = maxid + Path.GetExtension(fuImage.PostedFile.FileName);
string fullpath = actualFolder + imagename;
string thumbpath = thumbFolder + imagename;
string smallpath = smallFolder + imagename;
if (File.Exists(fullpath))
{
File.Delete(fullpath);
}
fuImage.SaveAs(fullpath);
ResizeImageFreeSize(fullpath, thumbpath, 320, 260, Path.GetExtension(fuImage.PostedFile.FileName));
ResizeImageFreeSize(fullpath, smallpath, 120, 80, Path.GetExtension(fuImage.PostedFile.FileName));
return imagename;
}
public static void ResizeImageFreeSize(string OriginalFile, string NewFile, int MinWidth, int MinHeight, string FileExtension)
{
var NewHeight = MinHeight;
var NewWidth = MinWidth;
var OriginalImage = System.Drawing.Image.FromFile(OriginalFile);
if (OriginalImage.Width < MinWidth || OriginalImage.Height < MinHeight)
throw new Exception(String.Format("Invalid Image Dimensions, please upload an image with minmum dimensions of {0}x{1}px", MinWidth.ToString(), MinHeight.ToString()));
if (OriginalImage.Height == OriginalImage.Width)
NewWidth = NewHeight = (MinWidth > MinHeight) ? MinWidth : MinHeight;
else
{
if (MinWidth > MinHeight)
NewHeight = (int)(OriginalImage.Height * ((float)MinWidth / (float)OriginalImage.Width));
else
NewWidth = (int)(OriginalImage.Width * ((float)MinHeight / (float)OriginalImage.Height));
}
var ResizedBitmap = new System.Drawing.Bitmap(OriginalImage, NewWidth, NewHeight);
FileExtension = FileExtension.ToLower().Replace(".", "");
ImageFormat Format = null;
switch (FileExtension)
{
case "jpg":
Format = ImageFormat.Jpeg;
break;
case "gif":
Format = ImageFormat.Gif;
break;
case "png":
Format = ImageFormat.Png;
break;
default:
Format = ImageFormat.Png;
break;
}
ResizedBitmap.Save(NewFile, Format);
OriginalImage.Dispose();
ResizedBitmap.Dispose();
}
public void FillData()
{
tblContent tbl = new tblContent();
tbl = bl.SelectSingle(Convert.ToInt32(Request.QueryString["id"]));
if (tbl != null)
{
txtTitle.Text = tbl.title;
hfImage.Value = tbl.img;
txtDescription.Text = tbl.desc;
strimg = "<a href="../Content/Thumb/" + hfImage.Value + "" runat="server" rel="facebox">View</a>";
}
}
}
}