You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为DDS图像创建MIP贴图:用BitmapSource替代Bitmap的优化方案求助

纯BitmapSource实现DDS Mip贴图生成方案

问题背景

原有基于System.Drawing.Bitmap的Mip贴图生成逻辑,重构时尝试改用System.Windows.Media.Imaging.BitmapSource但存在BitmapSource ↔ Bitmap的双重转换,需要彻底移除对System.Drawing的依赖,仅通过WPF的BitmapSource完成Mip贴图创建。

解决方案代码

using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

private static BitmapSource CreateMipMap(BitmapSource source, int targetWidth, int targetHeight)
{
    // 创建绘制容器
    var drawingVisual = new DrawingVisual();
    using (var drawingContext = drawingVisual.RenderOpen())
    {
        // 创建对应TileFlipXY环绕模式的ImageBrush
        var imageBrush = new ImageBrush(source)
        {
            TileMode = TileMode.Tile,
            FlipMode = FlipMode.XY,
            Stretch = Stretch.Fill,
            InterpolationMode = BitmapInterpolationMode.HighQuality
        };

        // 绘制到目标尺寸区域
        drawingContext.DrawRectangle(imageBrush, null, new Rect(0, 0, targetWidth, targetHeight));
    }

    // 渲染为BitmapSource
    var renderTarget = new RenderTargetBitmap(
        targetWidth,
        targetHeight,
        96,
        96,
        PixelFormats.Pbgra32);
    renderTarget.Render(drawingVisual);

    // 冻结BitmapSource提升性能
    renderTarget.Freeze();
    return renderTarget;
}

关键说明

  1. 环绕模式匹配:通过ImageBrush的TileMode=Tile+FlipMode=XY,完全对应原代码中WrapMode.TileFlipXY的纹理采样效果。
  2. 插值质量对齐:BitmapInterpolationMode.HighQuality与原代码的HighQualityBicubic插值效果一致,保证缩放后的图像质量。
  3. 彻底移除System.Drawing依赖:全程使用WPF原生的DrawingVisual、RenderTargetBitmap完成绘制与渲染,无任何System.Drawing相关调用。
  4. 性能优化:调用Freeze()将生成的BitmapSource转为只读状态,提升WPF场景下的使用性能。

内容的提问来源于stack exchange,提问作者Vlad i Slav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 19:07:58