gpt4 book ai didi

.net - WinForms 中的图层效果(模糊等)

转载 作者:行者123 更新时间:2023-12-01 08:40:38 25 4
gpt4 key购买 nike

在 WinForms 中,我正在寻找一种方法来实现类似于 Java 的 Swing 中 JXLayer 类的功能。
更具体。我想模糊窗口的整个内容,并在其上绘制一些东西(例如,等待圆圈)。
任何想法将不胜感激:)

最佳答案

这是我的解决方案。

  • 截屏。
  • 模糊它。
  • 将模糊的图片放在所有东西的前面。

  • 给定一个带有 Button button1 和 Panel panel1(上面有列表框和进度条)的表单,下面的代码工作得很好:
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Drawing.Imaging;

    namespace Blur
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    Bitmap bmp = Screenshot.TakeSnapshot(panel1);
    BitmapFilter.GaussianBlur(bmp, 4);

    PictureBox pb = new PictureBox();
    panel1.Controls.Add(pb);
    pb.Image = bmp;
    pb.Dock = DockStyle.Fill;
    pb.BringToFront();
    }
    }

    public class ConvMatrix
    {
    public int TopLeft = 0, TopMid = 0, TopRight = 0;
    public int MidLeft = 0, Pixel = 1, MidRight = 0;
    public int BottomLeft = 0, BottomMid = 0, BottomRight = 0;
    public int Factor = 1;
    public int Offset = 0;
    public void SetAll(int nVal)
    {
    TopLeft = TopMid = TopRight = MidLeft = Pixel = MidRight = BottomLeft = BottomMid = BottomRight = nVal;
    }
    }

    public class BitmapFilter
    {
    private static bool Conv3x3(Bitmap b, ConvMatrix m)
    {
    // Avoid divide by zero errors
    if (0 == m.Factor) return false;

    Bitmap bSrc = (Bitmap)b.Clone();

    // GDI+ still lies to us - the return format is BGR, NOT RGB.
    BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

    int stride = bmData.Stride;
    int stride2 = stride * 2;
    System.IntPtr Scan0 = bmData.Scan0;
    System.IntPtr SrcScan0 = bmSrc.Scan0;

    unsafe
    {
    byte* p = (byte*)(void*)Scan0;
    byte* pSrc = (byte*)(void*)SrcScan0;

    int nOffset = stride + 6 - b.Width * 3;
    int nWidth = b.Width - 2;
    int nHeight = b.Height - 2;

    int nPixel;

    for (int y = 0; y < nHeight; ++y)
    {
    for (int x = 0; x < nWidth; ++x)
    {
    nPixel = ((((pSrc[2] * m.TopLeft) + (pSrc[5] * m.TopMid) + (pSrc[8] * m.TopRight) +
    (pSrc[2 + stride] * m.MidLeft) + (pSrc[5 + stride] * m.Pixel) + (pSrc[8 + stride] * m.MidRight) +
    (pSrc[2 + stride2] * m.BottomLeft) + (pSrc[5 + stride2] * m.BottomMid) + (pSrc[8 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

    if (nPixel < 0) nPixel = 0;
    if (nPixel > 255) nPixel = 255;

    p[5 + stride] = (byte)nPixel;

    nPixel = ((((pSrc[1] * m.TopLeft) + (pSrc[4] * m.TopMid) + (pSrc[7] * m.TopRight) +
    (pSrc[1 + stride] * m.MidLeft) + (pSrc[4 + stride] * m.Pixel) + (pSrc[7 + stride] * m.MidRight) +
    (pSrc[1 + stride2] * m.BottomLeft) + (pSrc[4 + stride2] * m.BottomMid) + (pSrc[7 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

    if (nPixel < 0) nPixel = 0;
    if (nPixel > 255) nPixel = 255;

    p[4 + stride] = (byte)nPixel;

    nPixel = ((((pSrc[0] * m.TopLeft) + (pSrc[3] * m.TopMid) + (pSrc[6] * m.TopRight) +
    (pSrc[0 + stride] * m.MidLeft) + (pSrc[3 + stride] * m.Pixel) + (pSrc[6 + stride] * m.MidRight) +
    (pSrc[0 + stride2] * m.BottomLeft) + (pSrc[3 + stride2] * m.BottomMid) + (pSrc[6 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

    if (nPixel < 0) nPixel = 0;
    if (nPixel > 255) nPixel = 255;

    p[3 + stride] = (byte)nPixel;

    p += 3;
    pSrc += 3;
    }

    p += nOffset;
    pSrc += nOffset;
    }
    }

    b.UnlockBits(bmData);
    bSrc.UnlockBits(bmSrc);

    return true;
    }

    public static bool GaussianBlur(Bitmap b, int nWeight /* default to 4*/)
    {
    ConvMatrix m = new ConvMatrix();
    m.SetAll(1);
    m.Pixel = nWeight;
    m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 2;
    m.Factor = nWeight + 12;

    return BitmapFilter.Conv3x3(b, m);
    }
    }

    class Screenshot
    {
    public static Bitmap TakeSnapshot(Control ctl)
    {
    Bitmap bmp = new Bitmap(ctl.Size.Width, ctl.Size.Height);
    using (Graphics g = System.Drawing.Graphics.FromImage(bmp))
    {
    g.CopyFromScreen(
    ctl.PointToScreen(ctl.ClientRectangle.Location),
    new Point(0, 0), ctl.ClientRectangle.Size
    );
    }
    return bmp;
    }
    }
    }

    alt text alt text
    高斯模糊的代码是从 here 借来的.

    关于.net - WinForms 中的图层效果(模糊等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3598772/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com