I’m loading in an image from file and doing some checks to see if I can use it later but I can’t seem to get the pattern match to work when I am validating the type of image.
I’m loading the image via:
open System
open System.Drawing
open System.Drawing.Imaging
type bitmapLoader = string -> Bitmap
let loadBitmap : bitmapLoader =
fun filename -> Bitmap.FromFile(filename) :?> Bitmap
The details of the image are being copied into:
type BitmapDetails =
{ Height : int
VerticalResolution : float
Width : int
HorizontalResolution : float
PixelFormat : PixelFormat
RawFormat : ImageFormat
Flags : ImageFlags
}
And then I’m trying to only allow certain type and formats with something like:
let isInvalidBitmapType (bitmapDetails : BitmapDetails) =
let jpeg = ImageFormat.Jpeg
let png = ImageFormat.Png
match bitmapDetails.RawFormat, bitmapDetails.PixelFormat with
| jpeg , PixelFormat.Format24bppRgb -> false
| png , PixelFormat.Format24bppRgb -> false
| png , PixelFormat.Format32bppArgb -> false
| _ , _ -> true
But it’s not working – jpeg is not used and second rule will not be matched.
I’ve tried all kinds of different versions of the above function but they all have warning messages of various types:
let isInvalidBitmapType (bitmapDetails : BitmapDetails) =
match bitmapDetails.RawFormat, bitmapDetails.PixelFormat with
| :? System.Drawing.Imaging.ImageFormat.Jpeg , PixelFormat.Format24bppRgb -> false
| :? System.Drawing.Imaging.ImageFormat.Png , PixelFormat.Format24bppRgb -> false
| :? System.Drawing.Imaging.ImageFormat.Png , PixelFormat.Format32bppArgb -> false
| _ , _ -> true
let isInvalidBitmapType (bitmapDetails : BitmapDetails) =
let jpeg = ImageFormat.Jpeg
let png = ImageFormat.Png
match bitmapDetails.RawFormat, bitmapDetails.PixelFormat with
| :? jpeg , PixelFormat.Format24bppRgb -> false
| :? png , PixelFormat.Format24bppRgb -> false
| :? png , PixelFormat.Format32bppArgb -> false
| _ , _ -> true
let isInvalidBitmapType (bitmapDetails : BitmapDetails) =
let jpeg = ImageFormat.Jpeg.GetType()
let png = ImageFormat.Png.GetType()
match bitmapDetails.RawFormat.GetType(), bitmapDetails.PixelFormat with
| :? jpeg , PixelFormat.Format24bppRgb -> false
| :? png , PixelFormat.Format24bppRgb -> false
| :? png , PixelFormat.Format32bppArgb -> false
| _ , _ -> true
And so many others that it just seems like a futile/frustrating exercise now.
Can anyone help, and explain what I am doing wrong?