Official Everybody Edits Forums

Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?

You are not logged in.

#1 2015-09-25 03:18:33, last edited by SirJosh3917 (2015-09-26 03:00:02)

SirJosh3917
Formerly ninjasupeatsninja
From: USA
Joined: 2015-04-05
Posts: 2,095

[Resolved] About Graphics

answer:

		public void InitializationCode()
		{
			Debug.WriteLine("\n");
			Debug.WriteLine("InitializationCode() Activated");
			int sizeW = GameFrame.Size.Width;
			int sizeH = GameFrame.Size.Height;
			Debug.WriteLine(sizeW.ToString() + "//" + sizeH.ToString());
			Bitmap a = new Bitmap(sizeW, sizeH);
			if (SetupGame.FillScreen)
			{
				for (int y = 0; y < a.Height; y++)
				{
					for (int x = 0; x < a.Width; x++)
					{
						a.SetPixel(x, y, SetupGame.FillScreenColor);
					}
				}
				GUI = a;
				GameFrame.Image = GUI;
			}
		}

Hello. I am trying to make a game template, mainly because I'm interested with Bitmap, PictureBox, and Graphics. I have the following code:

			GUI = new Bitmap(GameFrame.Image.Width, GameFrame.Image.Width);
			if (SetupGame.FillScreen)
			{
				Graphics g = Graphics.FromImage(GUI);
				g.FillRectangle(new SolidBrush(SetupGame.FillScreenColor), new Rectangle(0, 0, GameFrame.Image.Width - 2, GameFrame.Image.Height - 2));
				g.Save();
				GUI = new Bitmap(GameFrame.Image.Width, GameFrame.Image.Width, g);
				GameFrame.Image = GUI;
				g.Dispose();
			}
public static Bitmap GUI;
	class SetupGame
	{

		/*
		 * 
		 * Setup the game using these options
		 * 
		 * Just modify them, the game will take place
		 * 
		 * From [cencored my irl name], 9/24/2015 10:06 PM
		 * 
		 */

		public static bool FillScreen = true;
		public static Color FillScreenColor = Color.Red;
	}

I cannot find out what's going on.
The current code as stated in the first code box doesn't draw a rectangle. The width and height of the GameFrame (Picturebox) is 484, 462

Offline

#2 2015-09-25 03:29:49, last edited by hummerz5 (2015-09-25 03:35:08)

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,853

Re: [Resolved] About Graphics

I don't feel particularly inclined to do all the steps required to properly debug your code

Personally I haven't ever created a new bitmap on top of one that I've already created.
Seems backwards.

I'm thinking your bug is due to the misspelling of the word "censor".

edit:
Ok, turns out I am motivated enough to humor this bug
And yes, it's what I thought it was.
Why do you even bother making GUI a larger-scope variable if you're going to completely pull a new bitmap every time?

Offline

#3 2015-09-25 03:52:22

SirJosh3917
Formerly ninjasupeatsninja
From: USA
Joined: 2015-04-05
Posts: 2,095

Re: [Resolved] About Graphics

hummerz5 wrote:

Why do you even bother making GUI a larger-scope variable if you're going to completely pull a new bitmap every time?

This is the bigger scope

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Image_Game
{
	public partial class Form1 : Form
	{



		public Form1()
		{
			InitializeComponent();
		}

		public static Bitmap GUI;

		private void Form1_Load(object sender, EventArgs e)
		{
			GUI = new Bitmap(GameFrame.Image.Width, GameFrame.Image.Width);
			if (SetupGame.FillScreen)
			{
				Graphics g = Graphics.FromImage(GUI);
				g.FillRectangle(new SolidBrush(SetupGame.FillScreenColor), new Rectangle(0, 0, GameFrame.Image.Width - 2, GameFrame.Image.Height - 2));
				g.Save();
				GUI = new Bitmap(GameFrame.Image.Width, GameFrame.Image.Width, g);
				GameFrame.Image = GUI;
				g.Dispose();
			}
		}
	}
}

Offline

#4 2015-09-25 03:55:38

capasha
Member
Joined: 2015-02-21
Posts: 4,066

Re: [Resolved] About Graphics

You can't dispose GUI when it's loaded in the picturebox.

Offline

#5 2015-09-25 13:43:25

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,853

Re: [Resolved] About Graphics

@capasha: basically that. really.

@ninja: Alright, so you're initializing your image. Closer to reasonable. Yet you're still overwriting the whole thing... not sure why

Offline

#6 2015-09-25 21:37:43

SirJosh3917
Formerly ninjasupeatsninja
From: USA
Joined: 2015-04-05
Posts: 2,095

Re: [Resolved] About Graphics

