Display GIF using Graphic and Image in C#

Hi Expert,

I am following the book to display a .gif file using Image and Graphic, when running the program, I cannot see the image at all, any idea why?
Image.gif -> is located in the executable path
And I was able to open the IMage.gif file from the paint.  Why this image does not displayed int he Form?


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace IMagesDisplay
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
            private Image image = Image.FromFile("Image.gif");
            private Graphics graphicsObject;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //

                  graphicsObject = this.CreateGraphics();
                  
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(920, 446);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.Load += new System.EventHandler(this.Form1_Load);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
                              
            }
      }
}
4eyesgirlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Use the graphics supplied in the Paint() event of the form:

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(image, new Point(0, 0));
        }

Get rid of the "graphicsObject" variable and the call to CreateGraphics().
0
vo1dCommented:
or, if you dont want to draw in the forms paint event, override its onpaint method:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.DrawImage(image, new Point(0, 0));
}

this implementation has the advantage, that you still paint your image right after the forms painted itself.
reacting on events have sometimes the problem, that you have to wait for the processed event.
if you have to solve some timecritical things, override the forms wndproc and do your painting in the wm_paint message.
this funtion is processed before any other events will be fired cause it is based on the windows messaging system.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
4eyesgirlAuthor Commented:
Thanks vo1d,

This explains why I cannot see the image, because the system painted the background after loading the image.

Very good, Thank you so much
0
vo1dCommented:
hi 4eyesgirl,
idle minds comment should work too.
the only difference is the calltime of both functions.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You couldn't see your image because you weren't actually painting it anywhere!

=O
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.