I'm using this function to generate a simple gradient bitmap using LinearGradientBrush which I save as a file in the end to use as a wallpaper. But the the problem is the result doesn't look "smooth", it looks rather "choppy" as you can see the colors' lines.
I use the bounds of the screen as the dimensions of the bitmap and increasing the size doesn't improve the quality (I tried 2x and even 4x).
I tried setting the InterpolationMode and SmoothingMode but neither seemed to have an effect on the end result.
Is there a way to fix this issue?
public static Bitmap GenerateGradient(Color color1, Color color2, int width, int height, LinearGradientMode mode)
{
Bitmap bitmap = new(width, height);
using (Graphics graphics = Graphics.FromImage(bitmap))
using (LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, width, height), color1, color2, mode))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.FillRectangle(brush, new Rectangle(0, 0, width, height));
}
return bitmap;
}
Edit: Here's how I call the function and save the bitmap, after re-checking the code I think the issue lies mainly with how I generate the color shades. After using named Colors instead generated shades, the result was much smoother.
//Generates a random shade of the specified color
//by randomly adjusting the alpha value.
public static Color RandomShade(Color color)
{
var random = new Random(DateTime.Now.Millisecond);
int alpha = random.Next(0, 255);
return Color.FromArgb(alpha, color);
}
private void buttonGenerate_Click(object sender, EventArgs e)
{
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
Color color1 = RandomShade(Color.AliceBlue);
Color color2 = RandomShade(Color.Navy);
var bitmap = GenerateGradient(color1, color2, width, height, LinearGradientMode.ForwardDiagonal);
bitmap.Save("wallpaper.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}

