我有一个多色渐变,它由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:
How your unmodified code looked:
更多推荐
发布评论