// Checks whether an area of a bitmap contains entirely the specified alpha value. public static bool IsAlphaEntirely(byte expectedAlpha, BitmapContent bitmap, Rectangle? region = null) { var bitmapRegion = region.HasValue ? region.Value : new Rectangle(0, 0, bitmap.Width, bitmap.Height); // Works with PixelBitmapContent<byte> at this stage if (bitmap is PixelBitmapContent<byte>) { var bmp = bitmap as PixelBitmapContent<byte>; for (int y = 0; y < bitmapRegion.Height; y++) { for (int x = 0; x < bitmapRegion.Width; x++) { var alpha = bmp.GetPixel(bitmapRegion.X + x, bitmapRegion.Y + y); if (alpha != expectedAlpha) return false; } } return true; } else if (bitmap is PixelBitmapContent<Color>) { var bmp = bitmap as PixelBitmapContent<Color>; for (int y = 0; y < bitmapRegion.Height; y++) { for (int x = 0; x < bitmapRegion.Width; x++) { var alpha = bmp.GetPixel(bitmapRegion.X + x, bitmapRegion.Y + y).A; if (alpha != expectedAlpha) return false; } } return true; } throw new ArgumentException("Expected PixelBitmapContent<byte> or PixelBitmapContent<Color>, got " + bitmap.GetType().Name, "bitmap"); }
internal static BitmapContent Resize(this BitmapContent bitmap, int newWidth, int newHeight) { BitmapContent src = bitmap; SurfaceFormat format; src.TryGetFormat(out format); if (format != SurfaceFormat.Vector4) { var v4 = new PixelBitmapContent <Vector4>(src.Width, src.Height); BitmapContent.Copy(src, v4); src = v4; } // Convert to FreeImage bitmap var bytes = src.GetPixelData(); var fi = FreeImage.ConvertFromRawBits(bytes, FREE_IMAGE_TYPE.FIT_RGBAF, src.Width, src.Height, SurfaceFormat.Vector4.GetSize() * src.Width, 128, 0, 0, 0, true); // Resize var newfi = FreeImage.Rescale(fi, newWidth, newHeight, FREE_IMAGE_FILTER.FILTER_BICUBIC); FreeImage.UnloadEx(ref fi); // Convert back to PixelBitmapContent<Vector4> src = new PixelBitmapContent <Vector4>(newWidth, newHeight); bytes = new byte[SurfaceFormat.Vector4.GetSize() * newWidth * newHeight]; FreeImage.ConvertToRawBits(bytes, newfi, SurfaceFormat.Vector4.GetSize() * newWidth, 128, 0, 0, 0, true); src.SetPixelData(bytes); FreeImage.UnloadEx(ref newfi); // Convert back to source type if required if (format != SurfaceFormat.Vector4) { var s = (BitmapContent)Activator.CreateInstance(bitmap.GetType(), new object[] { newWidth, newHeight }); BitmapContent.Copy(src, s); src = s; } return(src); }
// Checks whether an area of a bitmap contains entirely the specified alpha value. public static bool IsAlphaEntirely(byte expectedAlpha, BitmapContent bitmap, Rectangle?region = null) { var bitmapRegion = region.HasValue ? region.Value : new Rectangle(0, 0, bitmap.Width, bitmap.Height); // Works with PixelBitmapContent<byte> at this stage if (bitmap is PixelBitmapContent <byte> ) { var bmp = bitmap as PixelBitmapContent <byte>; for (int y = 0; y < bitmapRegion.Height; y++) { for (int x = 0; x < bitmapRegion.Width; x++) { var alpha = bmp.GetPixel(bitmapRegion.X + x, bitmapRegion.Y + y); if (alpha != expectedAlpha) { return(false); } } } return(true); } else if (bitmap is PixelBitmapContent <Color> ) { var bmp = bitmap as PixelBitmapContent <Color>; for (int y = 0; y < bitmapRegion.Height; y++) { for (int x = 0; x < bitmapRegion.Width; x++) { var alpha = bmp.GetPixel(bitmapRegion.X + x, bitmapRegion.Y + y).A; if (alpha != expectedAlpha) { return(false); } } } return(true); } throw new ArgumentException("Expected PixelBitmapContent<byte> or PixelBitmapContent<Color>, got " + bitmap.GetType().Name, "bitmap"); }