代码之家  ›  专栏  ›  技术社区  ›  Paul Sasik

如何创建简单的玻璃效果

  •  0
  • Paul Sasik  · 技术社区  · 14 年前

    我目前正在绘制一个浅蓝色,部分透明覆盖在业主绘制的对象,以表明某种状态。这没关系,但我想如果我能用某种玻璃效果来进一步确定某个物体的顶部覆盖着“某物”,那就更好了。

    我认为一些玻璃条纹,例如,除了蓝色的透明度将提供一个很好的效果。

    我在google上搜索了GDI+(和其他)算法来完成像这样简单的绘画,但结果都是空的。任何语言的任何(相当简单的)算法的链接将不胜感激。我更喜欢.NET,但可以从伪代码中找出这幅画。

    抱歉,我还应该指定我的目标是WinXP和使用.NET 2.0版-因此无法使用WPF或Vista/Win7 goodies。

    3 回复  |  直到 14 年前
        1
  •  1
  •   user384929 user384929    14 年前

    我自己没有做过,但是,我使用codeproject source呈现了一个示例…请尝试以下操作:

    http://www.codeproject.com/KB/GDI-plus/Image-Glass-Reflection.aspx

    public static Image DrawReflection(Image _Image, Color _BackgroundColor, int _Reflectivity)
    {
        // Calculate the size of the new image
        int height = (int)(_Image.Height + (_Image.Height * ((float)_Reflectivity / 255)));
        Bitmap newImage = new Bitmap(_Image.Width, height, PixelFormat.Format24bppRgb);
        newImage.SetResolution(_Image.HorizontalResolution, _Image.VerticalResolution);
    
        using (Graphics graphics = Graphics.FromImage(newImage))
        {
            // Initialize main graphics buffer
            graphics.Clear(_BackgroundColor);
            graphics.DrawImage(_Image, new Point(0, 0));
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Rectangle destinationRectangle = new Rectangle(0, _Image.Size.Height, 
                                             _Image.Size.Width, _Image.Size.Height);
    
            // Prepare the reflected image
            int reflectionHeight = (_Image.Height * _Reflectivity) / 255;
            Image reflectedImage = new Bitmap(_Image.Width, reflectionHeight);
    
            // Draw just the reflection on a second graphics buffer
            using (Graphics gReflection = Graphics.FromImage(reflectedImage))
            {
                gReflection.DrawImage(_Image, 
                   new Rectangle(0, 0, reflectedImage.Width, reflectedImage.Height),
                   0, _Image.Height - reflectedImage.Height, reflectedImage.Width, 
                   reflectedImage.Height, GraphicsUnit.Pixel);
            }
            reflectedImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
            Rectangle imageRectangle = 
                new Rectangle(destinationRectangle.X, destinationRectangle.Y,
                destinationRectangle.Width, 
                (destinationRectangle.Height * _Reflectivity) / 255);
    
            // Draw the image on the original graphics
            graphics.DrawImage(reflectedImage, imageRectangle);
    
            // Finish the reflection using a gradiend brush
            LinearGradientBrush brush = new LinearGradientBrush(imageRectangle,
                   Color.FromArgb(255 - _Reflectivity, _BackgroundColor),
                    _BackgroundColor, 90, false);
            graphics.FillRectangle(brush, imageRectangle);
        }
    
        return newImage;
    }
    
        2
  •  1
  •   Paul Sasik    14 年前

    实际上,我能够实现一个基本的玻璃效果,我的图像覆盖了一个矩形约三分之一的大小,下面的图像包含一个白色渐变填充,从25%的不透明度开始,到75%的不透明度。这是一个单一的绘画位产生玻璃状的“条纹”,我很高兴。同样的想法可以用不同的矩形宽度重复多次,产生几个“条纹”,给人一种玻璃覆盖层的错觉。