Files
better-genshin-impact/BetterGenshinImpact/Helpers/Extensions/BitmapExtension.cs
Shatyuka 0bea2d095a 截图优化 (#1480)
* BitBlt 优化

* BitBlt恢复Top-down

* 渲染时翻转图像

* CaptureSession引用计数

* 恢复成无拷贝Mat

* 合法性检查

* 优化截图预览窗口

* 保存截图文件必要时需要克隆一份Mat

* BitBlt内存池

* 返回拷贝就不用对Session做引用计数了

* 移除CaptureImageRes

* 优化DirectX

* 更好地处理padding

* BitBlt去掉padding

1920*1080的游戏窗口是4字节对齐的,因此不会有性能影响。这里主要用于测试。

* 修复修改窗口大小

* 合并CreateStagingTexture

* 修复设备丢失崩溃

* WGC截图支持HDR

* fix typo

* CodeQA

* 去掉1px窗口边框

* DirectX截图去掉A通道

* HDR转换使用GPU加速

---------

Co-authored-by: 辉鸭蛋 <huiyadanli@gmail.com>
2025-05-11 01:17:18 +08:00

72 lines
2.3 KiB
C#

using OpenCvSharp;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Color = System.Drawing.Color;
namespace BetterGenshinImpact.Helpers.Extensions
{
public static class BitmapExtension
{
public static BitmapSource ToBitmapSource(this Bitmap bitmap)
{
return bitmap.ToBitmapSource(out _);
}
public static BitmapSource ToBitmapSource(this Bitmap bitmap, out bool bottomUp)
{
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
var stride = bitmapData.Stride;
var buffer = bitmapData.Scan0;
if (stride < 0)
{
bottomUp = true;
stride = -stride;
buffer -= stride * (bitmapData.Height - 1);
}
else
{
bottomUp = false;
}
var pixelFormat = bitmap.PixelFormat switch
{
System.Drawing.Imaging.PixelFormat.Format24bppRgb => PixelFormats.Bgr24,
System.Drawing.Imaging.PixelFormat.Format32bppArgb => PixelFormats.Bgra32,
_ => throw new NotSupportedException($"Unsupported pixel format {bitmap.PixelFormat}")
};
var bitmapSource = BitmapSource.Create(
bitmapData.Width, bitmapData.Height,
bitmap.HorizontalResolution, bitmap.VerticalResolution,
pixelFormat, null,
buffer, stride * bitmapData.Height, stride);
bitmap.UnlockBits(bitmapData);
return bitmapSource;
}
public static BitmapImage ToBitmapImage(this Bitmap bitmap)
{
var ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
var image = new BitmapImage();
image.BeginInit();
ms.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms;
image.EndInit();
return image;
}
public static Scalar ToScalar(this Color color)
{
return new Scalar(color.R, color.G, color.B);
}
}
}