Пример #1
0
        protected void btnPostComment_Click(object sender, EventArgs e)
        {
            var divtimespan = Master.FindControl("divtimespan") as HtmlGenericControl;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            string photoIdstring = Request["photoId"];
            int photoId = -1;
            if (!int.TryParse(photoIdstring, out photoId)) return;

            using (GalleryEntities ge = new GalleryEntities())
            {
                Comments c = new Comments();
                c.photoId = photoId;
                c.name = tbName.Text;
                c.text = tbComment.Text;
                ge.AddToComments(c);
                ge.SaveChanges();
            }

            ListView1.DataBind();

            sw.Stop();
            if (divtimespan != null)
                divtimespan.InnerText = string.Format("Elapsed time: {0}", sw.Elapsed);
        }
Пример #2
0
        protected void uploadButton_Click(object sender, EventArgs e)
        {
            var divtimespan = Master.FindControl("divtimespan") as HtmlGenericControl;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            //get input data
            DateTime now = DateTime.Now;
            string title = tbTitle.Text;
            string fileName = FileUpload1.FileName;
            string ext = Path.GetExtension(fileName);
            byte[] file = FileUpload1.FileBytes;
            using (var input = new MemoryStream(file))
            {
                //watermarking image
                var output = new MemoryStream();
                AddWaterMark(input, now.ToString(), output);
                file = new byte[output.Length];
                output.Position = 0;
                BinaryReader br = new BinaryReader(output);
                file = br.ReadBytes((int)output.Length);
                br.Close();
            }

            //save image to disk and database
            string path = Server.MapPath("Images");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            fileName = Guid.NewGuid().GetHashCode().ToString("x") + ext;
            path = path + "\\" + fileName;
            try
            {
                var ge = new GalleryEntities();
                Photos photo = new Photos();
                photo.fileName = fileName;
                photo.postingDate = DateTime.Now;
                photo.title = title;
                using (var fs = new FileStream(path, FileMode.Create))
                {
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(file);
                }
                ge.AddToPhotos(photo);
                ge.SaveChanges();
            }
            finally { }
            ListView_Photos.DataBind();
            sw.Stop();
            if (divtimespan != null)
                divtimespan.InnerText = string.Format("Elapsed time: {0}", sw.Elapsed);
        }