Reputation: 61
I have an example of code which creates a MIP map for DDS image. Original:
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;
}
I tried to refactor this method by changing System.Drawing.Bitmap
to 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()));
}
So now it looks like a double conversion. I'm trying to to get rid of that - get rid of Bitmap
and replace it by BitmapSource
, but the problem is that I have not the foggiest idea what's going on inside this method (found it somewhere), except the fact that it's working correctly.
Could anyone be so kind as to provide any solution how can I do this?
Upvotes: 0
Views: 80