1

I am dynamically adding an image to a canvas object which was working for a while and suddenly stopped displaying for some particular reason.

Here is the code :

public void shootProjectile()
{

    var projectileImage = new Image();
    var currChar = (CharactersRef.SCharacterProjectile)lstSubMenu.SelectedItem;
    projectileImage.Name = currChar.projectileName;
    Uri imageUri = new Uri("/Images/Projectiles/"+ currChar.projectileName +".png", UriKind.Relative);

    projectileImage.Height = currChar.projectileHeight;
    projectileImage.Width=currChar.projectileWidth;
    var currPuppet = pg.PuppetList.SingleOrDefault(p => p.puppetID == turnID);
    var projectileY = Convert.ToDouble(pcCombatArea.PhysicsObjects[currPuppet.CharacterName+"torso"].GetValue(Canvas.TopProperty));
    var projectileX = Convert.ToDouble(pcCombatArea.PhysicsObjects[currPuppet.CharacterName + "torso"].GetValue(Canvas.LeftProperty));

    if (currPuppet.faceRight)
    {
        projectileX += currPuppet.elbowToArmpit + currPuppet.elbowToHandTip;
    }
    else
    {
        projectileX -= currPuppet.elbowToArmpit + currPuppet.elbowToHandTip;
    }           
    projectileImage.SetValue(Canvas.LeftProperty,projectileX);
    projectileImage.SetValue(Canvas.TopProperty, projectileY);
    cnvGame.Children.Add(projectileImage);
    projectileImage.Source = new BitmapImage(imageUri);
    projectileImage.ImageOpened += new EventHandler<RoutedEventArgs>(projectileImage_ImageOpened);
}

The Top and Left values are well in the range of the canvas size, (The canvas is 800 by 600 whilst the coordinates are round 200, 100). I can find the images in the canvas children, and the url seems to be ok, I double checked it. I set the png image Build type to resource but to no avail. If you notice I have added an ImageOpened even which is not being triggered.

1 Answer 1

1

If you have the image build type set to Resource, then try removing the leading '/' from the image URI.

Sign up to request clarification or add additional context in comments.

1 Comment

Changed the build type to content and it worked, will try out resource later

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.