Update GeoTaggedImage.WPF.cs

This commit is contained in:
Clemens 2022-01-09 16:44:42 +01:00
parent e31fde26b4
commit 577c39516f

View file

@ -59,44 +59,49 @@ namespace MapControl.Images
public static BitmapSource ConvertTransparentPixel(BitmapSource source, int transparentPixel) public static BitmapSource ConvertTransparentPixel(BitmapSource source, int transparentPixel)
{ {
var targetFormat = source.Format;
List<Color> colors = null; List<Color> colors = null;
var format = source.Format;
var bpp = format.BitsPerPixel;
if (format == PixelFormats.Indexed8 || if (source.Format == PixelFormats.Indexed8 ||
format == PixelFormats.Indexed4 || source.Format == PixelFormats.Indexed4 ||
format == PixelFormats.Indexed2) source.Format == PixelFormats.Indexed2)
{ {
targetFormat = source.Format;
colors = source.Palette.Colors.ToList(); colors = source.Palette.Colors.ToList();
} }
else if (format == PixelFormats.Gray8 || else if (source.Format == PixelFormats.Gray8)
format == PixelFormats.Gray4 ||
format == PixelFormats.Gray2)
{ {
format = bpp == 8 ? PixelFormats.Indexed8 targetFormat = PixelFormats.Indexed8;
: bpp == 4 ? PixelFormats.Indexed4 : PixelFormats.Indexed2; colors = BitmapPalettes.Gray256.Colors.ToList();
}
colors = Enumerable.Range(0, (1 << bpp)) else if (source.Format == PixelFormats.Gray4)
.Select(i => Color.FromRgb((byte)i, (byte)i, (byte)i)).ToList(); {
targetFormat = PixelFormats.Indexed8;
colors = BitmapPalettes.Gray16.Colors.ToList();
}
else if (source.Format == PixelFormats.Gray2)
{
targetFormat = PixelFormats.Indexed8;
colors = BitmapPalettes.Gray4.Colors.ToList();
} }
var target = source; if (colors == null || transparentPixel >= colors.Count)
if (colors != null && transparentPixel < colors.Count)
{ {
return source;
}
colors[transparentPixel] = Colors.Transparent; colors[transparentPixel] = Colors.Transparent;
var stride = (source.PixelWidth * bpp + 7) / 8; var stride = (source.PixelWidth * source.Format.BitsPerPixel + 7) / 8;
var buffer = new byte[stride * source.PixelHeight]; var buffer = new byte[stride * source.PixelHeight];
source.CopyPixels(buffer, stride, 0); source.CopyPixels(buffer, stride, 0);
target = BitmapSource.Create( var target = BitmapSource.Create(
source.PixelWidth, source.PixelHeight, source.DpiX, source.DpiY, source.PixelWidth, source.PixelHeight, source.DpiX, source.DpiY,
format, new BitmapPalette(colors), buffer, stride); targetFormat, new BitmapPalette(colors), buffer, stride);
target.Freeze(); target.Freeze();
}
return target; return target;
} }