Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.IO;
- using System.Drawing.Imaging;
- using System.Drawing;
- public partial class admin_Default2 : System.Web.UI.Page
- {
- /// <summary>
- /// Creates a Thumbnail bitmap of any image file (jpg or bmp).
- /// </summary>
- /// <param name="lcFilename">Filename to load</param>
- /// <param name="lnWidth">desired width after resize</param>
- /// <param name="lnHeight">desired height after resize</param>
- /// <returns>the bitmap image after resize</returns>
- protected Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
- {
- System.Drawing.Bitmap bmpOut = null;
- try
- {
- Bitmap loBMP = new Bitmap(lcFilename);
- ImageFormat loFormat = loBMP.RawFormat;
- decimal lnRatio;
- int lnNewWidth = 0;
- int lnNewHeight = 0;
- //if the image is smaller than the tn - just return it.
- if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
- return loBMP;
- //if landscape, calc new width/height
- if (loBMP.Width > loBMP.Height)
- {
- lnRatio = (decimal)lnWidth / loBMP.Width;
- lnNewWidth = lnWidth;
- decimal lnTemp = loBMP.Height * lnRatio;
- lnNewHeight = (int)lnTemp;
- }
- //if portrait, calc new height/width
- else
- {
- lnRatio = (decimal)lnHeight / loBMP.Height;
- lnNewHeight = lnHeight;
- decimal lnTemp = loBMP.Width * lnRatio;
- lnNewWidth = (int)lnTemp;
- }
- string s = string.Format("{0} changed from {1}x{2} to {3}x{4}<br />\n", lcFilename, loBMP.Width, loBMP.Height, lnNewWidth, lnNewHeight);
- //create output bitmap.
- bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
- Graphics g = Graphics.FromImage(bmpOut);
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
- g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
- g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
- g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
- loBMP.Dispose();
- }
- catch
- {
- return null;
- }
- return bmpOut;
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- /// <summary>
- /// Creates the Thumbnail views
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void Photos_Init(object sender, EventArgs e)
- {
- string localpath = Server.MapPath("~/Photos");
- string[] photos = Directory.GetFiles(localpath, "*.jpg");
- TableRow newrow = new TableRow();
- foreach (string photo in photos)
- {
- //create the cell containing the photo and it's options
- TableCell thisphoto = new TableCell();
- //image
- System.Web.UI.WebControls.Image thisimage = new System.Web.UI.WebControls.Image();
- thisimage.ImageUrl = "~/photos/tn/" + Path.GetFileName(photo);
- thisimage.Width = 200;
- thisphoto.Controls.Add(thisimage);
- //caption
- TextBox tb = new TextBox();
- tb.AutoPostBack = true;
- tb.Height = new Unit(100, UnitType.Pixel);
- tb.Width = new Unit(200, UnitType.Pixel);
- tb.Init += new EventHandler(captiontext_init);
- tb.TextChanged += new EventHandler(captiontext_TextChanged);
- tb.TextMode = TextBoxMode.MultiLine;
- string shortcaptionfilename = Path.GetFileName( Path.ChangeExtension(photo, ".txt"));
- tb.ToolTip = shortcaptionfilename;
- string textfilename = Server.MapPath("~/Photos/captions/") + Path.GetFileName(photo);
- textfilename = Path.ChangeExtension(textfilename, ".txt");
- if (File.Exists(textfilename))
- {
- tb.Text = File.ReadAllText(textfilename);
- }
- thisphoto.Controls.Add(new LiteralControl("<br />"));
- thisphoto.Controls.Add(tb);
- thisphoto.Controls.Add(new LiteralControl("<br />"));
- //link
- HyperLink link = new HyperLink();
- link.CssClass = "bluetext";
- link.Text = "../photos/" + Path.GetFileName(photo);
- link.NavigateUrl = link.Text;
- link.Target = "pic";
- link.Font.Size = FontUnit.Small;
- thisphoto.Controls.Add(new LiteralControl("<br />"));
- thisphoto.Controls.Add(link);
- //delete button
- thisphoto.Controls.Add(new LiteralControl("<br />"));
- Button delete_me = new Button();
- delete_me.Text = "delete";
- delete_me.ID = string.Format("{0}", Path.GetFileName(photo));
- delete_me.CommandArgument = string.Format("{0}", Path.GetFileName(photo));
- delete_me.Click += Delete_Click;
- thisphoto.Controls.Add(delete_me);
- thisphoto.Controls.Add(new LiteralControl("<br /><br /><br /><br />"));
- if (newrow.Cells.Count > 2)
- {
- Photos.Rows.Add(newrow); //add to table
- newrow = new TableRow(); //create a new row
- }
- newrow.Cells.Add(thisphoto);
- if (newrow.Cells.Count > 0)
- {
- Photos.Rows.Add(newrow);
- }
- }
- }
- protected void Resize_Photo(string rawfilename, string webfilename)
- {
- string photo = rawfilename;
- string tn = Server.MapPath("~/photos/tn/") + Path.GetFileName(rawfilename);
- if (System.IO.File.Exists(photo) == true)
- {
- //make the "full size" image from the megapixel that was uploaded
- Bitmap myBitmap = CreateThumbnail(photo, 600, 450);
- myBitmap.Save(webfilename);
- //make the thumbnail image
- Bitmap myThumbnail = CreateThumbnail(photo, 86, 64);
- myThumbnail.Save(tn);
- }
- }
- /// <summary>
- /// Deletes the specified photo
- /// </summary>
- /// <param name="filename">the filename to delete.</param>
- protected void Delete_Photo(string filename)
- {
- string photo = Server.MapPath("~/photos/") + filename;
- string tn = Server.MapPath("~/photos/tn/") + filename;
- if (System.IO.File.Exists(photo))
- {
- System.IO.File.Delete(photo);
- }
- if (System.IO.File.Exists(tn))
- {
- System.IO.File.Delete(tn);
- }
- }
- /// <summary>
- /// Delete button handler
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void Delete_Click(object sender, EventArgs e)
- {
- string filename = (sender as Button).CommandArgument;
- Delete_Photo(filename);
- Response.Redirect(Request.Url.LocalPath);
- }
- /// <summary>
- /// The Upload button handler
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void Button1_Click(object sender, EventArgs e)
- {
- string rawphotodir = Server.MapPath("~/Photos/raw");
- string photodir = Server.MapPath("~/Photos");
- string captiondir = Server.MapPath("~/Photos/captions");
- FileUpload[] fuArray = { FileUpload1, FileUpload2, FileUpload3, FileUpload4 };
- TextBox[] CapArray = { Caption1, Caption2, Caption3, Caption4 };
- for (int i = 0; i < 4; i++)
- //foreach (FileUpload fu in fuArray)
- {
- string filename = fuArray[i].FileName;
- if (Path.GetExtension(filename).ToLower() == ".jpg")
- {
- string savefilename = string.Format(@"{0}\{1}", rawphotodir, Path.GetFileName(filename));
- string webfilename = string.Format(@"{0}\{1}", photodir, Path.GetFileName(filename));
- fuArray[i].SaveAs(savefilename);//full sized
- Resize_Photo(savefilename, webfilename);
- //caps
- string capfilename = Path.ChangeExtension(filename, ".txt");
- string fullcapfilename = string.Format(@"{0}\{1}", captiondir, Path.GetFileName(capfilename));
- string captiontext = CapArray[i].Text;
- TextWriter tw = new StreamWriter(fullcapfilename);
- tw.Write(captiontext);
- tw.Flush();
- tw.Close();
- }
- }
- Response.Redirect(Request.Url.LocalPath);
- }
- /// <summary>
- /// set up the caption
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void captiontext_init(object sender, EventArgs e)
- {
- TextBox tb = (sender as TextBox);
- string filename = Server.MapPath("~/photos/captions/") + tb.ToolTip; //file.txt
- if (File.Exists(filename))
- {
- tb.Text = File.ReadAllText(filename);
- }
- }
- protected void captiontext_TextChanged(object sender, EventArgs e)
- {
- TextBox tb = (sender as TextBox);
- string filename = Server.MapPath("~/photos/captions/") + tb.ToolTip; //file.txt
- // tb.Text = File.ReadAllText(filename);
- File.WriteAllText(filename, tb.Text);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement