.net winform 实现CSS3.0 泼墨画效果

效果图
代码
csharp 复制代码
private unsafe void BlendImages1(Bitmap img1, Bitmap img2)
{
    // 确定两个图像的重叠区域
    Rectangle rect = new Rectangle(0, 0,
        Math.Min(img1.Width, img2.Width),
        Math.Min(img1.Height, img2.Height));

    // 创建输出图像,尺寸为重叠区域大小
    Bitmap blendedImage = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);

    // Lock the bits of each image and get the BitmapData.
    BitmapData data1 = img1.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    BitmapData data2 = img2.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    BitmapData dataBlended = blendedImage.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

    int bytesPerPixel = 4; // For Format32bppArgb
    int stride1 = data1.Stride;
    int stride2 = data2.Stride;
    int strideBlended = dataBlended.Stride;

    byte* ptr1 = (byte*)data1.Scan0.ToPointer();
    byte* ptr2 = (byte*)data2.Scan0.ToPointer();
    byte* ptrBlended = (byte*)dataBlended.Scan0.ToPointer();

    for (int y = 0; y < rect.Height; ++y)
    {
        byte* rowPtr1 = ptr1 + y * stride1;
        byte* rowPtr2 = ptr2 + y * stride2;
        byte* rowPtrBlended = ptrBlended + y * strideBlended;

        for (int x = 0; x < rect.Width; ++x)
        {
            int pixelOffset = x * bytesPerPixel;
            if (pixelOffset + bytesPerPixel <= Math.Abs(stride1) &&
                pixelOffset + bytesPerPixel <= Math.Abs(stride2) &&
                pixelOffset + bytesPerPixel <= Math.Abs(strideBlended))
            {
                rowPtrBlended[pixelOffset] = (byte)(255 - ((255 - rowPtr1[pixelOffset]) * (255 - rowPtr2[pixelOffset]) >> 8)); // B
                rowPtrBlended[pixelOffset + 1] = (byte)(255 - ((255 - rowPtr1[pixelOffset + 1]) * (255 - rowPtr2[pixelOffset + 1]) >> 8)); // G
                rowPtrBlended[pixelOffset + 2] = (byte)(255 - ((255 - rowPtr1[pixelOffset + 2]) * (255 - rowPtr2[pixelOffset + 2]) >> 8)); // R
                rowPtrBlended[pixelOffset + 3] = (byte)255; // A
            }
        }
    }

    // Unlock the bits of each image after processing.
    img1.UnlockBits(data1);
    img2.UnlockBits(data2);
    blendedImage.UnlockBits(dataBlended);

    // Display the blended image.
    pictureBoxResult.Image = blendedImage;
}

素材


相关推荐
HarderCoder3 分钟前
ByAI:Rect-redux实现及connect函数
前端·react.js
小张快跑。5 分钟前
【Vue3】(三)vue3中的pinia状态管理、组件通信
前端·javascript·vue.js
我想说一句5 分钟前
当 map 遇上 parseInt:JS 中一场参数引发的“血案”
前端·javascript·面试
陈随易6 分钟前
2025年100个产品计划之第12个(杰森排序) - 对 JSON 属性进行排序
前端·后端·程序员
LeeAt6 分钟前
《谁杀死了比尔?》:使用Trae完成的一个推理游戏项目!!
前端·游戏开发·trae
Hockor10 分钟前
写给前端的 Python 教程四(列表/元组)
前端·后端·python
GetcharZp11 分钟前
「DPlayer」超强弹幕视频播放器来了!支持m3u8直播,5分钟搞定集成!
前端
harry235day13 分钟前
Compose 带动画的待办清单列表页
android·android jetpack
vocal13 分钟前
我的安卓第一课:四大组件之一Activity及其组件RecyclerView
android
天天码行空14 分钟前
Bootstrap Table企业级web数据表格集成框架
前端·javascript·开源