我有一个为 DDS 图像创建 MIP 映射的代码示例。原文:
private static Bitmap CreateMipMap(Bitmap image, int width, int height)
{
var bmp = new Bitmap(width, height);
using var blitter = Graphics.FromImage(bmp);
blitter.InterpolationMode = InterpolationMode.HighQualityBicubic;
using var wrapMode = new ImageAttributes();
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
blitter.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
return bmp;
}
我试图通过将
System.Drawing.Bitmap
更改为 System.Windows.Media.Imaging.BitmapSource
来重构此方法:
private static BitmapSource CreateMipMap(BitmapSource image, int width, int height)
{
var bmp = new Bitmap(width, height);
using var blitter = Graphics.FromImage(bmp);
blitter.InterpolationMode = InterpolationMode.HighQualityBicubic;
using var wrapMode = new ImageAttributes();
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
using (var outStream = new MemoryStream())
{
var enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(image));
enc.Save(outStream);
var bitmap = new Bitmap(outStream);
blitter.DrawImage(bitmap, new Rectangle(0, 0, width, height), 0, 0, image.PixelWidth, image.PixelHeight, GraphicsUnit.Pixel, wrapMode);
}
return new WriteableBitmap(Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
}
所以现在看起来像是双重转换。我试图摆脱它 - 摆脱
Bitmap
并将其替换为BitmapSource
,但问题是我不知道这个方法内部发生了什么(在某处找到它),除了事实上它工作正常。
任何人都可以提供任何解决方案我该怎么做?