Beispiel #1
0
        // GET: Image
        public ActionResult Index(string camera)
        {
            base.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1.0));
            base.Response.Cache.SetValidUntilExpires(false);
            base.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            base.Response.Cache.SetNoStore();

            string url = urlCameraHelper.ObterUrlDeCamera(camera);

            AuthenticatedRequest request = new AuthenticatedRequest("casa", "familiaLuz");

            // bool precisaDeParametroParaVerImagem = urlCameraHelper.CarregarParametroParaVerImagem();


            using (Image image = request.GetImage(url))
            {
                bool escreverData;
                WriteImageOptions options = (bool.TryParse(Request.QueryString["data"], out escreverData) && escreverData) ? WriteImageOptions.Yes : WriteImageOptions.No;

                using (MemoryStream imageStream = image.WriteDateTimeAndConvertToMemoryStream(options))
                {
                    return(base.File(imageStream.ToArray(), "image/jpeg"));
                }
            }
        }
        public static MemoryStream WriteDateTimeAndConvertToMemoryStream(this Image image, WriteImageOptions imageOptions)
        {
            using (Bitmap bitmap = new Bitmap(image))
            {
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    if (imageOptions == WriteImageOptions.Yes)
                    {
                        graphics.DrawString(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff"), new Font("Arial", 14f, FontStyle.Bold), SystemBrushes.MenuHighlight, (PointF) new Point(400, 450));
                    }

                    MemoryStream stream = new MemoryStream();
                    bitmap.Save(stream, ImageFormat.Jpeg);
                    stream.Seek(0L, SeekOrigin.Begin);
                    return(stream);
                }
            }
        }