/// <summary> /// 图像格式转换,转换为BitmapSource /// </summary> public BitmapSource ConvertImageBitmap() { try { if (BlOpen) { #if (SDK) g_RawImage.ConvertToBitmapSource(g_ConvertImage); g_ConvertImage.bitmapsource.Freeze(); return(g_ConvertImage.bitmapsource); #endif return(null); } else { return(null); } } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(null); } finally { GC.Collect(); } }
void OnImageGrabbed(ManagedImage rawImage) { ManagedImage convertedBitmapSource = new ManagedImage(); rawImage.ConvertToBitmapSource(PixelFormatEnums.BGR8, convertedBitmapSource); try { if (Monitor.TryEnter(imageLock)) { try { BitmapSource bmpSource = convertedBitmapSource.bitmapsource; if (bmpSource == null) { return; } bmpSource.Freeze(); BitmapSource source = null; if (cameraType == CameraType.Top) { if ((App.Settings.CropHeight == 0 && App.Settings.CropWidth == 0) || (App.Settings.CropHeight + App.Settings.CropTop > bmpSource.Height) || (App.Settings.CropWidth + App.Settings.CropLeft > bmpSource.Width) ) { source = bmpSource; } else { source = new CroppedBitmap(bmpSource, new System.Windows.Int32Rect((int)App.Settings.CropLeft, (int)App.Settings.CropTop, (int)(App.Settings.CropWidth == 0 ? bmpSource.Width : App.Settings.CropWidth), (int)(App.Settings.CropHeight == 0 ? bmpSource.Height : App.Settings.CropHeight))); } } else { source = bmpSource; } source.Freeze(); latestBitmap = GetBitmap(source); //flip image vertically if (App.Settings.FlipImage) { latestBitmap.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY); } latestBitmapSrc = gColorFancy.Model.Utility.GetBitmapSource(latestBitmap); latestBitmapSrc.Freeze(); ImageReceivedEventNotification(latestBitmapSrc, latestBitmap, cameraType); } catch (Exception ex) { Debug.WriteLine("Error: exception: " + ex.Message); } finally { Monitor.Exit(imageLock); } } } catch (Exception ex) { lastError = ex.Message; } finally { convertedBitmapSource.Dispose(); GC.Collect(); } }
private unsafe void InitializeNewResultImage(ManagedImage mImg) { //byte* managedImageDataPtr = mImg.data; IntPtr dataPtr = new IntPtr(managedImageDataPtr); m_pixelsFormat = mImg.pixelFormat; ManagedImage manImg = new ManagedImage(); if (m_pixelsFormat == PixelFormat.PixelFormatMono12 || m_pixelsFormat == PixelFormat.PixelFormatMono16 || m_pixelsFormat == PixelFormat.PixelFormatMono8 || m_pixelsFormat == PixelFormat.PixelFormatSignedMono16) { mImg.Convert(PixelFormat.PixelFormatMono8, manImg); } else { mImg.Convert(PixelFormat.PixelFormatBgr, manImg); } int height = (int)manImg.rows, width = (int)manImg.cols, stride = (int)manImg.stride, dataSize = height * stride, bpp = (int)manImg.bitsPerPixel / 8; byte *imageData = manImg.data; //byte[] imgData = new byte[dataSize]; //Marshal.Copy(dataPtr, imgData, 0, dataSize); Point cursorPosition = this.CurrentCursorPosition; int xRec = (int)(cursorPosition.X / this.VideoStreamAreaActualWidth * width), yRec = (int)(cursorPosition.Y / this.VideoStreamAreaActualHeight * height); Int32Rect rec = GetValidRectangle(xRec, yRec, width, height); m_currentRec = rec; byte[] currBytes = new byte[rec.Height * rec.Width * bpp]; ManagedImage managImgForWrbmp = new ManagedImage(); manImg.ConvertToWriteAbleBitmap(managImgForWrbmp); WriteableBitmap bmpTemp = managImgForWrbmp.writeableBitmap; bmpTemp.CopyPixels(rec, currBytes, rec.Width * bpp, 0); m_currentBytes = currBytes; OnSetNeededCameraProperty(); OnCurrentBytesUpdated(); int xBegin = rec.X, xEnd = rec.X + rec.Width, yBegin = rec.Y, yEnd = rec.Y + rec.Height; for (int xPix = xBegin; xPix <= xEnd; xPix++) { for (int yPix = yBegin; yPix <= yEnd; yPix++) { if (yPix == yBegin || yPix == yEnd || xPix == xBegin || xPix == xEnd) { int pixelPosition = yPix * stride + xPix * bpp; if (manImg.pixelFormat == PixelFormat.PixelFormatBgr) { imageData[pixelPosition] = 0; imageData[pixelPosition + 1] = 0; imageData[pixelPosition + 2] = 0; } else { imageData[pixelPosition] = 0; } } } } ManagedImage temp = new ManagedImage(); manImg.ConvertToBitmapSource(temp); temp.bitmapsource.Freeze(); this.NewResultImage = temp.bitmapsource; OnNewResultImageIsReady(); }
override protected void OnImageEvent(ManagedImage image) { //Debug.WriteLine("OnImageEvent"); try { Dispatcher dispatcher = Dispatcher.CurrentDispatcher; dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal); Dispatcher.Run(); if (!image.IsIncomplete) { // test get image from file //string currentDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); //var tempFileNamePath = Path.Combine(currentDirectory, "laser_spot.bmp"); //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(tempFileNamePath); //var bitmapData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), // System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat); //var img = BitmapSource.Create(bitmapData.Width, bitmapData.Height, bmp.HorizontalResolution, bmp.VerticalResolution, // PixelFormats.Bgr24, null, bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride); //bmp.UnlockBits(bitmapData); // Convert image image.ConvertToBitmapSource(PixelFormatEnums.BGR8, managedImageConverted); BitmapSource img = managedImageConverted.bitmapsource.Clone(); img.Freeze(); RaiseImageChangedEvent(new ImageEventArgs(img)); //img.Dispatcher.InvokeShutdown(); if (_measuring && _imageQueue.Count <= 100) { imgCounter++; lock (_locker) { var temp = image.Convert(PixelFormatEnums.BGR8); _imageQueue.Enqueue(new PtGreyCameraImage { FrameId = (ulong)imgCounter, TimeStamp = (ulong)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds, Image = new System.Drawing.Bitmap(temp.bitmap) // for testing //Image = new System.Drawing.Bitmap(bmp) } ); } RaiseImageEnqueuedEvent(new ImageEnqueuedEventArgs(imgCounter)); _wh.Set(); Debug.WriteLine("enqueue frame: {0}", imgCounter); } else { //Debug.WriteLine("Dropped frame"); } } image.Release(); } catch (SpinnakerException ex) { Debug.WriteLine("Error: {0}", ex.Message); } catch (Exception ex1) { Debug.WriteLine("Error: {0}", ex1.Message); } finally { GC.Collect(); } }