public static ImageHandler FromBitmapAsGreyscale(Bitmap bmp, string title = null) { if (bmp.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb && bmp.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb && bmp.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppRgb) { throw new InvalidOperationException($"Неподдерживаемый формат изображения [{bmp.PixelFormat}]. Поддерживаются только форматы {nameof(System.Drawing.Imaging.PixelFormat.Format24bppRgb)},{nameof(System.Drawing.Imaging.PixelFormat.Format32bppRgb)} и {nameof(System.Drawing.Imaging.PixelFormat.Format32bppArgb)}."); } var imageHandler = new ImageHandler { Data = ImageUtils.BitmapAsGreyscaleToByteArray(bmp, out float min, out float max), PixelFormat = ImagePixelFormat.Float, Format = ImageFormat.Greyscale, Width = bmp.Width, Height = bmp.Height }; Singleton.Get <ImageHandlerRepository>().Add(imageHandler); if (title == null) { title = "Bitmap " + Singleton.Get <ImageHandlerRepository>().GetAll().Count; } imageHandler._tags.Add(ImageHandlerTagKeys.MaximumValueF, max); imageHandler._tags.Add(ImageHandlerTagKeys.MinimumValueF, min); imageHandler._tags.Add(ImageHandlerTagKeys.Title, title); imageHandler._tags.Add(ImageHandlerTagKeys.Thumbnail, ThumbnailGenerator.Generate(bmp)); imageHandler.Update(); return(imageHandler); }
public void UpdateFromBitmap(Bitmap bmp) { if (Format != ImageFormat.Greyscale && PixelFormat != ImagePixelFormat.Byte) { throw new InvalidOperationException($"{nameof(ImageHandler)} имеет недопустимый формат пикселей: {PixelFormat}"); } if (bmp.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb && bmp.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb) { throw new InvalidOperationException($"Неподдерживаемый формат изображения [{bmp.PixelFormat}]. Поддерживаются только форматы {nameof(System.Drawing.Imaging.PixelFormat.Format24bppRgb)} и {nameof(System.Drawing.Imaging.PixelFormat.Format32bppArgb)}."); } if (Format == ImageFormat.Greyscale) { Data = ImageUtils.BitmapAsGreyscaleToByteArray(bmp, out _, out _); } else { Data = ImageUtils.BitmapToByteArray(bmp); Format = bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? ImageFormat.RGB : ImageFormat.RGBA; } Width = bmp.Width; Height = bmp.Height; _tags.SetOrAdd(ImageHandlerTagKeys.Thumbnail, ThumbnailGenerator.Generate(bmp)); if (OpenGlTextureId.HasValue) { UploadToComputingDevice(true); } Update(); }