I tried removing "g.dispose()" and that did nothing. I now have this code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace Image_Game
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		public Bitmap GUI;

		public void InitializationCode()
		{
			Debug.WriteLine("\n\n\n\n\n\n\n\n\n");
			Debug.WriteLine("Form Loaded");
			int sizeW = GameFrame.Size.Width;
			int sizeH = GameFrame.Size.Height;
			Debug.WriteLine(sizeW.ToString() + "//" + sizeH.ToString());
			Bitmap a = new Bitmap(sizeW, sizeH);
			if (SetupGame.FillScreen)
			{
				Graphics g = Graphics.FromImage(a);
				g.FillRectangle(new SolidBrush(SetupGame.FillScreenColor), new Rectangle(0, 0, sizeW - 2, sizeH - 2));
				a = new Bitmap(sizeW, sizeH, g);
				GameFrame.Image = a;
				GUI = a;
			}
			GameFrame.Refresh();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			InitializationCode();
		}

		private void MouseMoved(object sender, MouseEventArgs e)
		{

		}

		private void button1_Click(object sender, EventArgs e)
		{
			InitializationCode();
		}
	}
}

I am completely stumped.
Here's the debug data:

Form Loaded
484//462

Offline

#7 2015-09-25 22:07:01, last edited by hummerz5 (2015-09-25 22:08:06)

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,853

Re: [Resolved] About Graphics

But g.Dispose is the graphics, not the bitmap

What are you going on about?

srs tho, stop overwriting the bitmap and you'll be fine

Offline

#8 2015-09-25 22:08:32, last edited by SirJosh3917 (2015-09-25 22:09:13)

SirJosh3917
Formerly ninjasupeatsninja
From: USA
Joined: 2015-04-05
Posts: 2,095

Re: [Resolved] About Graphics

hummerz5 wrote:

But g.Dispose is the graphics, not the bitmap

What are you going on about?

you guys said i was disposing GUI, but I didn't see GUI.Dispose();
So I figured you were talking about the Graphics g.
If you are really talking about GUI, where did I dispose it?

EDIT:
I can't define the bitmap as a new bitmap with GameFrame.Size.Width or Height, So i have do define it at Form1_Load()

Offline

#9 2015-09-25 22:56:53

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,853

Re: [Resolved] About Graphics

I never said "Dispose" but ofc.

I'm somewhat curious why the forums have let this little conversation take so long
The problem is rather plain as day, yet here we are, discussing it to no avail. I wonder how far we can push it?

Major hint: you aren't defining it once.

Offline

#10 2015-09-25 23:42:54

SirJosh3917
Formerly ninjasupeatsninja
From: USA
Joined: 2015-04-05
Posts: 2,095

Re: [Resolved] About Graphics

hummerz5 wrote:

I never said "Dispose" but ofc.

I'm somewhat curious why the forums have let this little conversation take so long
The problem is rather plain as day, yet here we are, discussing it to no avail. I wonder how far we can push it?

Major hint: you aren't defining it once.

I don't understand what you mean by I'm not defining it once. I'll ask on stack over flow
@mods: you can lock if you choose to.

Offline

#11 2015-09-26 02:14:53

capasha
Member
Joined: 2015-02-21
Posts: 4,066

Re: [Resolved] About Graphics

Also. Remove "a = new Bitmap(sizeW, sizeH, g);" and everything should work.
I have no idea why you are using it two times.

Offline

#12 2015-09-26 02:20:14

SirJosh3917
Formerly ninjasupeatsninja
From: USA
Joined: 2015-04-05
Posts: 2,095

Re: [Resolved] About Graphics

capasha wrote:

Also. Remove "a = new Bitmap(sizeW, sizeH, g);" and everything should work.
I have no idea why you are using it two times.

I do it twice because I need to convert the new generated graphics into a bitmap

Offline

#13 2015-09-26 02:59:24

SirJosh3917
Formerly ninjasupeatsninja
From: USA
Joined: 2015-04-05
Posts: 2,095

Re: [Resolved] About Graphics

I have decided to simply do

		public void InitializationCode()
		{
			Debug.WriteLine("\n");
			Debug.WriteLine("InitializationCode() Activated");
			int sizeW = GameFrame.Size.Width;
			int sizeH = GameFrame.Size.Height;
			Debug.WriteLine(sizeW.ToString() + "//" + sizeH.ToString());
			Bitmap a = new Bitmap(sizeW, sizeH);
			if (SetupGame.FillScreen)
			{
				for (int y = 0; y < a.Height; y++)
				{
					for (int x = 0; x < a.Width; x++)
					{
						a.SetPixel(x, y, SetupGame.FillScreenColor);
					}
				}
				GUI = a;
				GameFrame.Image = GUI;
			}
		}

It seems to be doing well and fast.
@mods: lock plz

Offline

Wooted by:
SirJosh39171443232764544053

Board footer

Powered by FluxBB

[ Started around 1732454915.7364 - Generated in 0.094 seconds, 12 queries executed - Memory usage: 1.53 MiB (Peak: 1.7 MiB) ]