如何使用 GDI(+) 在內存中渲染漸變 (How to render a gradient in memory using GDI(+))


問題描述

如何使用 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 KoellStefan Koellkgiannakakis)

參考文件

  1. How to render a gradient in memory using GDI(+) (CC BY‑SA 3.0/4.0)

#gdi #graphics #winforms #gdi+






相關問題

繪製鼠標光標 (Draw mouse cursor)

C 和 Windows GDI 中的雙緩衝 *framework* (Double-buffering *framework* in C and Windows GDI)

一個進程的 GDI 洩漏會影響其他進程嗎? (Can GDI leaks from one process affect other processes?)

BeginPath Textout EndPath 繪製反轉文本 (BeginPath Textout EndPath draws inverted text)

監控GDI通話 (Monitoring GDI calls)

如何捕獲(並希望修復)GDI 資源洩漏 (How to catch (and hopefully fix) a GDI resource leak)

如何使用 GDI(+) 在內存中渲染漸變 (How to render a gradient in memory using GDI(+))

使用 BITMAP::bmBits 與 GetDIBits 有什麼區別? (What is the difference between using BITMAP::bmBits vs GetDIBits?)

獲取背景窗口的縮略圖 (Get Thumbnail of background window)

AlphaBlend 返回“假”的原因可能是什麼 (What could be the reason for AlphaBlend to return 'false')

在 C++ 中查找像素 RGB 數據的最快方法是什麼? (What is the fastest method to lookup pixel RGB data in C++?)

逐行讀取文本框並將其視為單獨的文本 (Read a textbox line by line and treat it as a separate text)







留言討論