【问题标题】:Winforms C# Gradient Opacity bitmapWinforms C# 渐变不透明度位图
【发布时间】:2014-04-22 13:29:42
【问题描述】:

我想制作一个应用了线性不透明度的位图。 (即,它在左侧更加不透明,并且随着靠近右侧逐渐变得不透明。)

这可能吗?我知道它可能具有恒定的不透明度级别。

【问题讨论】:

    标签: c# .net winforms controls opacity


    【解决方案1】:

    将位图放入PictureBox 控件中。然后您将不得不使用PictureBoxPaint 事件处理程序来进行淡入淡出。像

    private void pictureBox_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(pictureBox.Image, 0, 0, 
            pictureBox.ClientRectangle, GraphicsUnit.Pixel);
        Color left = Color.FromArgb(128, Color.Blue);
        Color right = Color.FromArgb(128, Color.Red);
        LinearGradientMode direction = LinearGradientMode.Horizontal;
        LinearGradientBrush brush = new LinearGradientBrush(
            pictureBox.ClientRectangle, left, right, direction);
        e.Graphics.FillRectangle(brush, pictureBox.ClientRectangle);
    }
    

    此示例将图像叠加层从蓝色渐变为红色。我相信您可以使用它来获得您想要的工作。

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我知道它可以有一个恒定的不透明度水平

      所以不要让它保持不变,LinearGradientBrush 可以毫无问题地插入 alpha 值。设置了 BackgroundImage 的表单的简单演示:

          protected override void OnPaint(PaintEventArgs e) {
              base.OnPaint(e);
              var rc = new Rectangle(20, 20, this.ClientSize.Width - 40, 50);
              using (var brush = new System.Drawing.Drawing2D.LinearGradientBrush(
                  rc, 
                  Color.FromArgb(255, Color.BlueViolet), 
                  Color.FromArgb(0,   Color.BlueViolet), 
                  0f)) {
                      e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                      e.Graphics.FillRectangle(brush, rc);
              }
          }
      

      制作:

      【讨论】:

        猜你喜欢
        • 2011-02-07
        • 2016-10-28
        • 2014-07-21
        • 2013-03-13
        • 1970-01-01
        • 1970-01-01
        • 2016-12-13
        • 2015-09-20
        • 1970-01-01
        相关资源
        最近更新 更多