Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Create Gradient Image

Tags:

c#

.net

How can I create a gradient image (with a given height & width, start color and end color) using C#? Does anybody have a simple sample snippet? Thank you!

like image 394
Alex Avatar asked Sep 29 '09 17:09

Alex


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

You can do this using a LinearGradientBrush. For example

// using System.Drawing;
// using System.Drawing.Imaging;
// using System.Drawing.Drawing2D;

public static void OutputGradientImage()
{
    using (Bitmap bitmap = new Bitmap(100, 100)) // 100x100 pixels
    using (Graphics graphics = Graphics.FromImage(bitmap))
    using (LinearGradientBrush brush = new LinearGradientBrush(
        new Rectangle(0, 0, 100, 100),
        Color.Blue,
        Color.Red,
        LinearGradientMode.Vertical))
    {
        brush.SetSigmaBellShape(0.5f);
        graphics.FillRectangle(brush, new Rectangle(0, 0, 100, 100));
        bitmap.Save("gradientImage.jpg", ImageFormat.Jpeg);
    }
}
like image 168
Reed Copsey Avatar answered Sep 29 '22 15:09

Reed Copsey


LinearGradientBrush is your friend here:


    Bitmap bmp = new Bitmap(Width, Height);
    Graphics g = Graphics.FromImage(bmp);
    LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), new Point(Width, Height), Color.Black, Color.Red);
    g.FillRectangle(lgb, 0, 0, Width, Height);
    bmp.Save("FileName");
    lgb.Dispose();
    g.Dispose();
    bmp.Dispose();

like image 44
Jacob G Avatar answered Sep 29 '22 16:09

Jacob G