Advertisement
some_yahoo

Photo Upload Page Code.

Oct 22nd, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.64 KB | None | 0 0
  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.IO;
  5. using System.Drawing.Imaging;
  6. using System.Drawing;
  7.  
  8. public partial class admin_Default2 : System.Web.UI.Page
  9. {
  10.     /// <summary>
  11.     /// Creates a Thumbnail bitmap of any image file (jpg or bmp).
  12.     /// </summary>
  13.     /// <param name="lcFilename">Filename to load</param>
  14.     /// <param name="lnWidth">desired width after resize</param>
  15.     /// <param name="lnHeight">desired height after resize</param>
  16.     /// <returns>the bitmap image after resize</returns>
  17.     protected Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
  18.     {
  19.         System.Drawing.Bitmap bmpOut = null;
  20.         try
  21.         {
  22.             Bitmap loBMP = new Bitmap(lcFilename);
  23.             ImageFormat loFormat = loBMP.RawFormat;
  24.  
  25.             decimal lnRatio;
  26.             int lnNewWidth = 0;
  27.             int lnNewHeight = 0;
  28.  
  29.             //if the image is smaller than the tn - just return it.
  30.             if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
  31.                 return loBMP;
  32.  
  33.             //if landscape, calc new width/height
  34.             if (loBMP.Width > loBMP.Height)
  35.             {
  36.                 lnRatio = (decimal)lnWidth / loBMP.Width;
  37.                 lnNewWidth = lnWidth;
  38.                 decimal lnTemp = loBMP.Height * lnRatio;
  39.                 lnNewHeight = (int)lnTemp;
  40.             }
  41.             //if portrait, calc new height/width
  42.             else
  43.             {
  44.                 lnRatio = (decimal)lnHeight / loBMP.Height;
  45.                 lnNewHeight = lnHeight;
  46.                 decimal lnTemp = loBMP.Width * lnRatio;
  47.                 lnNewWidth = (int)lnTemp;
  48.             }
  49.             string s = string.Format("{0} changed from {1}x{2} to {3}x{4}<br />\n", lcFilename, loBMP.Width, loBMP.Height, lnNewWidth, lnNewHeight);
  50.  
  51.             //create output bitmap.
  52.             bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
  53.             Graphics g = Graphics.FromImage(bmpOut);
  54.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  55.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  56.             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  57.             g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
  58.             g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
  59.             g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
  60.             loBMP.Dispose();
  61.         }
  62.         catch
  63.         {
  64.             return null;
  65.         }
  66.         return bmpOut;
  67.     }
  68.  
  69.  
  70.     protected void Page_Load(object sender, EventArgs e)
  71.     {
  72.  
  73.     }
  74.  
  75.  
  76.     /// <summary>
  77.     /// Creates the Thumbnail views
  78.     /// </summary>
  79.     /// <param name="sender"></param>
  80.     /// <param name="e"></param>
  81.     protected void Photos_Init(object sender, EventArgs e)
  82.     {
  83.         string localpath = Server.MapPath("~/Photos");
  84.         string[] photos = Directory.GetFiles(localpath, "*.jpg");
  85.  
  86.         TableRow newrow = new TableRow();
  87.  
  88.         foreach (string photo in photos)
  89.         {
  90.             //create the cell containing the photo and it's options
  91.             TableCell thisphoto = new TableCell();
  92.             //image
  93.             System.Web.UI.WebControls.Image thisimage = new System.Web.UI.WebControls.Image();
  94.             thisimage.ImageUrl = "~/photos/tn/" + Path.GetFileName(photo);
  95.             thisimage.Width = 200;
  96.             thisphoto.Controls.Add(thisimage);
  97.  
  98.             //caption
  99.             TextBox tb = new TextBox();
  100.             tb.AutoPostBack = true;
  101.             tb.Height = new Unit(100, UnitType.Pixel);
  102.             tb.Width = new Unit(200, UnitType.Pixel);
  103.             tb.Init += new EventHandler(captiontext_init);
  104.             tb.TextChanged += new EventHandler(captiontext_TextChanged);
  105.             tb.TextMode = TextBoxMode.MultiLine;
  106.             string shortcaptionfilename = Path.GetFileName( Path.ChangeExtension(photo, ".txt"));
  107.  
  108.             tb.ToolTip = shortcaptionfilename;
  109.  
  110.             string textfilename = Server.MapPath("~/Photos/captions/") + Path.GetFileName(photo);
  111.             textfilename = Path.ChangeExtension(textfilename, ".txt");
  112.  
  113.             if (File.Exists(textfilename))
  114.             {
  115.                 tb.Text = File.ReadAllText(textfilename);
  116.             }
  117.             thisphoto.Controls.Add(new LiteralControl("<br />"));
  118.             thisphoto.Controls.Add(tb);
  119.  
  120.             thisphoto.Controls.Add(new LiteralControl("<br />"));
  121.  
  122.             //link
  123.             HyperLink link = new HyperLink();
  124.             link.CssClass = "bluetext";
  125.             link.Text = "../photos/" + Path.GetFileName(photo);
  126.             link.NavigateUrl = link.Text;
  127.             link.Target = "pic";
  128.             link.Font.Size = FontUnit.Small;
  129.             thisphoto.Controls.Add(new LiteralControl("<br />"));
  130.             thisphoto.Controls.Add(link);
  131.  
  132.             //delete button
  133.             thisphoto.Controls.Add(new LiteralControl("<br />"));
  134.             Button delete_me = new Button();
  135.             delete_me.Text = "delete";
  136.             delete_me.ID = string.Format("{0}", Path.GetFileName(photo));
  137.             delete_me.CommandArgument = string.Format("{0}", Path.GetFileName(photo));
  138.             delete_me.Click += Delete_Click;
  139.             thisphoto.Controls.Add(delete_me);
  140.             thisphoto.Controls.Add(new LiteralControl("<br /><br /><br /><br />"));
  141.  
  142.             if (newrow.Cells.Count > 2)
  143.             {
  144.                 Photos.Rows.Add(newrow);  //add to table
  145.                 newrow = new TableRow();  //create a new row
  146.             }
  147.             newrow.Cells.Add(thisphoto);
  148.             if (newrow.Cells.Count > 0)
  149.             {
  150.                 Photos.Rows.Add(newrow);
  151.             }
  152.         }
  153.     }
  154.  
  155.  
  156.     protected void Resize_Photo(string rawfilename, string webfilename)
  157.     {
  158.         string photo = rawfilename;
  159.         string tn = Server.MapPath("~/photos/tn/") + Path.GetFileName(rawfilename);
  160.         if (System.IO.File.Exists(photo) == true)
  161.         {
  162.             //make the "full size" image from the megapixel that was uploaded
  163.             Bitmap myBitmap = CreateThumbnail(photo, 600, 450);
  164.             myBitmap.Save(webfilename);
  165.  
  166.             //make the thumbnail image
  167.             Bitmap myThumbnail = CreateThumbnail(photo, 86, 64);
  168.             myThumbnail.Save(tn);
  169.         }
  170.     }
  171.  
  172.     /// <summary>
  173.     /// Deletes the specified photo
  174.     /// </summary>
  175.     /// <param name="filename">the filename to delete.</param>
  176.     protected void Delete_Photo(string filename)
  177.     {
  178.         string photo = Server.MapPath("~/photos/") + filename;
  179.         string tn = Server.MapPath("~/photos/tn/") + filename;
  180.         if (System.IO.File.Exists(photo))
  181.         {
  182.             System.IO.File.Delete(photo);
  183.         }
  184.         if (System.IO.File.Exists(tn))
  185.         {
  186.             System.IO.File.Delete(tn);
  187.         }
  188.     }
  189.  
  190.     /// <summary>
  191.     /// Delete button handler
  192.     /// </summary>
  193.     /// <param name="sender"></param>
  194.     /// <param name="e"></param>
  195.     protected void Delete_Click(object sender, EventArgs e)
  196.     {
  197.         string filename = (sender as Button).CommandArgument;
  198.         Delete_Photo(filename);
  199.         Response.Redirect(Request.Url.LocalPath);
  200.     }
  201.  
  202.  
  203.     /// <summary>
  204.     /// The Upload button handler
  205.     /// </summary>
  206.     /// <param name="sender"></param>
  207.     /// <param name="e"></param>
  208.     protected void Button1_Click(object sender, EventArgs e)
  209.     {
  210.         string rawphotodir = Server.MapPath("~/Photos/raw");
  211.         string photodir = Server.MapPath("~/Photos");
  212.         string captiondir = Server.MapPath("~/Photos/captions");
  213.  
  214.         FileUpload[] fuArray = { FileUpload1, FileUpload2, FileUpload3, FileUpload4 };
  215.         TextBox[] CapArray = { Caption1, Caption2, Caption3, Caption4 };
  216.  
  217.  
  218.         for (int i = 0; i < 4; i++)
  219.         //foreach (FileUpload fu in fuArray)
  220.         {
  221.             string filename = fuArray[i].FileName;
  222.             if (Path.GetExtension(filename).ToLower() == ".jpg")
  223.             {
  224.                 string savefilename = string.Format(@"{0}\{1}", rawphotodir, Path.GetFileName(filename));
  225.                 string webfilename = string.Format(@"{0}\{1}", photodir, Path.GetFileName(filename));
  226.                 fuArray[i].SaveAs(savefilename);//full sized
  227.                 Resize_Photo(savefilename, webfilename);
  228.                 //caps
  229.                 string capfilename = Path.ChangeExtension(filename, ".txt");
  230.                 string fullcapfilename = string.Format(@"{0}\{1}", captiondir, Path.GetFileName(capfilename));
  231.                 string captiontext = CapArray[i].Text;
  232.  
  233.                 TextWriter tw = new StreamWriter(fullcapfilename);
  234.                 tw.Write(captiontext);
  235.                 tw.Flush();
  236.                 tw.Close();
  237.             }
  238.         }
  239.         Response.Redirect(Request.Url.LocalPath);
  240.     }
  241.  
  242.     /// <summary>
  243.     /// set up the caption
  244.     /// </summary>
  245.     /// <param name="sender"></param>
  246.     /// <param name="e"></param>
  247.     protected void captiontext_init(object sender, EventArgs e)
  248.     {
  249.         TextBox tb = (sender as TextBox);
  250.         string filename = Server.MapPath("~/photos/captions/") + tb.ToolTip; //file.txt
  251.         if (File.Exists(filename))
  252.         {
  253.             tb.Text = File.ReadAllText(filename);
  254.         }
  255.     }
  256.  
  257.  
  258.     protected void captiontext_TextChanged(object sender, EventArgs e)
  259.     {
  260.         TextBox tb = (sender as TextBox);
  261.         string filename = Server.MapPath("~/photos/captions/") + tb.ToolTip; //file.txt
  262.         //  tb.Text = File.ReadAllText(filename);
  263.         File.WriteAllText(filename, tb.Text);
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement