【发布时间】:2015-09-02 16:22:13
【问题描述】:
我想创建一个具有 7 步颜色和自定义大小的线性渐变 - 从黑色、蓝色、青色、绿色、黄色、红色到白色。我的问题是最终的位图右侧有一条黑色条纹。有谁知道这是怎么回事?
public static List<Color> interpolateColorScheme(int size)
{
// create result list with for interpolated colors
List<Color> colorList = new List<Color>();
// use Bitmap and Graphics from bitmap
using (Bitmap bmp = new Bitmap(size, 200))
using (Graphics G = Graphics.FromImage(bmp))
{
// create empty rectangle canvas
Rectangle rect = new Rectangle(Point.Empty, bmp.Size);
// use LinearGradientBrush class for gradient computation
LinearGradientBrush brush = new LinearGradientBrush
(rect, Color.Empty, Color.Empty, 0, false);
// setup ColorBlend object
ColorBlend colorBlend = new ColorBlend();
colorBlend.Positions = new float[7];
colorBlend.Positions[0] = 0;
colorBlend.Positions[1] = 1 / 6f;
colorBlend.Positions[2] = 2 / 6f;
colorBlend.Positions[3] = 3 / 6f;
colorBlend.Positions[4] = 4 / 6f;
colorBlend.Positions[5] = 5 / 6f;
colorBlend.Positions[6] = 1;
// blend colors and copy them to result color list
colorBlend.Colors = new Color[7];
colorBlend.Colors[0] = Color.Black;
colorBlend.Colors[1] = Color.Blue;
colorBlend.Colors[2] = Color.Cyan;
colorBlend.Colors[3] = Color.Green;
colorBlend.Colors[4] = Color.Yellow;
colorBlend.Colors[5] = Color.Red;
colorBlend.Colors[6] = Color.White;
brush.InterpolationColors = colorBlend;
G.FillRectangle(brush, rect);
bmp.Save("gradient_debug_image_sarcus.png", ImageFormat.Png);
for (int i = 0; i < size; i++) colorList.Add(bmp.GetPixel(i, 0));
brush.Dispose();
}
// return interpolated colors
return colorList;
}
【问题讨论】:
-
一个可能愚蠢的解决方法是尝试
brush.WrapMode = WrapMode.TileFlipX,但我假设您正在寻找一些关于为什么原始代码没有按照所有外观应该做的事情的见解。 -
即使有证明图像,我也测试了您的代码,右侧没有黑线 (imgur.com/fI2uaQ5)。您发布的代码与用于生成图像的代码相比是否有任何更改? (我改变的只是保存路径和
new Bitmap(size, 200)>new Bitmap(size, 10) -
@BerndLinde 这绝对不应该有所作为,但是......你在将其更改为 10 之前尝试了 200 吗?
-
@JerryFederspiel 我做到了,结果相同,没有黑线。我降低了高度以更准确地反映样张图像
-
如您所见,位图生成并保存在此函数中。实际上,我将图像与 new Bitmap(size, 10) 放在一起并忘记了它。不过不管怎样,位图的高度真的和这个该死的黑条有关系吗?