public override void StartCapture(BackgroundWorker bw) { NikonCameraImage helper = new NikonCameraImage(); Int32 index = 0; helper.camera = _camera; helper.index = index; bw.RunWorkerAsync(helper); }
public override BitmapSource GetImage(DoWorkEventArgs e) { NikonCameraImage helper = (NikonCameraImage)e.Argument; BitmapSource source = null, orginalSource = null; try { SniCamResult sResult = DS_U3Wrapper.SniCamGetNextImage(helper.camera.handle, helper.stop, out helper.format, out helper.index, ref helper.image); if (sResult < SniCamResult.SNI_OK) { throw new Exception("SniCamGetNextImage returned " + sResult); } DS_U3Wrapper.SniImageFormat format = (DS_U3Wrapper.SniImageFormat)Marshal.PtrToStructure( helper.format, typeof(DS_U3Wrapper.SniImageFormat)); //Allocate space for image buffer IntPtr buffer = IntPtr.Zero; byte[] managedBuffer; try { buffer = Marshal.AllocHGlobal(2 * format.buf_size.ToInt32()); sResult = DS_U3Wrapper.SniCamGetImageData(helper.camera.handle, helper.image, (uint)(2 * format.buf_size.ToInt32()), ref buffer); if (sResult != SniCamResult.SNI_OK) { DS_U3Wrapper.SniCamReleaseImage(helper.camera.handle, ref helper.image); throw new Exception("SniCamGetImageData returned " + sResult); } if (format.mode == ESniColorMode.sniRgb24) { managedBuffer = new byte[format.buf_size.ToInt32()]; Marshal.Copy(buffer, managedBuffer, 0, format.buf_size.ToInt32()); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(format.width, format.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits( new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat); Marshal.Copy(managedBuffer, 0, bmpData.Scan0, managedBuffer.Length); bmp.UnlockBits(bmpData); IntPtr hBitmap = bmp.GetHbitmap(); BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions(); try { orginalSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, System.Windows.Int32Rect.Empty, sizeOptions); } finally { DeleteObject(hBitmap); } if ((Properties.Settings.Default.CropHeight == 0 && Properties.Settings.Default.CropWidth == 0) || (Properties.Settings.Default.CropHeight + Properties.Settings.Default.CropTop > orginalSource.Height) || (Properties.Settings.Default.CropWidth + Properties.Settings.Default.CropLeft > orginalSource.Width) ) { source = orginalSource; CropImageWidth = 0; CropImageHeight = 0; } else { source = new CroppedBitmap(orginalSource, new System.Windows.Int32Rect((int)Properties.Settings.Default.CropLeft, (int)Properties.Settings.Default.CropTop, (int)(Properties.Settings.Default.CropWidth == 0 ? orginalSource.Width : Properties.Settings.Default.CropWidth), (int)(Properties.Settings.Default.CropHeight == 0 ? orginalSource.Height : Properties.Settings.Default.CropHeight))); CropImageWidth = source.Width; CropImageHeight = source.Height; } source.Freeze(); ImageWidth = orginalSource.Width; ImageHeight = orginalSource.Height; sResult = DS_U3Wrapper.SniCamReleaseImage(helper.camera.handle, ref helper.image); if (sResult != SniCamResult.SNI_OK) { throw new Exception("SniCamReleaseImage returned " + sResult); } } else { App.LogEntry.AddEntry("Nikon Camera is not in ESniColorMode.sniRgb24 - please reset it."); } } catch (Exception /*ex*/) { throw; } finally { if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); buffer = IntPtr.Zero; } } } catch (Exception ex) { App.LogEntry.AddEntry("Failed to get image: " + ex.Message); } finally { } return(source); }