From 4837385037edf8a7d0b285ad7e60cc470d341e57 Mon Sep 17 00:00:00 2001
From: MaxED <j.maxed@gmail.com>
Date: Mon, 18 Jan 2016 11:02:03 +0000
Subject: [PATCH] Fixed, Visual mode: fixed a crash when creating classic
 skybox texture when sky texture's height was less than 28 pixels.

---
 Source/Core/Data/DataManager.cs | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/Source/Core/Data/DataManager.cs b/Source/Core/Data/DataManager.cs
index 2dd68a9a8..6737305ea 100644
--- a/Source/Core/Data/DataManager.cs
+++ b/Source/Core/Data/DataManager.cs
@@ -2425,7 +2425,10 @@ namespace CodeImp.DoomBuilder.Data
 		{
 			// Get averaged top and bottom colors from the original image
 			int tr = 0, tg = 0, tb = 0, br = 0, bg = 0, bb = 0;
-			const int colorsampleheight = 28; // TODO: is this value calculated from the image's height?
+			const int defaultcolorsampleheight = 28;
+			int colorsampleheight = Math.Max(1, Math.Min(defaultcolorsampleheight, img.Height / 2)); // TODO: is this value calculated from the image's height?
+			bool dogradients = colorsampleheight < img.Height / 2;
+
 			for(int w = 0; w < img.Width; w++)
 			{
 				for(int h = 0; h < colorsampleheight; h++)
@@ -2481,19 +2484,22 @@ namespace CodeImp.DoomBuilder.Data
 			}
 
 			// Apply top/bottom gradients
-			using(Graphics g = Graphics.FromImage(skyimage))
+			if(dogradients)
 			{
-				Rectangle area = new Rectangle(0, 0, skyimage.Width, colorsampleheight);
-				using(LinearGradientBrush b = new LinearGradientBrush(area, topcolor, Color.FromArgb(0, topcolor), 90f))
+				using(Graphics g = Graphics.FromImage(skyimage))
 				{
-					g.FillRectangle(b, area);
-				}
+					Rectangle area = new Rectangle(0, 0, skyimage.Width, colorsampleheight);
+					using(LinearGradientBrush b = new LinearGradientBrush(area, topcolor, Color.FromArgb(0, topcolor), 90f))
+					{
+						g.FillRectangle(b, area);
+					}
 
-				area = new Rectangle(0, skyimage.Height - colorsampleheight, skyimage.Width, colorsampleheight);
-				using(LinearGradientBrush b = new LinearGradientBrush(area, Color.FromArgb(0, bottomcolor), bottomcolor, 90f))
-				{
-					area.Y += 1;
-					g.FillRectangle(b, area);
+					area = new Rectangle(0, skyimage.Height - colorsampleheight, skyimage.Width, colorsampleheight);
+					using(LinearGradientBrush b = new LinearGradientBrush(area, Color.FromArgb(0, bottomcolor), bottomcolor, 90f))
+					{
+						area.Y += 1;
+						g.FillRectangle(b, area);
+					}
 				}
 			}
 
-- 
GitLab