Question Can't get image to draw

DarkD

Member
Joined
Jun 16, 2014
Messages
16
Programming Experience
Beginner
Simple question, trying to get a jpg to draw and it just won't work.

This is in my main section
C#:
            PictureBox p = new PictureBox();
            p.Width = 800;
            p.Height = 800;
            Tile t = new Tile(x_coord, y_coord);
            p.Paint += new PaintEventHandler(t.draw);
Here's my draw method.
C#:
using System;
using System.Drawing;
using System.Windows.Forms;


        public void draw(Object sender, PaintEventArgs drawEvent)
        {
            Image nTileImage = Image.FromFile(@"tile.jpg");


            Rectangle rectangleAreaToDrawImage = new Rectangle(x_position, y_position, nTileImage.Width, nTileImage.Height);
            // Draw image to screen.
            drawEvent.Graphics.DrawImage(nTileImage, rectangleAreaToDrawImage);
        }
I have the image included in the project in visual studio. Don't know if I did that right. I right clicked the project->Add->ExistingObject->tile.jpg
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,151
Location
Sydney, Australia
Programming Experience
10+
Does the code execute and is an exception thrown?

By the way, you shouldn't be creating the Image object inside the Paint event handler. That method can be executed many times but you don't want to keep creating new Image objects every time. Create the Image only once and assign it to a member variable. Don't forget to dispose it when you're done with it too.
 

DarkD

Member
Joined
Jun 16, 2014
Messages
16
Programming Experience
Beginner
I don't get the chance to find out, draw never executes. It passes over that line of code even when I click step into(F11) or add breakpoint inside of the method. Clearly I'm doing something wrong with PictureBox.

I have made the changes you recommended

main method

C#:
            PictureBox p = new PictureBox();
            p.Width = 800;
            p.Height = 800;
            Tile t = new Tile(1,1,1);
            p.Paint += new PaintEventHandler(t.draw);
            t.clean();
            return 0;
Tile draw methods

C#:
using System.Drawing;
using System.Windows.Forms;


namespace ConsoleApplication1.GameBoard
{
    public class Tile
    {


        private Image nTileImage;


        public Tile(params) 
        {            nTileImage = Image.FromFile(@"ExternalDriveLetter:\...\ConsoleApplication1\Resources\tile.jpg");
            
        }



        public void draw(Object sender, PaintEventArgs drawEvent)
        {
            Rectangle rectangleAreaToDrawImage = new Rectangle(x_position, y_position, nTileImage.Width, nTileImage.Height);
            // Draw image to screen.
            drawEvent.Graphics.DrawImage(nTileImage, rectangleAreaToDrawImage);
        }


        public void clean() 
        {
            nTileImage.Dispose();
        }
It was crashing when I moved the image creation thing to the constructor so I changed it to the absolute path which works, but I don't like it. Also you may have noticed that I changed how I'm including the image to be a resource in my project now. Read a tutorial for that somewhere. What would the relative path for this be?

My overall problem is that I am completely new to C# graphics, and I just need to get images to draw. my intent for later is to draw a boardgame and allow people to play and rotate the gameboard. So eventually I'll need methods to draw the various images at angles and such, but for now I just want to get the basic draw image done.

Just so you know the level of how lacking in knowledge I am. I have absolutely no code relating to forms. I have no idea why that's the only code example I can find, one which says forms are needed, but all I wanna do is draw images that I can manipulate. I am making a boardgame here, not a document. So if I'm using the wrong code for this, by all means correct me.
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,151
Location
Sydney, Australia
Programming Experience
10+
If you want to display images then your app obviously needs a GUI of some kind so where else would you draw the images but on a form? If your event handler is not being executed then your event must not be being raised. The Paint event of a control will only not be raised if the control is not displayed. Are you adding that PictureBox to a form? If not then it's not going to be displayed and therefore never painted.
 

DarkD

Member
Joined
Jun 16, 2014
Messages
16
Programming Experience
Beginner
Ok so I got the image to draw like I wanted, but is a form the right choice for a board game? I am working on a practicum and need to make a boardgame with various requirements, and I don't wanna get halfway through and findout that forms don't support transformations. IE rotation, scaling, dynamic image additions. The boardgame will have a limited 3D effect basically, not to the level of models, although animations is one of the requirements I need to add later on.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,151
Location
Sydney, Australia
Programming Experience
10+
Unless you go the way of a full-screen DirectX game, you're creating a Windows app so you need to use either WinForms or WPF. If it's WinForms then everything goes on a form. If it's WPF then everything goes on a window. They are basically the same thing.
 

DarkD

Member
Joined
Jun 16, 2014
Messages
16
Programming Experience
Beginner
Back to this question, can winforms be used for a browser version of everything I mentioned here? I just read this

c# - Running a WinForms Application Inside Web Browser - Stack Overflow

and it says that it won't work. The person here recommends switching to ASP.Net. Will this require severe overhauling of my winforms code? Sorry, I forgot to mention that it was a browser game.

edit

Just noticed theres a "webforms". How similar is that to winforms?
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,151
Location
Sydney, Australia
Programming Experience
10+
Windows Forms is a technology specifically for creating Windows applications. As suggested, it will not run in a browser. You can make WPF apps run in a browser but that would require you to overhaul your code anyway. Silverlight is also a supposed subset of WPF that specifically runs in a browser but Silverlight doesn;t really exist as an entity going forward.

Web Forms was designed to work as much like WinForms as possible, given that the environment in which web apps exist is quite different to that for Windows apps. You can add controls to a form in a designer, drag them around, set their properties and handle their events. It is, in my opinion, quite clunky though. For an event handler to be executed, the web page has to post back to the server. Continual postbacks is a performance issue. Web Forms does support AJAX though, so that can help.

The best technology depends on the specifics of the application.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,151
Location
Sydney, Australia
Programming Experience
10+
Top Bottom