public static ExtraImageInfo CreateImageFromFile(string fileName) { WriteableBitmap resultImage = null; double dpiX = OS.SystemDpiX; double dpiY = OS.SystemDpiY; ExtraImageInfo extraImageInfo = WriteableBitmapCreator.CreateWriteableBitmapFromFile(fileName, dpiX, dpiY); bool isImageFormatGrayScale = WriteableBitmapWrapper.IsImageFormatGrayScale(extraImageInfo.Image); if (isImageFormatGrayScale) { resultImage = extraImageInfo.Image; } else if (extraImageInfo.Image.Format != PixelFormats.Bgra32) { PixelFormat pixelFormat = PixelFormats.Bgra32; WriteableBitmap newImage = WriteableBitmapConverter.ConvertWriteableBitmap(extraImageInfo.Image, pixelFormat); resultImage = newImage; extraImageInfo.Image = resultImage; } return(extraImageInfo); }
//---------------------------------------------------------------------------------------------- //Создание битовой маски из изображения //( изображение должно иметь только два цвета - черный и белый ) public static BitMask2D CreateBitMaskFormWriteableBitmap(WriteableBitmap bitmap) { WriteableBitmapWrapper wrapper = WriteableBitmapWrapper.Create(bitmap); BitMask2D bitMask = new BitMask2D(bitmap.PixelHeight, bitmap.PixelWidth, false); for (int x = 0; x < wrapper.Image.PixelWidth; x++) { for (int y = 0; y < wrapper.Image.PixelHeight; y++) { Color color = wrapper.GetPixelColor(x, y); bitMask[y, x] = color.Equals(Colors.White); } } return(bitMask); }
public static double[] GetRowGrayScaleValues(WriteableBitmap bitmap, int row) { WriteableBitmapWrapper wrapper = WriteableBitmapWrapper.Create(bitmap); if (wrapper.IsFormatGrayScale) { double[] grayScaleValues = wrapper.GetRowGrayValues(row); return(grayScaleValues); } else { Color[] rowColors = wrapper.GetRowColors(row); double[] grayScaleValues = new double[rowColors.Length]; for (int index = 0; index < rowColors.Length; index++) { Color color = rowColors[index]; double grayIntensity = ColorWrapper.GetGrayIntensity(color); grayScaleValues[index] = grayIntensity; } return(grayScaleValues); } }
private void Application_Startup(object sender, StartupEventArgs e) { string[] args = e.Args; MainViewModel mainViewModel = new MainViewModel(); int imageNumber = 1; Action actionPictureTaken = () => { Application.Current.Dispatcher.BeginInvoke (DispatcherPriority.Background, new Action( () => { WriteableBitmap bitmap = ReadForPictureTakenEvent(); mainViewModel.MainImageSource = bitmap; if (args.Contains(HoloCommon.Synchronization.Events.Action.SAVE_IMAGE)) { WriteableBitmapWrapper bitmapWrapper = WriteableBitmapWrapper.Create(bitmap); string fileName = Path.Combine(DEFAULT_SAVE_IMAGE_PATH, imageNumber.ToString() + ".png"); bitmapWrapper.SaveToPngFile(fileName); imageNumber++; HoloCommon.Synchronization.SynchronizationManager.SetSignal(HoloCommon.Synchronization.Events.Image.IMAGE_SAVED); } }) ); }; Action actionImageCreated = () => { Application.Current.Dispatcher.BeginInvoke (DispatcherPriority.Background, new Action( () => { WriteableBitmap bitmap = ReadForImageCreatedEvent(); mainViewModel.MainImageSource = bitmap; HoloCommon.Synchronization.SynchronizationManager.SetSignal(HoloCommon.Synchronization.Events.Image.IMAGE_UPDATED); }) ); }; Thread threadPictureTaken = null; Thread threadImageCreated = null; MainWindow mainWindow = new MainWindow(); mainWindow.ViewModel = mainViewModel; if (e.Args.Length > 0) { mainWindow.Title = string.Join(" ", e.Args); if (e.Args.Contains(HoloCommon.Synchronization.Events.Camera.PICTURE_TAKEN)) { threadPictureTaken = HoloCommon.Synchronization.SynchronizationManager.RunActionOnSignal(actionPictureTaken, HoloCommon.Synchronization.Events.Camera.PICTURE_TAKEN); } if (e.Args.Contains(HoloCommon.Synchronization.Events.Image.IMAGE_CREATED)) { threadImageCreated = HoloCommon.Synchronization.SynchronizationManager.RunActionOnSignal(actionImageCreated, HoloCommon.Synchronization.Events.Image.IMAGE_CREATED); } } mainWindow.Closed += (object ss, EventArgs eventArgs) => { if (threadPictureTaken != null) { threadPictureTaken.Abort(); } if (threadImageCreated != null) { threadImageCreated.Abort(); } }; mainWindow.Show(); }