?
Solved

Display GIF using Graphic and Image in C#

Posted on 2006-04-14
5
Medium Priority
?
282 Views
Last Modified: 2010-04-16
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)
            {
                              
            }
      }
}
0
Comment
Question by:4eyesgirl
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16458671
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
 
LVL 11

Accepted Solution

by:
vo1d earned 2000 total points
ID: 16460136
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
 

Author Comment

by:4eyesgirl
ID: 16460499
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
 
LVL 11

Expert Comment

by:vo1d
ID: 16460989
hi 4eyesgirl,
idle minds comment should work too.
the only difference is the calltime of both functions.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16461031
You couldn't see your image because you weren't actually painting it anywhere!

=O
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question