問題描述
如何使用 GDI(+) 在內存中渲染漸變 (How to render a gradient in memory using GDI(+))
I am trying to render an Image object in memory with the dimensions 1x16. This image is used as a tiled background. The gradient itself should have 3 colors in a non‑linear fashion.
Pixel 1 to 6: Gradient Color 1 to Color 2
Pixel 7 to 16: Gradient Color 3 to Color 4
‑‑‑‑‑
參考解法
方法 1:
I just found out myself how to do it. I was expecting an answer like this:
Bitmap bmp = new Bitmap(1, 16);
Graphics g = Graphics.FromImage(bmp);
System.Drawing.Drawing2D.LinearGradientBrush b1 =
new System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(0, 0, 1, 6),
Color1,
Color2,
System.Drawing.Drawing2D.LinearGradientMode.Vertical);
System.Drawing.Drawing2D.LinearGradientBrush b2 =
new System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(0, 7, 1, 16),
Color3,
Color4,
System.Drawing.Drawing2D.LinearGradientMode.Vertical);
g.FillRectangle(b1, new Rectangle(0, 0, 1, 6));
g.FillRectangle(b2, new Rectangle(0, 7, 1, 16));
g.Dispose();
The Bitmap bmp has now the 2 gradient.
方法 2:
You could use the GradientFill function.
For a custom solution, see if this article can help.
(by Stefan Koell、Stefan Koell、kgiannakakis)