Ejemplo n.º 1
0
        // If you want to see the counter increase for every hit on the page, please change the check
        // in page_load from the session object to the page.ispostback. Read more about the firefox bug that made me
        // change the logic in this way below.

        // Warning!!! For some obscure reason, the pageload event is firing twice in Firefox. More about that
        // here: http://stackoverflow.com/questions/2153579/page-load-fires-twice-on-firefox. Despite my efforts (disabling
        // all firefox extensions for example), the event is still firing twice and the img tag that is generated by ASP
        // does not have the common error of missing an img attribute. So, in firefox, every reload of the page causes
        //  the counter used to increase by 2 if I used PostBack to check if the user is new. Therefore I enabled
        // a session variable that checks if the user is already counted. That means that simple reload of the page
        // will not increase the counter. You have to use a different browser or close the current one and open the page 
        // again to increase it. Bear in mind that if you stopped the server, the counter will be reset (I have implemented
        // this functionality via Global.asax Application_Start event.
        protected void Page_Load(object sender, EventArgs e)
        {
            var context = new DataContext();
            var data = context.Data.SingleOrDefault();
            if (data == null)
            {
                var visitorData = new VisitorData();
                visitorData.Number++;
                context.Data.Add(visitorData);
                context.SaveChanges();
                return;
            }

            if (Session["Catched"] == null)  //(!Page.IsPostBack)
            {
                Session["Catched"] = "catched";
                data.Number++;
                context.SaveChanges();                
            }

            context.Dispose();
            Response.Clear();

            GetImage();
            
        }
Ejemplo n.º 2
0
 protected void Application_Start(object sender, EventArgs e)
 {
     var context = new DataContext();
     var data = context.Data.SingleOrDefault();
     if (data == null)
     {
         var visitorData = new VisitorData();
         context.Data.Add(visitorData);
         context.SaveChanges();
         return;
     }
     else
     {
         data.Number = 0;
         context.SaveChanges();
     }
 }
Ejemplo n.º 3
0
        private void GetImage()
        {
            Bitmap generatedImage = new Bitmap(200, 200);
            using (generatedImage)
            {
                Graphics gr = Graphics.FromImage(generatedImage);
                using (gr)
                {
                    gr.FillRectangle(Brushes.MediumSeaGreen, 0, 0, 200, 200);
                    var context = new DataContext();
                    var data = context.Data.SingleOrDefault();
                    gr.DrawString("Number of users: " + data.Number,
                        new Font("Arial", 14, FontStyle.Regular), SystemBrushes.WindowText, new PointF(10, 40));

                    Response.ContentType = "image/gif";


                    generatedImage.Save(Response.OutputStream, ImageFormat.Gif);
                }
            }
        }