多色渐变不显示所有颜色(Multi

编程入门 行业动态 更新时间:2024-10-23 02:04:02
多色渐变不显示所有颜色(Multi-color gradient does not show all colors)

我有一个多色渐变,它由14种颜色组成。 这是我的代码:

panel1.Paint += new PaintEventHandler(panel1_Paint); panel1.Refresh(); private void panel1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle, Color.FromArgb(255, 0, 0, 0), Color.FromArgb(255, 100, 100, 200),0, false); System.Drawing.Drawing2D.ColorBlend cb = new System.Drawing.Drawing2D.ColorBlend(); cb.Positions = new[] { 0, 1 /15f, 2 / 15f, 3 / 15f, 4 / 15f, 5 / 15f, 6 / 15f, 7 / 15f, 8 / 15f, 9 / 15f, 10 / 15f, 11 / 15f, 12 / 15f, 13 / 15f,1 }; cb.Colors = new[] { Color.FromArgb(255, 0, 0, 0), Color.FromArgb(255, 0, 0, 50), Color.FromArgb(255, 10, 10, 100), Color.FromArgb(255, 30, 30, 100), Color.FromArgb(255, 70, 70,200), Color.FromArgb(255, 100, 100, 255), Color.FromArgb(255, 170, 170, 255), Color.FromArgb(255, 55, 151, 107), Color.FromArgb(255, 117, 194, 103), Color.FromArgb(255, 230, 230, 128), Color.FromArgb(255, 202, 157, 75), Color.FromArgb(255, 185, 154, 100), Color.FromArgb(255, 220, 220, 220), Color.FromArgb(255, 255, 255, 255), Color.FromArgb(255, 100, 100, 200) }; br.InterpolationColors = cb; // rotate br.RotateTransform(90); // paint g.FillRectangle(br, this.ClientRectangle); }

不幸的是它没有显示所有的颜色。 我的矩形高度为100,但它显示如下渐变:

有什么问题,我该如何解决?

I have a multicolor gradient it consists of 14 colors. Here is my code:

panel1.Paint += new PaintEventHandler(panel1_Paint); panel1.Refresh(); private void panel1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle, Color.FromArgb(255, 0, 0, 0), Color.FromArgb(255, 100, 100, 200),0, false); System.Drawing.Drawing2D.ColorBlend cb = new System.Drawing.Drawing2D.ColorBlend(); cb.Positions = new[] { 0, 1 /15f, 2 / 15f, 3 / 15f, 4 / 15f, 5 / 15f, 6 / 15f, 7 / 15f, 8 / 15f, 9 / 15f, 10 / 15f, 11 / 15f, 12 / 15f, 13 / 15f,1 }; cb.Colors = new[] { Color.FromArgb(255, 0, 0, 0), Color.FromArgb(255, 0, 0, 50), Color.FromArgb(255, 10, 10, 100), Color.FromArgb(255, 30, 30, 100), Color.FromArgb(255, 70, 70,200), Color.FromArgb(255, 100, 100, 255), Color.FromArgb(255, 170, 170, 255), Color.FromArgb(255, 55, 151, 107), Color.FromArgb(255, 117, 194, 103), Color.FromArgb(255, 230, 230, 128), Color.FromArgb(255, 202, 157, 75), Color.FromArgb(255, 185, 154, 100), Color.FromArgb(255, 220, 220, 220), Color.FromArgb(255, 255, 255, 255), Color.FromArgb(255, 100, 100, 200) }; br.InterpolationColors = cb; // rotate br.RotateTransform(90); // paint g.FillRectangle(br, this.ClientRectangle); }

Unfortunately it does not show all the colors. My rectangle's height is 100 but it shows a gradient like this:

What is wrong and how can I fix it?

最满意答案

如果要填充Rectangle Vertical,则应在LinearGradientBrush的构造函数中指定Rotation:

new LinearGradientBrush(this.ClientRectangle, colorFrom, colorTo, 90, false); Here --^

并删除转换

br.RotateTransform(90);

由于ClientRectangle的宽度和高度基本上由当前实现交换(因为旋转90°),因此您只能看到渐变的一小部分或整个渐变多次。

这也不起作用,因为您使用了错误的ClientRectangle 。 您在Form的代码隐藏中使用this.ClientRectangle 。 将每个this.ClientRectangle替换为panel1.ClientRectangle 。

这看起来应该如何:

在此处输入图像描述

未经修改的代码如何显示:

在此处输入图像描述

If you want to fill the Rectangle Vertical, you should specify the Rotation in the constructor of your LinearGradientBrush:

new LinearGradientBrush(this.ClientRectangle, colorFrom, colorTo, 90, false); Here --^

And remove the Transformation

br.RotateTransform(90);

Since the width and height of the ClientRectangle are basically swapped by your current implementation (because of the 90° rotation), you either see only a small part of the gradient or the entire gradient multiple times.

This also doesn't work because you use the wrong ClientRectangle. You are using this.ClientRectangle in your Form's code-behind. Replace every this.ClientRectangle with panel1.ClientRectangle.

How this should look:

enter image description here

How your unmodified code looked:

enter image description here

更多推荐

本文发布于:2023-07-08 19:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多色   颜色   Multi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!