locked
Tutorial "Backgound Image on Form" throws NullReferenceException RRS feed

  • Question

  • Greetings,

    I am looking for a solution to the below problem or a code example that we show me how to change the background image on a form.

    Thank You

    John K

    Using the below example I'm trying to change the background image on my Form. I am using Visual Studio 2005 and programming in C#. The target is mobile phone 2003. The JohnK.jpg is added to the project and the "Build Action" is set to  "Embedded Reference"

    This code looks good to me (a novice). The example is for a PDA. It should still work.

     

    http://samples.gotdotnet.com/quickstart/CompactFramework/doc/bkgndimage.aspx

    Everytime I add the recommended code snippet:

    public Form1()

    {

    InitializeComponent();

    backgroundImage = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("ConstructCalc.JohnK.jpg"));


    }

    The emulator throws the following exception:

    System.NullReferenceException was unhandled
      Message="NullReferenceException"


     

    Thursday, January 18, 2007 4:35 PM

Answers

  • When using resources embedded by VS, you need to remember that VS prefixes your resource with the default namespace for your project.

    You can also get the full path to your resource using ILDASM.exe. Look in the manifest for the resource with the name you want and make a note of what the full path is. Try passing that to GetManifestResourceStream.

    <><

    Friday, January 19, 2007 11:43 PM

All replies

  • Please use debugger to figure out what exactly is null and fix it accordingly. Probably resource name is incorrect ore resource is missing so you get no stream.

    Thursday, January 18, 2007 5:34 PM
  • I'm sorry can you exspand on this. I'm new to this IDE and the debugger is vague and ambiguous at best.

    My point of view is that MS should not deploy code that simply does not work.

    Thanks,

    John K

     

    Thursday, January 18, 2007 7:05 PM
  • VS comes with a great help system, so consider hitting F1 and reading "How do I..." section. I'm sure you’ll figure it out in no time even if that’s the first time you see VS debugger.

    As to samples, GotDotNet hosts many user samples which are provided by your fellow developers. I'm fairly confident they do work - you probably simply mistyped bitmap name or something like this. It’s all good though – great opportunity to learn how to use VS to solve trivial everyday issues like this one.

    Thursday, January 18, 2007 10:06 PM
  •  

    Look it I'm grateful that for you trying to help but, your opinions are wrong. The help is overwhelming in the amount of results that it returns. In fact for this problem ,it does not work at all. Yes I checked the settings. If ever I'm asked what IDE to use for smart phone development, I'll say avoid Visual Studio.

    Again I really appriate your effort trying to show me how to fish but I just want the fish! I want a form with picture in the background. I cannot care less how it gets there.

    If you cannot offer a concise fix then please don't chime in.  Because you are not helping. Now I have to repost the problem in hopes that someone who actually experience this trouble can step up and help me.  

    Again Thanks 

    John K

     

     

     

    Friday, January 19, 2007 12:09 AM
  • No problem, John, I will ignore your posts from now on.

    There's no need to repost anything, just don’t mark this thread as answered and may be somebody would post a fish for you.

    Friday, January 19, 2007 1:11 AM

  • It may work. Try it out. If you still have any, let me know

    Bitmap bmp = TRYPerformanceMain.Properties.Resources.Image;
    Friday, January 19, 2007 12:05 PM
  •  

    Greetings,

    It did show me that I commented out :

    private Image backgroundImage; while trouble shooting.

    However, this still did not work. It still throws the exception at InitializeComponet().

    I have checked to make sure "JohnK" is a embedded resource.

    I'd be willing to buy a custom Form componet that has the background property enabled if you know of one.

    Thanks for help

    John K 

    public Form1()

    {

    backgroundImage = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("ConstructCalc.JohnK"));

    InitializeComponent(); throws NullReferenceException here

    }

    Friday, January 19, 2007 5:58 PM
  • Greetings,

    I just test tested the code below

    (http://msdn2.microsoft.com/en-us/library/ms172529(VS.80).aspx)

     in a vigin project and it too generates the exact same exception, too bad.

    In both cases the code provided by M$ does not work.

    Time to move on.

    John K

    protected override void OnPaint(PaintEventArgs e)
    {       
        // Get image compiled as an embedded resource.
        Assembly asm = Assembly.GetExecutingAssembly();
        Bitmap backgroundImage = new Bitmap(asm.GetManifestResourceStream("mypicture.jpg"));

        e.Graphics.DrawImage(backgroundImage, this.ClientRectangle,
            new Rectangle(0,0, backgroundImage.Width, backgroundImage.Height),
            GraphicsUnit.Pixel);
    }

    Friday, January 19, 2007 10:10 PM
  • When using resources embedded by VS, you need to remember that VS prefixes your resource with the default namespace for your project.

    You can also get the full path to your resource using ILDASM.exe. Look in the manifest for the resource with the name you want and make a note of what the full path is. Try passing that to GetManifestResourceStream.

    <><

    Friday, January 19, 2007 11:43 PM