private void ActiveCamera_LiveViewUpdated(Camera sender, Stream img) { if (!hasDevice) { return; } using (Bitmap rawInput = new Bitmap(img)) { Bitmap videoBuffer = null; if (rawInput.Width != DriverInterface.Width || rawInput.Height != DriverInterface.Height) { videoBuffer = new Bitmap(DriverInterface.Width, DriverInterface.Height); Graphics gfx = Graphics.FromImage(videoBuffer); float scaleX = 1.0f; if (rawInput.Width != DriverInterface.Width) { scaleX = DriverInterface.Width / (float)rawInput.Width; } float scaleY = 1.0f; if (rawInput.Height != DriverInterface.Height) { scaleY = DriverInterface.Height / (float)rawInput.Height; } float scale = Math.Min(scaleX, scaleY); int newWidth = (int)Math.Floor(scale * rawInput.Width); int newHeight = (int)Math.Floor(scale * rawInput.Height); gfx.Clear(Color.Black); gfx.DrawImage(rawInput, new Rectangle((videoBuffer.Width / 2) - (newWidth / 2), (videoBuffer.Height / 2) - (newHeight / 2), newWidth, newHeight)); gfx.Dispose(); } else { videoBuffer = rawInput; } BitmapData imageLock = videoBuffer.LockBits(new Rectangle(0, 0, videoBuffer.Width, videoBuffer.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); DriverInterface.SetData(imageLock.Scan0, imageLock.Stride, imageLock.Width, imageLock.Height); videoBuffer.UnlockBits(imageLock); if (videoBuffer != rawInput) { videoBuffer.Dispose(); } rawInput.Dispose(); } }
private void btnLoadImage_Click(object sender, EventArgs e) { OpenFileDialog opf = new OpenFileDialog(); opf.Filter = "Image files|*.bmp;*.jpg;*.png"; if (opf.ShowDialog(this) == DialogResult.OK) { Bitmap rawInput = new Bitmap(opf.FileName); Bitmap videoBuffer = null; if (rawInput.Width != DriverInterface.Width || rawInput.Height != DriverInterface.Height) { videoBuffer = new Bitmap(DriverInterface.Width, DriverInterface.Height); Graphics gfx = Graphics.FromImage(videoBuffer); float scaleX = 1.0f; if (rawInput.Width != DriverInterface.Width) { scaleX = DriverInterface.Width / (float)rawInput.Width; } float scaleY = 1.0f; if (rawInput.Height != DriverInterface.Height) { scaleY = DriverInterface.Height / (float)rawInput.Height; } float scale = Math.Min(scaleX, scaleY); int newWidth = (int)Math.Floor(scale * rawInput.Width); int newHeight = (int)Math.Floor(scale * rawInput.Height); gfx.Clear(Color.Black); gfx.DrawImage(rawInput, new Rectangle((videoBuffer.Width / 2) - (newWidth / 2), (videoBuffer.Height / 2) - (newHeight / 2), newWidth, newHeight)); gfx.Dispose(); } else { videoBuffer = rawInput; } if (picView.Image != null) { picView.Image.Dispose(); } picView.Image = (Bitmap)videoBuffer.Clone(); Stopwatch sw = new Stopwatch(); BitmapData imageLock = videoBuffer.LockBits(new Rectangle(0, 0, videoBuffer.Width, videoBuffer.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); sw.Start(); DriverInterface.SetData(imageLock.Scan0, imageLock.Stride, imageLock.Width, imageLock.Height); sw.Stop(); videoBuffer.UnlockBits(imageLock); Console.WriteLine(sw.Elapsed.ToString()); if (videoBuffer != rawInput) { videoBuffer.Dispose(); } rawInput.Dispose(); } }