public ActionResult Image(string imageFile, int? width, int? height, int? id) { var memoryStream = new MemoryStream(); var image = new Bitmap(ObjectsRepository.CatalogFolder + imageFile.Replace("___", "/")); if (width != null && height != null) { image = image.Clip10X15(); image = image.ResizeImage(width.Value, height.Value); } if (id != null) { image = image.AddBluredRect(30); var htmlText = ObjectsRepository.GetObjectDescription(id.Value); if (!string.IsNullOrEmpty(htmlText)) { image = image.AddText(30, 0, htmlText); } //var backImage = new Bitmap(Server.MapPath("~\\Content\\images\\keyend.png")); //image = image.AddBackBitmap(backImage); } image.Save(memoryStream, ImageFormat.Jpeg); memoryStream.Seek(0, SeekOrigin.Begin); return File(memoryStream, "image/jpeg"); }
private void StreamVideo(ImageCodecInfo codecInfo, EncoderParameters encoderParams) { MemoryStream m = new MemoryStream(20000); Bitmap image = null; IntPtr ip = IntPtr.Zero; Font fontOverlay = new Font("Times New Roman", 14, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point); DateTime lastMotion = DateTime.Now; int interval = 5; _isRecording = false; _player = new SoundPlayer(); _player.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler((obj, arg) => _player.PlayLooping()); Task.Factory.StartNew(() => { _device.Start(); while (_requested) { try { // capture image ip = _device.GetBitMap(); image = new Bitmap(_device.Width, _device.Height, _device.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, ip); image.RotateFlip(RotateFlipType.RotateNoneFlipY); _currentSensor.ProcessFrame(ref image); if (_currentSensor.IsAlarming) { if (!_isRecording && (DateTime.Now.Second % interval == 0)) _messenger.SendMessage("record"); lastMotion = DateTime.Now; } else { if (DateTime.Now.Subtract(lastMotion).Seconds > interval) { if (_isRecording) { _messenger.SendMessage("stop-record"); _isRecording = false; } if (_isAlarming) { _player.Stop(); _isAlarming = false; } } } // add text that displays date time to the image image.AddText(fontOverlay, 10, 10, DateTime.Now.ToString()); // save it to jpeg using quality options image.Save(m, codecInfo, encoderParams); // send the jpeg image if server requests it if (_messenger != null) _messenger.SendMessage(m.GetBuffer(), MessageType.Image); } catch (SocketException) { _messenger.Dispose(); _messenger = null; _requested = false; } catch { } finally { // clean up m.SetLength(0); if (image != null) image.Dispose(); if (ip != IntPtr.Zero) { Marshal.FreeCoTaskMem(ip); ip = IntPtr.Zero; } } } _device.Pause(); _player.Stop(); _player.Dispose(); fontOverlay.Dispose(); disposeReady.Set(); }); }
// Start serving up frames private void StartWebcam(Webcam cam, ConnectionManager conManager, StreamWriter sw, ImageCodecInfo myImageCodecInfo, EncoderParameters myEncoderParameters) { MemoryStream m = new MemoryStream(20000); Bitmap image = null; IntPtr ip = IntPtr.Zero; Font fontOverlay = new Font("Times New Roman", 14, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point); MotionDetector motionDetector = new MotionDetector(); DateTime lastMotion = DateTime.Now; int interval = 5; stopCondition = false; isRecording = false; player = new System.Media.SoundPlayer(); motionDetector.MotionLevelCalculation = true; player.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler((obj, arg) => player.PlayLooping()); cam.Start(); while (!stopCondition) { try { // capture image ip = cam.GetBitMap(); image = new Bitmap(cam.Width, cam.Height, cam.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, ip); image.RotateFlip(RotateFlipType.RotateNoneFlipY); motionDetector.ProcessFrame(ref image); if (motionDetector.MotionLevel >= alarmLevel) { if (!isRecording && (DateTime.Now.Second % interval == 0)) conManager.SendMessage("record"); lastMotion = DateTime.Now; } else { if (DateTime.Now.Subtract(lastMotion).Seconds > interval) { if (isRecording) { conManager.SendMessage("stop-record"); isRecording = false; } if (isAlarming) { player.Stop(); isAlarming = false; } } } // add text that displays date time to the image image.AddText(fontOverlay, 10, 10, DateTime.Now.ToString()); // save it to jpeg using quality options image.Save(m, myImageCodecInfo, myEncoderParameters); // send the jpeg image if server requests it if (requested) conManager.SendImage(m); } catch (Exception ex) { try { sw.WriteLine(DateTime.Now.ToString()); sw.WriteLine(ex); } catch { } } finally { // Empty the stream m.SetLength(0); // remove the image from memory if (image != null) { image.Dispose(); image = null; } if (ip != IntPtr.Zero) { Marshal.FreeCoTaskMem(ip); ip = IntPtr.Zero; } } } cam.Pause(); player.Stop(); fontOverlay.Dispose(); }